LastfmHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Model.Entities;
  9. namespace MediaBrowser.Controller.Providers.Music
  10. {
  11. public static class LastfmHelper
  12. {
  13. public static string LocalArtistMetaFileName = "MBArtist.json";
  14. public static string LocalAlbumMetaFileName = "MBAlbum.json";
  15. public static void ProcessArtistData(BaseItem artist, LastfmArtist data)
  16. {
  17. artist.Overview = data.bio != null ? data.bio.content : null;
  18. var yearFormed = 0;
  19. try
  20. {
  21. yearFormed = Convert.ToInt32(data.bio.yearformed);
  22. }
  23. catch (FormatException)
  24. {
  25. }
  26. catch (NullReferenceException)
  27. {
  28. }
  29. catch (OverflowException)
  30. {
  31. }
  32. artist.PremiereDate = new DateTime(yearFormed, 1,1);
  33. if (data.tags != null)
  34. {
  35. AddGenres(artist, data.tags);
  36. }
  37. }
  38. public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
  39. {
  40. if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);
  41. var overview = data.wiki != null ? data.wiki.content : null;
  42. if (!string.IsNullOrEmpty(overview))
  43. {
  44. overview = StripHtml(overview);
  45. }
  46. item.Overview = overview;
  47. var release = DateTime.MinValue;
  48. DateTime.TryParse(data.releasedate, out release);
  49. item.PremiereDate = release;
  50. if (data.toptags != null)
  51. {
  52. AddGenres(item, data.toptags);
  53. }
  54. }
  55. private static string StripHtml(string htmlString)
  56. {
  57. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  58. const string pattern = @"<(.|\n)*?>";
  59. return Regex.Replace(htmlString, pattern, string.Empty);
  60. }
  61. private static void AddGenres(BaseItem item, LastfmTags tags)
  62. {
  63. foreach (var tag in tags.tag)
  64. {
  65. item.AddGenre(tag.name);
  66. }
  67. }
  68. }
  69. }