RssParser.cs 1.2 KB

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