The Webfeed Flutter package for Appeto SDK.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

160 行
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({
  28. this.title,
  29. this.description,
  30. this.link,
  31. this.items,
  32. this.image,
  33. this.cloud,
  34. this.categories,
  35. this.skipDays,
  36. this.skipHours,
  37. this.lastBuildDate,
  38. this.language,
  39. this.generator,
  40. this.copyright,
  41. this.docs,
  42. this.managingEditor,
  43. this.rating,
  44. this.webMaster,
  45. this.ttl,
  46. });
  47. factory RssFeed.parse(String xmlString) {
  48. var document = parse(xmlString);
  49. XmlElement channelElement;
  50. try {
  51. channelElement = document.findAllElements("channel").first;
  52. } on StateError {
  53. throw new ArgumentError("channel not found");
  54. }
  55. var title = xmlGetString(channelElement, "title", strict: false);
  56. var description =
  57. xmlGetString(channelElement, "description", strict: false);
  58. var link = xmlGetString(channelElement, "link", strict: false);
  59. var feeds = channelElement.findElements("item").map((element) {
  60. return new RssItem.parse(element);
  61. }).toList();
  62. RssImage image;
  63. try {
  64. image = new RssImage.parse(channelElement.findElements("image").first);
  65. } on StateError {}
  66. RssCloud cloud;
  67. try {
  68. cloud = new RssCloud.parse(channelElement.findElements("cloud").first);
  69. } on StateError {}
  70. var categories = channelElement.findElements("category").map((element) {
  71. return new RssCategory.parse(element);
  72. }).toList();
  73. var skipDays = new List<String>();
  74. var skipDaysNodes = channelElement.findElements("skipDays");
  75. if (skipDaysNodes.isNotEmpty) {
  76. skipDays = skipDaysNodes.first.findAllElements("day").map((element) {
  77. return element.text;
  78. }).toList();
  79. }
  80. var skipHours = new List<int>();
  81. var skipHoursNodes = channelElement.findElements("skipHours");
  82. if (skipHoursNodes.isNotEmpty) {
  83. skipHours = skipHoursNodes.first.findAllElements("hour").map((element) {
  84. try {
  85. return int.parse(element.text);
  86. } on FormatException {
  87. return null;
  88. }
  89. }).toList();
  90. }
  91. var lastBuildDate =
  92. xmlGetString(channelElement, "lastBuildDate", strict: false);
  93. var language = xmlGetString(channelElement, "language", strict: false);
  94. var generator = xmlGetString(channelElement, "generator", strict: false);
  95. var copyright = xmlGetString(channelElement, "copyright", strict: false);
  96. var docs = xmlGetString(channelElement, "docs", strict: false);
  97. var managingEditor =
  98. xmlGetString(channelElement, "managingEditor", strict: false);
  99. var rating = xmlGetString(channelElement, "rating", strict: false);
  100. var webMaster = xmlGetString(channelElement, "webMaster", strict: false);
  101. var ttl = xmlGetInt(channelElement, "ttl", strict: false);
  102. return new RssFeed(
  103. title: title,
  104. description: description,
  105. link: link,
  106. items: feeds,
  107. image: image,
  108. cloud: cloud,
  109. categories: categories,
  110. skipDays: skipDays,
  111. skipHours: skipHours,
  112. lastBuildDate: lastBuildDate,
  113. language: language,
  114. generator: generator,
  115. copyright: copyright,
  116. docs: docs,
  117. managingEditor: managingEditor,
  118. rating: rating,
  119. webMaster: webMaster,
  120. ttl: ttl,
  121. );
  122. }
  123. @override
  124. String toString() {
  125. return '''
  126. title: $title
  127. description: $description
  128. link: $link
  129. items: $items
  130. image: $image
  131. cloud: $cloud
  132. categories: $categories
  133. skipDays: $skipDays
  134. skipHours: $skipHours
  135. lastBuildDate: $lastBuildDate
  136. language: $language
  137. generator: $generator
  138. copyright: $copyright
  139. docs: $docs
  140. managingEditor: $managingEditor
  141. rating: $rating
  142. webMaster: $webMaster
  143. ttl: $ttl
  144. ''';
  145. }
  146. }