OmdbProvider.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, string language, string country, 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. // Only take the name and rating if the user's language is set to english, since Omdb has no localization
  45. if (string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
  46. {
  47. item.Name = result.Title;
  48. if (string.Equals(country, "us", StringComparison.OrdinalIgnoreCase))
  49. {
  50. item.OfficialRating = result.Rated;
  51. }
  52. }
  53. int year;
  54. if (!string.IsNullOrEmpty(result.Year)
  55. && int.TryParse(result.Year, NumberStyles.Number, _usCulture, out year)
  56. && year >= 0)
  57. {
  58. item.ProductionYear = year;
  59. }
  60. var hasCriticRating = item as IHasCriticRating;
  61. if (hasCriticRating != null)
  62. {
  63. // Seeing some bogus RT data on omdb for series, so filter it out here
  64. // RT doesn't even have tv series
  65. int tomatoMeter;
  66. if (!string.IsNullOrEmpty(result.tomatoMeter)
  67. && int.TryParse(result.tomatoMeter, NumberStyles.Integer, _usCulture, out tomatoMeter)
  68. && tomatoMeter >= 0)
  69. {
  70. hasCriticRating.CriticRating = tomatoMeter;
  71. }
  72. if (!string.IsNullOrEmpty(result.tomatoConsensus)
  73. && !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
  74. && !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
  75. {
  76. hasCriticRating.CriticRatingSummary = WebUtility.HtmlDecode(result.tomatoConsensus);
  77. }
  78. }
  79. int voteCount;
  80. if (!string.IsNullOrEmpty(result.imdbVotes)
  81. && int.TryParse(result.imdbVotes, NumberStyles.Number, _usCulture, out voteCount)
  82. && voteCount >= 0)
  83. {
  84. item.VoteCount = voteCount;
  85. }
  86. float imdbRating;
  87. if (!string.IsNullOrEmpty(result.imdbRating)
  88. && float.TryParse(result.imdbRating, NumberStyles.Any, _usCulture, out imdbRating)
  89. && imdbRating >= 0)
  90. {
  91. item.CommunityRating = imdbRating;
  92. }
  93. if (!string.IsNullOrEmpty(result.Website)
  94. && !string.Equals(result.Website, "n/a", StringComparison.OrdinalIgnoreCase))
  95. {
  96. item.HomePageUrl = result.Website;
  97. }
  98. if (!string.IsNullOrWhiteSpace(result.imdbID)
  99. && !string.Equals(result.imdbID, "n/a", StringComparison.OrdinalIgnoreCase))
  100. {
  101. item.SetProviderId(MetadataProviders.Imdb, result.imdbID);
  102. }
  103. ParseAdditionalMetadata(item, result);
  104. }
  105. }
  106. private void ParseAdditionalMetadata(BaseItem item, RootObject result)
  107. {
  108. // Grab series genres because imdb data is better than tvdb. Leave movies alone
  109. // But only do it if english is the preferred language because this data will not be localized
  110. if (ShouldFetchGenres(item) &&
  111. !string.IsNullOrWhiteSpace(result.Genre) &&
  112. !string.Equals(result.Genre, "n/a", StringComparison.OrdinalIgnoreCase))
  113. {
  114. item.Genres.Clear();
  115. foreach (var genre in result.Genre
  116. .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  117. .Select(i => i.Trim())
  118. .Where(i => !string.IsNullOrWhiteSpace(i)))
  119. {
  120. item.AddGenre(genre);
  121. }
  122. }
  123. var hasMetascore = item as IHasMetascore;
  124. if (hasMetascore != null)
  125. {
  126. float metascore;
  127. if (!string.IsNullOrEmpty(result.Metascore) && float.TryParse(result.Metascore, NumberStyles.Any, _usCulture, out metascore) && metascore >= 0)
  128. {
  129. hasMetascore.Metascore = metascore;
  130. }
  131. }
  132. var hasAwards = item as IHasAwards;
  133. if (hasAwards != null && !string.IsNullOrEmpty(result.Awards) &&
  134. !string.Equals(result.Awards, "n/a", StringComparison.OrdinalIgnoreCase))
  135. {
  136. hasAwards.AwardSummary = WebUtility.HtmlDecode(result.Awards);
  137. }
  138. var hasShortOverview = item as IHasShortOverview;
  139. if (hasShortOverview != null)
  140. {
  141. // Imdb plots are usually pretty short
  142. hasShortOverview.ShortOverview = result.Plot;
  143. }
  144. }
  145. private bool ShouldFetchGenres(BaseItem item)
  146. {
  147. var lang = item.GetPreferredMetadataLanguage();
  148. // The data isn't localized and so can only be used for english users
  149. return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
  150. }
  151. private class RootObject
  152. {
  153. public string Title { get; set; }
  154. public string Year { get; set; }
  155. public string Rated { get; set; }
  156. public string Released { get; set; }
  157. public string Runtime { get; set; }
  158. public string Genre { get; set; }
  159. public string Director { get; set; }
  160. public string Writer { get; set; }
  161. public string Actors { get; set; }
  162. public string Plot { get; set; }
  163. public string Poster { get; set; }
  164. public string imdbRating { get; set; }
  165. public string imdbVotes { get; set; }
  166. public string imdbID { get; set; }
  167. public string Type { get; set; }
  168. public string tomatoMeter { get; set; }
  169. public string tomatoImage { get; set; }
  170. public string tomatoRating { get; set; }
  171. public string tomatoReviews { get; set; }
  172. public string tomatoFresh { get; set; }
  173. public string tomatoRotten { get; set; }
  174. public string tomatoConsensus { get; set; }
  175. public string tomatoUserMeter { get; set; }
  176. public string tomatoUserRating { get; set; }
  177. public string tomatoUserReviews { get; set; }
  178. public string DVD { get; set; }
  179. public string BoxOffice { get; set; }
  180. public string Production { get; set; }
  181. public string Website { get; set; }
  182. public string Response { get; set; }
  183. public string Language { get; set; }
  184. public string Country { get; set; }
  185. public string Awards { get; set; }
  186. public string Metascore { get; set; }
  187. }
  188. }
  189. }