The Webfeed Flutter package for Appeto SDK.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

147 linhas
4.3 KiB

  1. import 'dart:core';
  2. import 'package:webfeedclient/domain/rss_category.dart';
  3. import 'package:webfeedclient/domain/rss_cloud.dart';
  4. import 'package:webfeedclient/domain/rss_item.dart';
  5. import 'package:webfeedclient/util/helpers.dart';
  6. import 'package:xml/xml.dart';
  7. import 'package:webfeedclient/domain/rss_image.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(XmlDocument document) {
  43. XmlElement channelElement;
  44. try {
  45. channelElement = document.findAllElements("channel").first;
  46. } on StateError {
  47. throw new ArgumentError("channel not found");
  48. }
  49. var title = xmlGetString(channelElement, "title");
  50. var description = xmlGetString(channelElement, "description");
  51. var link = xmlGetString(channelElement, "link");
  52. List<RssItem> feeds = channelElement.findElements("item").map((element) {
  53. return new RssItem.parse(element);
  54. }).toList();
  55. RssImage image;
  56. try {
  57. image = new RssImage.parse(channelElement.findElements("image").first);
  58. } on StateError {}
  59. RssCloud cloud;
  60. try {
  61. cloud = new RssCloud.parse(channelElement.findElements("cloud").first);
  62. } on StateError {}
  63. List<RssCategory> categories = channelElement.findElements("category").map((element) {
  64. return new RssCategory.parse(element);
  65. }).toList();
  66. List<String> skipDays = new List<String>();
  67. var skipDaysNodes = channelElement.findElements("skipDays");
  68. if (skipDaysNodes.isNotEmpty) {
  69. skipDays = skipDaysNodes.first.findAllElements("day").map((element) {
  70. return element.text;
  71. }).toList();
  72. }
  73. List<int> skipHours = new List<int>();
  74. var skipHoursNodes = channelElement.findElements("skipHours");
  75. if (skipHoursNodes.isNotEmpty) {
  76. skipHours = skipHoursNodes.first.findAllElements("hour").map((element) {
  77. try {
  78. return int.parse(element.text);
  79. } on FormatException {
  80. return null;
  81. }
  82. }).toList();
  83. }
  84. var lastBuildDate = xmlGetString(channelElement, "lastBuildDate", strict: false);
  85. var language = xmlGetString(channelElement, "language", strict: false);
  86. var generator = xmlGetString(channelElement, "generator", strict: false);
  87. var copyright = xmlGetString(channelElement, "copyright", strict: false);
  88. var docs = xmlGetString(channelElement, "docs", strict: false);
  89. var managingEditor = xmlGetString(channelElement, "managingEditor", strict: false);
  90. var rating = xmlGetString(channelElement, "rating", strict: false);
  91. var webMaster = xmlGetString(channelElement, "webMaster", strict: false);
  92. var ttl = xmlGetInt(channelElement, "ttl", strict: false);
  93. return new RssFeed(title, description, link, feeds,
  94. image: image,
  95. cloud: cloud,
  96. categories: categories,
  97. skipDays: skipDays,
  98. skipHours: skipHours,
  99. lastBuildDate: lastBuildDate,
  100. language: language,
  101. generator: generator,
  102. copyright: copyright,
  103. docs: docs,
  104. managingEditor: managingEditor,
  105. rating: rating,
  106. webMaster: webMaster,
  107. ttl: ttl);
  108. }
  109. @override
  110. String toString() {
  111. return '''
  112. title: $title
  113. description: $description
  114. link: $link
  115. items: $items
  116. image: $image
  117. cloud: $cloud
  118. categories: $categories
  119. skipDays: $skipDays
  120. skipHours: $skipHours
  121. lastBuildDate: $lastBuildDate
  122. language: $language
  123. generator: $generator
  124. copyright: $copyright
  125. docs: $docs
  126. managingEditor: $managingEditor
  127. rating: $rating
  128. webMaster: $webMaster
  129. ttl: $ttl
  130. ''';
  131. }
  132. }