OmdbProvider.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Serialization;
  5. using System;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Providers.Omdb
  12. {
  13. public class OmdbProvider
  14. {
  15. internal static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(1, 1);
  16. private readonly IJsonSerializer _jsonSerializer;
  17. private readonly IHttpClient _httpClient;
  18. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  19. public static OmdbProvider Current;
  20. public OmdbProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient)
  21. {
  22. _jsonSerializer = jsonSerializer;
  23. _httpClient = httpClient;
  24. Current = this;
  25. }
  26. public async Task Fetch(BaseItem item, string imdbId, CancellationToken cancellationToken)
  27. {
  28. if (string.IsNullOrWhiteSpace(imdbId))
  29. {
  30. throw new ArgumentNullException("imdbId");
  31. }
  32. var imdbParam = imdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? imdbId : "tt" + imdbId;
  33. var url = string.Format("http://www.omdbapi.com/?i={0}&tomatoes=true", imdbParam);
  34. using (var stream = await _httpClient.Get(new HttpRequestOptions
  35. {
  36. Url = url,
  37. ResourcePool = ResourcePool,
  38. CancellationToken = cancellationToken,
  39. CacheMode = CacheMode.Unconditional,
  40. CacheLength = TimeSpan.FromDays(7)
  41. }).ConfigureAwait(false))
  42. {
  43. var result = _jsonSerializer.DeserializeFromStream<RootObject>(stream);
  44. item.Name = result.Title;
  45. int year;
  46. if (!string.IsNullOrEmpty(result.Year)
  47. && int.TryParse(result.Year, NumberStyles.Number, _usCulture, out year)
  48. && year >= 0)
  49. {
  50. item.ProductionYear = year;
  51. }
  52. item.OfficialRating = result.Rated;
  53. var hasCriticRating = item as IHasCriticRating;
  54. if (hasCriticRating != null)
  55. {
  56. // Seeing some bogus RT data on omdb for series, so filter it out here
  57. // RT doesn't even have tv series
  58. int tomatoMeter;
  59. if (!string.IsNullOrEmpty(result.tomatoMeter)
  60. && int.TryParse(result.tomatoMeter, NumberStyles.Integer, _usCulture, out tomatoMeter)
  61. && tomatoMeter >= 0)
  62. {
  63. hasCriticRating.CriticRating = tomatoMeter;
  64. }
  65. if (!string.IsNullOrEmpty(result.tomatoConsensus)
  66. && !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
  67. && !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
  68. {
  69. hasCriticRating.CriticRatingSummary = WebUtility.HtmlDecode(result.tomatoConsensus);
  70. }
  71. }
  72. int voteCount;
  73. if (!string.IsNullOrEmpty(result.imdbVotes)
  74. && int.TryParse(result.imdbVotes, NumberStyles.Number, _usCulture, out voteCount)
  75. && voteCount >= 0)
  76. {
  77. item.VoteCount = voteCount;
  78. }
  79. float imdbRating;
  80. if (!string.IsNullOrEmpty(result.imdbRating)
  81. && float.TryParse(result.imdbRating, NumberStyles.Any, _usCulture, out imdbRating)
  82. && imdbRating >= 0)
  83. {
  84. item.CommunityRating = imdbRating;
  85. }
  86. if (!string.IsNullOrEmpty(result.Website)
  87. && !string.Equals(result.Website, "n/a", StringComparison.OrdinalIgnoreCase))
  88. {
  89. item.HomePageUrl = result.Website;
  90. }
  91. if (!string.IsNullOrWhiteSpace(result.imdbID)
  92. && !string.Equals(result.imdbID, "n/a", StringComparison.OrdinalIgnoreCase))
  93. {
  94. item.SetProviderId(MetadataProviders.Imdb, result.imdbID);
  95. }
  96. ParseAdditionalMetadata(item, result);
  97. }
  98. }
  99. private void ParseAdditionalMetadata(BaseItem item, RootObject result)
  100. {
  101. // Grab series genres because imdb data is better than tvdb. Leave movies alone
  102. // But only do it if english is the preferred language because this data will not be localized
  103. if (ShouldFetchGenres(item) &&
  104. !string.IsNullOrWhiteSpace(result.Genre) &&
  105. !string.Equals(result.Genre, "n/a", StringComparison.OrdinalIgnoreCase))
  106. {
  107. item.Genres.Clear();
  108. foreach (var genre in result.Genre
  109. .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  110. .Select(i => i.Trim())
  111. .Where(i => !string.IsNullOrWhiteSpace(i)))
  112. {
  113. item.AddGenre(genre);
  114. }
  115. }
  116. var hasMetascore = item as IHasMetascore;
  117. if (hasMetascore != null)
  118. {
  119. float metascore;
  120. if (!string.IsNullOrEmpty(result.Metascore) && float.TryParse(result.Metascore, NumberStyles.Any, _usCulture, out metascore) && metascore >= 0)
  121. {
  122. hasMetascore.Metascore = metascore;
  123. }
  124. }
  125. var hasAwards = item as IHasAwards;
  126. if (hasAwards != null && !string.IsNullOrEmpty(result.Awards) &&
  127. !string.Equals(result.Awards, "n/a", StringComparison.OrdinalIgnoreCase))
  128. {
  129. hasAwards.AwardSummary = WebUtility.HtmlDecode(result.Awards);
  130. }
  131. var hasShortOverview = item as IHasShortOverview;
  132. if (hasShortOverview != null)
  133. {
  134. // Imdb plots are usually pretty short
  135. hasShortOverview.ShortOverview = result.Plot;
  136. }
  137. }
  138. private bool ShouldFetchGenres(BaseItem item)
  139. {
  140. var lang = item.GetPreferredMetadataLanguage();
  141. // The data isn't localized and so can only be used for english users
  142. return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
  143. }
  144. private class RootObject
  145. {
  146. public string Title { get; set; }
  147. public string Year { get; set; }
  148. public string Rated { get; set; }
  149. public string Released { get; set; }
  150. public string Runtime { get; set; }
  151. public string Genre { get; set; }
  152. public string Director { get; set; }
  153. public string Writer { get; set; }
  154. public string Actors { get; set; }
  155. public string Plot { get; set; }
  156. public string Poster { get; set; }
  157. public string imdbRating { get; set; }
  158. public string imdbVotes { get; set; }
  159. public string imdbID { get; set; }
  160. public string Type { get; set; }
  161. public string tomatoMeter { get; set; }
  162. public string tomatoImage { get; set; }
  163. public string tomatoRating { get; set; }
  164. public string tomatoReviews { get; set; }
  165. public string tomatoFresh { get; set; }
  166. public string tomatoRotten { get; set; }
  167. public string tomatoConsensus { get; set; }
  168. public string tomatoUserMeter { get; set; }
  169. public string tomatoUserRating { get; set; }
  170. public string tomatoUserReviews { get; set; }
  171. public string DVD { get; set; }
  172. public string BoxOffice { get; set; }
  173. public string Production { get; set; }
  174. public string Website { get; set; }
  175. public string Response { get; set; }
  176. public string Language { get; set; }
  177. public string Country { get; set; }
  178. public string Awards { get; set; }
  179. public string Metascore { get; set; }
  180. }
  181. }
  182. }