OmdbProvider.cs 7.6 KB

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