OmdbProvider.cs 7.5 KB

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