OpenMovieDatabaseProvider.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Movies;
  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.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Providers.Movies
  15. {
  16. public class OpenMovieDatabaseProvider : BaseMetadataProvider
  17. {
  18. private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(2, 2);
  19. /// <summary>
  20. /// Gets the json serializer.
  21. /// </summary>
  22. /// <value>The json serializer.</value>
  23. protected IJsonSerializer JsonSerializer { get; private set; }
  24. /// <summary>
  25. /// Gets the HTTP client.
  26. /// </summary>
  27. /// <value>The HTTP client.</value>
  28. protected IHttpClient HttpClient { get; private set; }
  29. public OpenMovieDatabaseProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
  30. : base(logManager, configurationManager)
  31. {
  32. JsonSerializer = jsonSerializer;
  33. HttpClient = httpClient;
  34. }
  35. /// <summary>
  36. /// Gets the provider version.
  37. /// </summary>
  38. /// <value>The provider version.</value>
  39. protected override string ProviderVersion
  40. {
  41. get
  42. {
  43. return "6";
  44. }
  45. }
  46. /// <summary>
  47. /// Gets a value indicating whether [requires internet].
  48. /// </summary>
  49. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  50. public override bool RequiresInternet
  51. {
  52. get
  53. {
  54. return true;
  55. }
  56. }
  57. /// <summary>
  58. /// Gets a value indicating whether [refresh on version change].
  59. /// </summary>
  60. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  61. protected override bool RefreshOnVersionChange
  62. {
  63. get
  64. {
  65. return true;
  66. }
  67. }
  68. /// <summary>
  69. /// Supports the specified item.
  70. /// </summary>
  71. /// <param name="item">The item.</param>
  72. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  73. public override bool Supports(BaseItem item)
  74. {
  75. var trailer = item as Trailer;
  76. // Don't support local trailers
  77. if (trailer != null)
  78. {
  79. return !trailer.IsLocalTrailer;
  80. }
  81. return item is Movie || item is MusicVideo;
  82. }
  83. /// <summary>
  84. /// Gets the priority.
  85. /// </summary>
  86. /// <value>The priority.</value>
  87. public override MetadataProviderPriority Priority
  88. {
  89. get
  90. {
  91. // Run after moviedb and xml providers
  92. return MetadataProviderPriority.Last;
  93. }
  94. }
  95. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  96. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  97. {
  98. BaseProviderInfo data;
  99. if (!item.ProviderData.TryGetValue(Id, out data))
  100. {
  101. data = new BaseProviderInfo();
  102. item.ProviderData[Id] = data;
  103. }
  104. var imdbId = item.GetProviderId(MetadataProviders.Imdb);
  105. if (string.IsNullOrEmpty(imdbId))
  106. {
  107. data.LastRefreshStatus = ProviderRefreshStatus.Success;
  108. return true;
  109. }
  110. var imdbParam = imdbId.StartsWith("tt", StringComparison.OrdinalIgnoreCase) ? imdbId : "tt" + imdbId;
  111. var url = string.Format("http://www.omdbapi.com/?i={0}&tomatoes=true", imdbParam);
  112. using (var stream = await HttpClient.Get(new HttpRequestOptions
  113. {
  114. Url = url,
  115. ResourcePool = _resourcePool,
  116. CancellationToken = cancellationToken
  117. }).ConfigureAwait(false))
  118. {
  119. var result = JsonSerializer.DeserializeFromStream<RootObject>(stream);
  120. int tomatoMeter;
  121. if (!string.IsNullOrEmpty(result.tomatoMeter) && int.TryParse(result.tomatoMeter, NumberStyles.Integer, UsCulture, out tomatoMeter))
  122. {
  123. item.CriticRating = tomatoMeter;
  124. }
  125. if (!string.IsNullOrEmpty(result.tomatoConsensus)
  126. && !string.Equals(result.tomatoConsensus, "n/a", StringComparison.OrdinalIgnoreCase)
  127. && !string.Equals(result.tomatoConsensus, "No consensus yet.", StringComparison.OrdinalIgnoreCase))
  128. {
  129. item.CriticRatingSummary = result.tomatoConsensus;
  130. }
  131. }
  132. data.LastRefreshStatus = ProviderRefreshStatus.Success;
  133. SetLastRefreshed(item, DateTime.UtcNow);
  134. return true;
  135. }
  136. protected class RootObject
  137. {
  138. public string Title { get; set; }
  139. public string Year { get; set; }
  140. public string Rated { get; set; }
  141. public string Released { get; set; }
  142. public string Runtime { get; set; }
  143. public string Genre { get; set; }
  144. public string Director { get; set; }
  145. public string Writer { get; set; }
  146. public string Actors { get; set; }
  147. public string Plot { get; set; }
  148. public string Poster { get; set; }
  149. public string imdbRating { get; set; }
  150. public string imdbVotes { get; set; }
  151. public string imdbID { get; set; }
  152. public string Type { get; set; }
  153. public string tomatoMeter { get; set; }
  154. public string tomatoImage { get; set; }
  155. public string tomatoRating { get; set; }
  156. public string tomatoReviews { get; set; }
  157. public string tomatoFresh { get; set; }
  158. public string tomatoRotten { get; set; }
  159. public string tomatoConsensus { get; set; }
  160. public string tomatoUserMeter { get; set; }
  161. public string tomatoUserRating { get; set; }
  162. public string tomatoUserReviews { get; set; }
  163. public string DVD { get; set; }
  164. public string BoxOffice { get; set; }
  165. public string Production { get; set; }
  166. public string Website { get; set; }
  167. public string Response { get; set; }
  168. }
  169. }
  170. }