OmdbProvider.cs 7.1 KB

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