LastfmHelper.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = new DateTime(yearFormed, 1,1);
  38. if (data.tags != null)
  39. {
  40. AddGenres(artist, data.tags);
  41. }
  42. }
  43. public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
  44. {
  45. if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);
  46. var overview = data.wiki != null ? data.wiki.content : null;
  47. if (!string.IsNullOrEmpty(overview))
  48. {
  49. overview = StripHtml(overview);
  50. }
  51. item.Overview = overview;
  52. var release = DateTime.MinValue;
  53. DateTime.TryParse(data.releasedate, out release);
  54. item.PremiereDate = release;
  55. if (data.toptags != null)
  56. {
  57. AddGenres(item, data.toptags);
  58. }
  59. }
  60. private static string StripHtml(string htmlString)
  61. {
  62. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  63. const string pattern = @"<(.|\n)*?>";
  64. return Regex.Replace(htmlString, pattern, string.Empty);
  65. }
  66. private static void AddGenres(BaseItem item, LastfmTags tags)
  67. {
  68. foreach (var tag in tags.tag)
  69. {
  70. item.AddGenre(tag.name);
  71. }
  72. }
  73. }
  74. }