The Webfeed Flutter package for Appeto SDK.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

150 рядки
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. var 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 =
  52. xmlGetString(channelElement, "description", strict: false);
  53. var link = xmlGetString(channelElement, "link", strict: false);
  54. var feeds = channelElement.findElements("item").map((element) {
  55. return new RssItem.parse(element);
  56. }).toList();
  57. RssImage image;
  58. try {
  59. image = new RssImage.parse(channelElement.findElements("image").first);
  60. } on StateError {}
  61. RssCloud cloud;
  62. try {
  63. cloud = new RssCloud.parse(channelElement.findElements("cloud").first);
  64. } on StateError {}
  65. var categories = channelElement.findElements("category").map((element) {
  66. return new RssCategory.parse(element);
  67. }).toList();
  68. var skipDays = new List<String>();
  69. var skipDaysNodes = channelElement.findElements("skipDays");
  70. if (skipDaysNodes.isNotEmpty) {
  71. skipDays = skipDaysNodes.first.findAllElements("day").map((element) {
  72. return element.text;
  73. }).toList();
  74. }
  75. var skipHours = new List<int>();
  76. var skipHoursNodes = channelElement.findElements("skipHours");
  77. if (skipHoursNodes.isNotEmpty) {
  78. skipHours = skipHoursNodes.first.findAllElements("hour").map((element) {
  79. try {
  80. return int.parse(element.text);
  81. } on FormatException {
  82. return null;
  83. }
  84. }).toList();
  85. }
  86. var lastBuildDate =
  87. xmlGetString(channelElement, "lastBuildDate", strict: false);
  88. var language = xmlGetString(channelElement, "language", strict: false);
  89. var generator = xmlGetString(channelElement, "generator", strict: false);
  90. var copyright = xmlGetString(channelElement, "copyright", strict: false);
  91. var docs = xmlGetString(channelElement, "docs", strict: false);
  92. var managingEditor =
  93. xmlGetString(channelElement, "managingEditor", strict: false);
  94. var rating = xmlGetString(channelElement, "rating", strict: false);
  95. var webMaster = xmlGetString(channelElement, "webMaster", strict: false);
  96. var ttl = xmlGetInt(channelElement, "ttl", strict: false);
  97. return new RssFeed(title, description, link, feeds,
  98. image: image,
  99. cloud: cloud,
  100. categories: categories,
  101. skipDays: skipDays,
  102. skipHours: skipHours,
  103. lastBuildDate: lastBuildDate,
  104. language: language,
  105. generator: generator,
  106. copyright: copyright,
  107. docs: docs,
  108. managingEditor: managingEditor,
  109. rating: rating,
  110. webMaster: webMaster,
  111. ttl: ttl);
  112. }
  113. @override
  114. String toString() {
  115. return '''
  116. title: $title
  117. description: $description
  118. link: $link
  119. items: $items
  120. image: $image
  121. cloud: $cloud
  122. categories: $categories
  123. skipDays: $skipDays
  124. skipHours: $skipHours
  125. lastBuildDate: $lastBuildDate
  126. language: $language
  127. generator: $generator
  128. copyright: $copyright
  129. docs: $docs
  130. managingEditor: $managingEditor
  131. rating: $rating
  132. webMaster: $webMaster
  133. ttl: $ttl
  134. ''';
  135. }
  136. }