Baconography /BaconographyW8Core/PlatformServices/ImageAPI/Imgur.cs

Language C# Lines 96
MD5 Hash d4d0de356178725b6b1a74b794c2138a
Repository https://github.com/hippiehunter/Baconography.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using BaconographyW8.PlatformServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Baconography.PlatformServices.ImageAPI
{
    class Imgur
    {
        //Transliterated from Reddit Enhancement Suite https://github.com/honestbleeps/Reddit-Enhancement-Suite/blob/master/lib/reddit_enhancement_suite.user.js
        private static Regex hashRe = new Regex(@"^https?:\/\/(?:[i.]|[edge.]|[www.])*imgur.com\/(?:r\/[\w]+\/)?([\w]{5,}(?:[&,][\w]{5,})?)(\.[\w]{3,4})?(?:#(\d*))?(?:\?(?:\d*))?$");
        private static Regex albumHashRe = new Regex(@"^https?:\/\/(?:i\.)?imgur.com\/a\/([\w]+)(\..+)?(?:\/)?(?:#\d*)?$");
        private static string apiPrefix = "http://api.imgur.com/2/";

        internal static async Task<IEnumerable<Tuple<string, string>>> GetImagesFromUri(string title, Uri uri)
        {
            var href = uri.OriginalString;
            var groups = hashRe.Match(href).Groups;
            GroupCollection albumGroups = null;

			if (groups.Count == 0 || (groups.Count > 0 && string.IsNullOrWhiteSpace(groups[0].Value)))
				albumGroups = albumHashRe.Match(href).Groups;

			if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
			{
				if (Regex.IsMatch(groups[1].Value, "[&,]"))
				{
					var hashes = Regex.Split(groups[1].Value, "[&,]");
					//Imgur doesn't really care about the extension and the browsers don't seem to either.
					return hashes
						.Select(hash => Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", hash)));

				}
				else
				{
					if (uri.AbsolutePath.ToLower().StartsWith("/gallery"))
					{
						return await GetImagesFromUri(title, new Uri("http://imgur.com/a/" + groups[1].Value));
					}
					else
					{
						//Imgur doesn't really care about the extension and the browsers don't seem to either.
						return new Tuple<string, string>[] { Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", groups[1].Value)) };
					}
				}
			}
			else if (albumGroups.Count > 2 && string.IsNullOrWhiteSpace(albumGroups[2].Value))
			{
				var apiURL = string.Format("{0}album/{1}.json", apiPrefix, albumGroups[1].Value);
				var getClient = new HttpClient();
				var jsonResult = await getClient.GetStringAsync(apiURL);

				if (string.IsNullOrWhiteSpace(jsonResult))
					return Enumerable.Empty<Tuple<string, string>>();

				var result = JsonConvert.DeserializeObject(jsonResult) as JObject;
				if (result != null && result.HasValues)
				{
					JToken errorToken;
					if (result.TryGetValue("error", out errorToken))
					{
						return Enumerable.Empty<Tuple<string, string>>();
					}

					var albumTitleElement = (string)((JObject)result.GetValue("album")).GetValue("title");
					var albumTitle = string.IsNullOrWhiteSpace(albumTitleElement) ? title : albumTitleElement;

					return ((IEnumerable)((JObject)result.GetValue("album")).GetValue("images"))
						.Cast<JObject>()
						.Select(e =>
						{
							var caption = (string)((JObject)e.GetValue("image")).GetValue("caption");

							if (!string.IsNullOrWhiteSpace(caption))
								caption = caption.Replace("&#039;", "'").Replace("&#038;", "&").Replace("&#034;", "\"");

							return Tuple.Create(string.IsNullOrWhiteSpace(caption) ? albumTitle : caption, (string)((JObject)e.GetValue("links")).GetValue("original"));
						});
				}
				else
					return Enumerable.Empty<Tuple<string, string>>();
			}
			else
				return Enumerable.Empty<Tuple<string, string>>();
        }
    }
}
Back to Top