The Webfeed Flutter package for Appeto SDK.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

111 lignes
3.6 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 rss = findFirstElement(document, 'rss');
  58. var rdf = findFirstElement(document, 'rdf:RDF');
  59. if (rss == null && rdf == null) {
  60. throw ArgumentError('not a rss feed');
  61. }
  62. var channelElement = findFirstElement(rss ?? rdf, 'channel');
  63. if (channelElement == null) {
  64. throw ArgumentError('channel not found');
  65. }
  66. return RssFeed(
  67. title: findFirstElement(channelElement, 'title')?.text,
  68. author: findFirstElement(channelElement, 'author')?.text,
  69. description: findFirstElement(channelElement, 'description')?.text,
  70. link: findFirstElement(channelElement, 'link')?.text,
  71. items: (rss != null ? channelElement : rdf)
  72. .findElements('item')
  73. .map((e) => RssItem.parse(e))
  74. .toList(),
  75. image: RssImage.parse(
  76. findFirstElement(rss != null ? channelElement : rdf, 'image')),
  77. cloud: RssCloud.parse(findFirstElement(channelElement, 'cloud')),
  78. categories: channelElement
  79. .findElements('category')
  80. .map((e) => RssCategory.parse(e))
  81. .toList(),
  82. skipDays: findFirstElement(channelElement, 'skipDays')
  83. ?.findAllElements('day')
  84. ?.map((e) => e.text)
  85. ?.toList() ??
  86. [],
  87. skipHours: findFirstElement(channelElement, 'skipHours')
  88. ?.findAllElements('hour')
  89. ?.map((e) => int.tryParse(e.text ?? '0'))
  90. ?.toList() ??
  91. [],
  92. lastBuildDate: findFirstElement(channelElement, 'lastBuildDate')?.text,
  93. language: findFirstElement(channelElement, 'language')?.text,
  94. generator: findFirstElement(channelElement, 'generator')?.text,
  95. copyright: findFirstElement(channelElement, 'copyright')?.text,
  96. docs: findFirstElement(channelElement, 'docs')?.text,
  97. managingEditor: findFirstElement(channelElement, 'managingEditor')?.text,
  98. rating: findFirstElement(channelElement, 'rating')?.text,
  99. webMaster: findFirstElement(channelElement, 'webMaster')?.text,
  100. ttl: int.tryParse(findFirstElement(channelElement, 'ttl')?.text ?? '0'),
  101. dc: DublinCore.parse(channelElement),
  102. itunes: Itunes.parse(channelElement),
  103. );
  104. }
  105. }