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.

147 lignes
4.4 KiB

  1. import 'dart:core';
  2. import 'package:webfeed/domain/rss_category.dart';
  3. import 'package:webfeed/domain/rss_cloud.dart';
  4. import 'package:webfeed/domain/rss_image.dart';
  5. import 'package:webfeed/domain/rss_item.dart';
  6. import 'package:webfeed/util/helpers.dart';
  7. import 'package:xml/xml.dart';
  8. class RssFeed {
  9. final String title;
  10. final String description;
  11. final String link;
  12. final List<RssItem> items;
  13. final RssImage image;
  14. final RssCloud cloud;
  15. final List<RssCategory> categories;
  16. final List<String> skipDays;
  17. final List<int> skipHours;
  18. final String lastBuildDate;
  19. final String language;
  20. final String generator;
  21. final String copyright;
  22. final String docs;
  23. final String managingEditor;
  24. final String rating;
  25. final String webMaster;
  26. final int ttl;
  27. RssFeed(this.title, this.description, this.link, this.items,
  28. {this.image,
  29. this.cloud,
  30. this.categories,
  31. this.skipDays,
  32. this.skipHours,
  33. this.lastBuildDate,
  34. this.language,
  35. this.generator,
  36. this.copyright,
  37. this.docs,
  38. this.managingEditor,
  39. this.rating,
  40. this.webMaster,
  41. this.ttl});
  42. factory RssFeed.parse(String xmlString) {
  43. XmlDocument document = parse(xmlString);
  44. XmlElement channelElement;
  45. try {
  46. channelElement = document.findAllElements("channel").first;
  47. } on StateError {
  48. throw new ArgumentError("channel not found");
  49. }
  50. var title = xmlGetString(channelElement, "title");
  51. var description = xmlGetString(channelElement, "description");
  52. var link = xmlGetString(channelElement, "link");
  53. List<RssItem> feeds = channelElement.findElements("item").map((element) {
  54. return new RssItem.parse(element);
  55. }).toList();
  56. RssImage image;
  57. try {
  58. image = new RssImage.parse(channelElement.findElements("image").first);
  59. } on StateError {}
  60. RssCloud cloud;
  61. try {
  62. cloud = new RssCloud.parse(channelElement.findElements("cloud").first);
  63. } on StateError {}
  64. List<RssCategory> categories = channelElement.findElements("category").map((element) {
  65. return new RssCategory.parse(element);
  66. }).toList();
  67. List<String> skipDays = new List<String>();
  68. var skipDaysNodes = channelElement.findElements("skipDays");
  69. if (skipDaysNodes.isNotEmpty) {
  70. skipDays = skipDaysNodes.first.findAllElements("day").map((element) {
  71. return element.text;
  72. }).toList();
  73. }
  74. List<int> skipHours = new List<int>();
  75. var skipHoursNodes = channelElement.findElements("skipHours");
  76. if (skipHoursNodes.isNotEmpty) {
  77. skipHours = skipHoursNodes.first.findAllElements("hour").map((element) {
  78. try {
  79. return int.parse(element.text);
  80. } on FormatException {
  81. return null;
  82. }
  83. }).toList();
  84. }
  85. var lastBuildDate = xmlGetString(channelElement, "lastBuildDate", strict: false);
  86. var language = xmlGetString(channelElement, "language", strict: false);
  87. var generator = xmlGetString(channelElement, "generator", strict: false);
  88. var copyright = xmlGetString(channelElement, "copyright", strict: false);
  89. var docs = xmlGetString(channelElement, "docs", strict: false);
  90. var managingEditor = xmlGetString(channelElement, "managingEditor", strict: false);
  91. var rating = xmlGetString(channelElement, "rating", strict: false);
  92. var webMaster = xmlGetString(channelElement, "webMaster", strict: false);
  93. var ttl = xmlGetInt(channelElement, "ttl", strict: false);
  94. return new RssFeed(title, description, link, feeds,
  95. image: image,
  96. cloud: cloud,
  97. categories: categories,
  98. skipDays: skipDays,
  99. skipHours: skipHours,
  100. lastBuildDate: lastBuildDate,
  101. language: language,
  102. generator: generator,
  103. copyright: copyright,
  104. docs: docs,
  105. managingEditor: managingEditor,
  106. rating: rating,
  107. webMaster: webMaster,
  108. ttl: ttl);
  109. }
  110. @override
  111. String toString() {
  112. return '''
  113. title: $title
  114. description: $description
  115. link: $link
  116. items: $items
  117. image: $image
  118. cloud: $cloud
  119. categories: $categories
  120. skipDays: $skipDays
  121. skipHours: $skipHours
  122. lastBuildDate: $lastBuildDate
  123. language: $language
  124. generator: $generator
  125. copyright: $copyright
  126. docs: $docs
  127. managingEditor: $managingEditor
  128. rating: $rating
  129. webMaster: $webMaster
  130. ttl: $ttl
  131. ''';
  132. }
  133. }