OpenMovieDatabaseProvider.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Providers.Movies
  16. {
  17. public class OpenMovieDatabaseProvider : BaseMetadataProvider
  18. {
  19. private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
  20. /// <summary>
  21. /// Gets the json serializer.
  22. /// </summary>
  23. /// <value>The json serializer.</value>
  24. protected IJsonSerializer JsonSerializer { get; private set; }
  25. /// <summary>
  26. /// Gets the HTTP client.
  27. /// </summary>
  28. /// <value>The HTTP client.</value>
  29. protected IHttpClient HttpClient { get; private set; }
  30. public OpenMovieDatabaseProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
  31. : base(logManager, configurationManager)
  32. {
  33. JsonSerializer = jsonSerializer;
  34. HttpClient = httpClient;
  35. }
  36. /// <summary>
  37. /// Gets the provider version.
  38. /// </summary>
  39. /// <value>The provider version.</value>
  40. protected override string ProviderVersion
  41. {
  42. get
  43. {
  44. return "13";
  45. }
  46. }
  47. /// <summary>
  48. /// Gets a value indicating whether [requires internet].
  49. /// </summary>
  50. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  51. public override bool RequiresInternet
  52. {
  53. get
  54. {
  55. return true;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets a value indicating whether [refresh on version change].
  60. /// </summary>
  61. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  62. protected override bool RefreshOnVersionChange
  63. {
  64. get
  65. {
  66. return true;
  67. }
  68. }
  69. /// <summary>
  70. /// Supports the specified item.
  71. /// </summary>
  72. /// <param name="item">The item.</param>
  73. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  74. public override bool Supports(BaseItem item)
  75. {
  76. var trailer = item as Trailer;
  77. // Don't support local trailers
  78. if (trailer != null)
  79. {
  80. return !trailer.IsLocalTrailer;
  81. }
  82. return item is Movie || item is MusicVideo || item is Series;
  83. }
  84. /// <summary>
  85. /// Gets the priority.
  86. /// </summary>
  87. /// <value>The priority.</value>
  88. public override MetadataProviderPriority Priority
  89. {
  90. get
  91. {
  92. // Run after moviedb and xml providers
  93. return MetadataProviderPriority.Fifth;
  94. }
  95. }
  96. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  97. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  98. {
  99. BaseProviderInfo data;
  100. if (!item.ProviderData.TryGetValue(Id, out data))
  101. {
  102. data = new BaseProviderInfo();
  103. item.ProviderData[Id] = data;
  104. }
  105. var imdbId = item.GetProviderId(MetadataProviders.Imdb);
  106. if (string.IsNullOrEmpty(imdbId))
  107. {
  108. data.LastRefreshStatus = ProviderRefreshStatus.Success;
  109. return true;
  110. }
  111. var imdbParam = imdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? imdbId : "tt" + imdbId;
  112. var url = string.Format("http://www.omdbapi.com/?i={0}&tomatoes=true", imdbParam);
  113. using (var stream = await HttpClient.Get(new HttpRequestOptions
  114. {
  115. Url = url,
  116. ResourcePool = _resourcePool,
  117. CancellationToken = cancellationToken
  118. }).ConfigureAwait(false))
  119. {
  120. var result = JsonSerializer.DeserializeFromStream<RootObject>(stream);
  121. var hasCriticRating = item as IHasCriticRating;
  122. if (hasCriticRating != null)
  123. {
  124. // Seeing some bogus RT data on omdb for series, so filter it out here
  125. // RT doesn't even have tv series
  126. int tomatoMeter;
  127. if (!string.IsNullOrEmpty(result.tomatoMeter)
  128. && int.TryParse(result.tomatoMeter, NumberStyles.Integer, UsCulture, out tomatoMeter)
  129. && tomatoMeter >= 0)
  130. {
  131. hasCriticRating.CriticRating = tomatoMeter;
  132. }
  133. if (!string.IsNullOrEmpty(result.tomatoConsensus)
  134. && !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
  135. && !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
  136. {
  137. hasCriticRating.CriticRatingSummary = result.tomatoConsensus;
  138. }
  139. }
  140. int voteCount;
  141. if (!string.IsNullOrEmpty(result.imdbVotes)
  142. && int.TryParse(result.imdbVotes, NumberStyles.Number, UsCulture, out voteCount)
  143. && voteCount >= 0)
  144. {
  145. item.VoteCount = voteCount;
  146. }
  147. float imdbRating;
  148. if (!string.IsNullOrEmpty(result.imdbRating)
  149. && float.TryParse(result.imdbRating, NumberStyles.Any, UsCulture, out imdbRating)
  150. && imdbRating >= 0)
  151. {
  152. item.CommunityRating = imdbRating;
  153. }
  154. ParseAdditionalMetadata(item, result);
  155. }
  156. data.LastRefreshStatus = ProviderRefreshStatus.Success;
  157. SetLastRefreshed(item, DateTime.UtcNow);
  158. return true;
  159. }
  160. private void ParseAdditionalMetadata(BaseItem item, RootObject result)
  161. {
  162. // Grab series genres because imdb data is better than tvdb. Leave movies alone
  163. // But only do it if english is the preferred language because this data will not be localized
  164. if (!item.LockedFields.Contains(MetadataFields.Genres) &&
  165. ShouldFetchGenres(item) &&
  166. !string.IsNullOrWhiteSpace(result.Genre) &&
  167. !string.Equals(result.Genre, "n/a", StringComparison.OrdinalIgnoreCase))
  168. {
  169. item.Genres.Clear();
  170. foreach (var genre in result.Genre
  171. .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  172. .Select(i => i.Trim())
  173. .Where(i => !string.IsNullOrWhiteSpace(i)))
  174. {
  175. item.AddGenre(genre);
  176. }
  177. }
  178. }
  179. private bool ShouldFetchGenres(BaseItem item)
  180. {
  181. // Only fetch if other providers didn't get anything
  182. if (item is Trailer)
  183. {
  184. return item.Genres.Count == 0;
  185. }
  186. return item is Series;
  187. }
  188. protected class RootObject
  189. {
  190. public string Title { get; set; }
  191. public string Year { get; set; }
  192. public string Rated { get; set; }
  193. public string Released { get; set; }
  194. public string Runtime { get; set; }
  195. public string Genre { get; set; }
  196. public string Director { get; set; }
  197. public string Writer { get; set; }
  198. public string Actors { get; set; }
  199. public string Plot { get; set; }
  200. public string Poster { get; set; }
  201. public string imdbRating { get; set; }
  202. public string imdbVotes { get; set; }
  203. public string imdbID { get; set; }
  204. public string Type { get; set; }
  205. public string tomatoMeter { get; set; }
  206. public string tomatoImage { get; set; }
  207. public string tomatoRating { get; set; }
  208. public string tomatoReviews { get; set; }
  209. public string tomatoFresh { get; set; }
  210. public string tomatoRotten { get; set; }
  211. public string tomatoConsensus { get; set; }
  212. public string tomatoUserMeter { get; set; }
  213. public string tomatoUserRating { get; set; }
  214. public string tomatoUserReviews { get; set; }
  215. public string DVD { get; set; }
  216. public string BoxOffice { get; set; }
  217. public string Production { get; set; }
  218. public string Website { get; set; }
  219. public string Response { get; set; }
  220. }
  221. }
  222. }