LastfmHelper.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. var overview = data.bio != null ? data.bio.content : null;
  18. if (!string.IsNullOrEmpty(overview))
  19. {
  20. overview = StripHtml(overview);
  21. }
  22. artist.Overview = overview;
  23. var yearFormed = 0;
  24. try
  25. {
  26. yearFormed = Convert.ToInt32(data.bio.yearformed);
  27. }
  28. catch (FormatException)
  29. {
  30. }
  31. catch (NullReferenceException)
  32. {
  33. }
  34. catch (OverflowException)
  35. {
  36. }
  37. artist.PremiereDate = yearFormed > 0 ? new DateTime(yearFormed, 1,1) : DateTime.MinValue;
  38. artist.ProductionYear = yearFormed;
  39. if (data.tags != null)
  40. {
  41. AddGenres(artist, data.tags);
  42. }
  43. }
  44. public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
  45. {
  46. if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);
  47. var overview = data.wiki != null ? data.wiki.content : null;
  48. if (!string.IsNullOrEmpty(overview))
  49. {
  50. overview = StripHtml(overview);
  51. }
  52. item.Overview = overview;
  53. var release = DateTime.MinValue;
  54. DateTime.TryParse(data.releasedate, out release);
  55. item.PremiereDate = release;
  56. item.ProductionYear = release.Year;
  57. if (data.toptags != null)
  58. {
  59. AddGenres(item, data.toptags);
  60. }
  61. }
  62. private static string StripHtml(string htmlString)
  63. {
  64. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  65. const string pattern = @"<(.|\n)*?>";
  66. return Regex.Replace(htmlString, pattern, string.Empty);
  67. }
  68. private static void AddGenres(BaseItem item, LastfmTags tags)
  69. {
  70. foreach (var tag in tags.tag)
  71. {
  72. item.AddGenre(tag.name);
  73. }
  74. }
  75. }
  76. }