OmdbProvider.cs 8.5 KB

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