The Webfeed Flutter package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 line
3.3 KiB

  1. import 'dart:core';
  2. import 'package:webfeed/domain/dublin_core/dublin_core.dart';
  3. import 'package:webfeed/domain/itunes/itunes.dart';
  4. import 'package:webfeed/domain/rss_category.dart';
  5. import 'package:webfeed/domain/rss_cloud.dart';
  6. import 'package:webfeed/domain/rss_image.dart';
  7. import 'package:webfeed/domain/rss_item.dart';
  8. import 'package:webfeed/util/xml.dart';
  9. import 'package:xml/xml.dart';
  10. class RssFeed {
  11. final String title;
  12. final String author;
  13. final String description;
  14. final String link;
  15. final List<RssItem> items;
  16. final RssImage image;
  17. final RssCloud cloud;
  18. final List<RssCategory> categories;
  19. final List<String> skipDays;
  20. final List<int> skipHours;
  21. final String lastBuildDate;
  22. final String language;
  23. final String generator;
  24. final String copyright;
  25. final String docs;
  26. final String managingEditor;
  27. final String rating;
  28. final String webMaster;
  29. final int ttl;
  30. final DublinCore dc;
  31. final Itunes itunes;
  32. RssFeed({
  33. this.title,
  34. this.author,
  35. this.description,
  36. this.link,
  37. this.items,
  38. this.image,
  39. this.cloud,
  40. this.categories,
  41. this.skipDays,
  42. this.skipHours,
  43. this.lastBuildDate,
  44. this.language,
  45. this.generator,
  46. this.copyright,
  47. this.docs,
  48. this.managingEditor,
  49. this.rating,
  50. this.webMaster,
  51. this.ttl,
  52. this.dc,
  53. this.itunes,
  54. });
  55. factory RssFeed.parse(String xmlString) {
  56. var document = XmlDocument.parse(xmlString);
  57. var channelElement = findFirstElement(document, 'channel', recursive: true);
  58. if (channelElement == null) {
  59. throw ArgumentError('channel not found');
  60. }
  61. return RssFeed(
  62. title: findFirstElement(channelElement, 'title')?.text,
  63. author: findFirstElement(channelElement, 'author')?.text,
  64. description: findFirstElement(channelElement, 'description')?.text,
  65. link: findFirstElement(channelElement, 'link')?.text,
  66. items: channelElement
  67. .findElements('item')
  68. .map((e) => RssItem.parse(e))
  69. .toList(),
  70. image: RssImage.parse(findFirstElement(channelElement, 'image')),
  71. cloud: RssCloud.parse(findFirstElement(channelElement, 'cloud')),
  72. categories: channelElement
  73. .findElements('category')
  74. .map((e) => RssCategory.parse(e))
  75. .toList(),
  76. skipDays: findFirstElement(channelElement, 'skipDays')
  77. ?.findAllElements('day')
  78. ?.map((e) => e.text)
  79. ?.toList() ??
  80. [],
  81. skipHours: findFirstElement(channelElement, 'skipHours')
  82. ?.findAllElements('hour')
  83. ?.map((e) => int.tryParse(e.text ?? '0'))
  84. ?.toList() ??
  85. [],
  86. lastBuildDate: findFirstElement(channelElement, 'lastBuildDate')?.text,
  87. language: findFirstElement(channelElement, 'language')?.text,
  88. generator: findFirstElement(channelElement, 'generator')?.text,
  89. copyright: findFirstElement(channelElement, 'copyright')?.text,
  90. docs: findFirstElement(channelElement, 'docs')?.text,
  91. managingEditor: findFirstElement(channelElement, 'managingEditor')?.text,
  92. rating: findFirstElement(channelElement, 'rating')?.text,
  93. webMaster: findFirstElement(channelElement, 'webMaster')?.text,
  94. ttl: int.tryParse(findFirstElement(channelElement, 'ttl')?.text ?? '0'),
  95. dc: DublinCore.parse(channelElement),
  96. itunes: Itunes.parse(channelElement),
  97. );
  98. }
  99. }