PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BaconographyW8Core/PlatformServices/ImageAPI/Imgur.cs

https://github.com/hippiehunter/Baconography
C# | 95 lines | 81 code | 11 blank | 3 comment | 17 complexity | d4d0de356178725b6b1a74b794c2138a MD5 | raw file
  1. using BaconographyW8.PlatformServices;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. namespace Baconography.PlatformServices.ImageAPI
  14. {
  15. class Imgur
  16. {
  17. //Transliterated from Reddit Enhancement Suite https://github.com/honestbleeps/Reddit-Enhancement-Suite/blob/master/lib/reddit_enhancement_suite.user.js
  18. private static Regex hashRe = new Regex(@"^https?:\/\/(?:[i.]|[edge.]|[www.])*imgur.com\/(?:r\/[\w]+\/)?([\w]{5,}(?:[&,][\w]{5,})?)(\.[\w]{3,4})?(?:#(\d*))?(?:\?(?:\d*))?$");
  19. private static Regex albumHashRe = new Regex(@"^https?:\/\/(?:i\.)?imgur.com\/a\/([\w]+)(\..+)?(?:\/)?(?:#\d*)?$");
  20. private static string apiPrefix = "http://api.imgur.com/2/";
  21. internal static async Task<IEnumerable<Tuple<string, string>>> GetImagesFromUri(string title, Uri uri)
  22. {
  23. var href = uri.OriginalString;
  24. var groups = hashRe.Match(href).Groups;
  25. GroupCollection albumGroups = null;
  26. if (groups.Count == 0 || (groups.Count > 0 && string.IsNullOrWhiteSpace(groups[0].Value)))
  27. albumGroups = albumHashRe.Match(href).Groups;
  28. if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
  29. {
  30. if (Regex.IsMatch(groups[1].Value, "[&,]"))
  31. {
  32. var hashes = Regex.Split(groups[1].Value, "[&,]");
  33. //Imgur doesn't really care about the extension and the browsers don't seem to either.
  34. return hashes
  35. .Select(hash => Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", hash)));
  36. }
  37. else
  38. {
  39. if (uri.AbsolutePath.ToLower().StartsWith("/gallery"))
  40. {
  41. return await GetImagesFromUri(title, new Uri("http://imgur.com/a/" + groups[1].Value));
  42. }
  43. else
  44. {
  45. //Imgur doesn't really care about the extension and the browsers don't seem to either.
  46. return new Tuple<string, string>[] { Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", groups[1].Value)) };
  47. }
  48. }
  49. }
  50. else if (albumGroups.Count > 2 && string.IsNullOrWhiteSpace(albumGroups[2].Value))
  51. {
  52. var apiURL = string.Format("{0}album/{1}.json", apiPrefix, albumGroups[1].Value);
  53. var getClient = new HttpClient();
  54. var jsonResult = await getClient.GetStringAsync(apiURL);
  55. if (string.IsNullOrWhiteSpace(jsonResult))
  56. return Enumerable.Empty<Tuple<string, string>>();
  57. var result = JsonConvert.DeserializeObject(jsonResult) as JObject;
  58. if (result != null && result.HasValues)
  59. {
  60. JToken errorToken;
  61. if (result.TryGetValue("error", out errorToken))
  62. {
  63. return Enumerable.Empty<Tuple<string, string>>();
  64. }
  65. var albumTitleElement = (string)((JObject)result.GetValue("album")).GetValue("title");
  66. var albumTitle = string.IsNullOrWhiteSpace(albumTitleElement) ? title : albumTitleElement;
  67. return ((IEnumerable)((JObject)result.GetValue("album")).GetValue("images"))
  68. .Cast<JObject>()
  69. .Select(e =>
  70. {
  71. var caption = (string)((JObject)e.GetValue("image")).GetValue("caption");
  72. if (!string.IsNullOrWhiteSpace(caption))
  73. caption = caption.Replace("&#039;", "'").Replace("&#038;", "&").Replace("&#034;", "\"");
  74. return Tuple.Create(string.IsNullOrWhiteSpace(caption) ? albumTitle : caption, (string)((JObject)e.GetValue("links")).GetValue("original"));
  75. });
  76. }
  77. else
  78. return Enumerable.Empty<Tuple<string, string>>();
  79. }
  80. else
  81. return Enumerable.Empty<Tuple<string, string>>();
  82. }
  83. }
  84. }