OmdbProvider.cs 7.2 KB

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