RssParser.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BanTur.Core.Entity;
  6. using CodeHollow.FeedReader;
  7. using CodeHollow.FeedReader.Feeds;
  8. namespace BanTur.Core.Rss
  9. {
  10. public static class RssParser
  11. {
  12. public static BangumiEntry[] Parse(string xml)
  13. {
  14. var doc = FeedReader.ReadFromString(xml);
  15. var isFeed20 = doc.Type == FeedType.Rss_2_0;
  16. var list = new List<BangumiEntry>();
  17. foreach (var item in doc.Items)
  18. {
  19. string magnet = null;
  20. if (isFeed20)
  21. {
  22. var rss20 = (Rss20FeedItem)item.SpecificItem;
  23. if (rss20.Enclosure?.MediaType == "application/x-bittorrent") magnet = rss20.Enclosure.Url;
  24. }
  25. if (magnet == null) continue;
  26. var entry = new BangumiEntry
  27. {
  28. FetchDate = DateTime.Now,
  29. PubDate = item.PublishingDate,
  30. Magnet = magnet,
  31. Title = item.Title,
  32. Url = item.Link,
  33. };
  34. list.Add(entry);
  35. }
  36. return list.ToArray();
  37. }
  38. }
  39. }