MusicArtistProviderFromJson.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Model.Serialization;
  14. namespace MediaBrowser.Controller.Providers.Music
  15. {
  16. public class MusicArtistProviderFromJson : BaseMetadataProvider
  17. {
  18. /// <summary>
  19. /// Gets the json serializer.
  20. /// </summary>
  21. /// <value>The json serializer.</value>
  22. protected IJsonSerializer JsonSerializer { get; private set; }
  23. public MusicArtistProviderFromJson(IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager)
  24. : base(logManager, configurationManager)
  25. {
  26. if (jsonSerializer == null)
  27. {
  28. throw new ArgumentNullException("jsonSerializer");
  29. }
  30. JsonSerializer = jsonSerializer;
  31. }
  32. protected override Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  33. {
  34. return Task.Run(() =>
  35. {
  36. cancellationToken.ThrowIfCancellationRequested();
  37. var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LastfmHelper.LocalArtistMetaFileName));
  38. if (entry.HasValue)
  39. {
  40. // read in our saved meta and pass to processing function
  41. var data = JsonSerializer.DeserializeFromFile<LastfmArtist>(entry.Value.Path);
  42. cancellationToken.ThrowIfCancellationRequested();
  43. LastfmHelper.ProcessArtistData(item, data);
  44. SetLastRefreshed(item, DateTime.UtcNow);
  45. return true;
  46. }
  47. return false;
  48. });
  49. }
  50. public override MetadataProviderPriority Priority
  51. {
  52. get
  53. {
  54. return MetadataProviderPriority.First;
  55. }
  56. }
  57. public override bool Supports(BaseItem item)
  58. {
  59. return item is MusicArtist;
  60. }
  61. public override bool RequiresInternet
  62. {
  63. get
  64. {
  65. return false;
  66. }
  67. }
  68. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  69. {
  70. if (!item.ResolveArgs.ContainsMetaFileByName(LastfmHelper.LocalArtistMetaFileName))
  71. {
  72. return false; // nothing to read
  73. }
  74. // Need to re-override to jump over intermediate implementation
  75. return CompareDate(item) > providerInfo.LastRefreshed;
  76. }
  77. /// <summary>
  78. /// Override this to return the date that should be compared to the last refresh date
  79. /// to determine if this provider should be re-fetched.
  80. /// </summary>
  81. /// <param name="item">The item.</param>
  82. /// <returns>DateTime.</returns>
  83. protected override DateTime CompareDate(BaseItem item)
  84. {
  85. var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LastfmHelper.LocalArtistMetaFileName));
  86. return entry != null ? entry.Value.LastWriteTimeUtc : DateTime.MinValue;
  87. }
  88. }
  89. }