The Webfeed Flutter package for Appeto SDK.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

127 行
4.0 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/domain/syndication/syndication.dart';
  9. import 'package:webfeed/util/iterable.dart';
  10. import 'package:xml/xml.dart';
  11. class RssFeed {
  12. final String? title;
  13. final String? author;
  14. final String? description;
  15. final String? link;
  16. final List<RssItem>? items;
  17. final RssImage? image;
  18. final RssCloud? cloud;
  19. final List<RssCategory>? categories;
  20. final List<String>? skipDays;
  21. final List<int>? skipHours;
  22. final String? lastBuildDate;
  23. final String? language;
  24. final String? generator;
  25. final String? copyright;
  26. final String? docs;
  27. final String? managingEditor;
  28. final String? rating;
  29. final String? webMaster;
  30. final int? ttl;
  31. final DublinCore? dc;
  32. final Itunes? itunes;
  33. final Syndication? syndication;
  34. RssFeed({
  35. this.title,
  36. this.author,
  37. this.description,
  38. this.link,
  39. this.items,
  40. this.image,
  41. this.cloud,
  42. this.categories,
  43. this.skipDays,
  44. this.skipHours,
  45. this.lastBuildDate,
  46. this.language,
  47. this.generator,
  48. this.copyright,
  49. this.docs,
  50. this.managingEditor,
  51. this.rating,
  52. this.webMaster,
  53. this.ttl,
  54. this.dc,
  55. this.itunes,
  56. this.syndication,
  57. });
  58. factory RssFeed.parse(String xmlString) {
  59. var document = XmlDocument.parse(xmlString);
  60. var rss = document.findElements('rss').firstOrNull;
  61. var rdf = document.findElements('rdf:RDF').firstOrNull;
  62. if (rss == null && rdf == null) {
  63. throw ArgumentError('not a rss feed');
  64. }
  65. var channelElement = (rss ?? rdf)!.findElements('channel').firstOrNull;
  66. if (channelElement == null) {
  67. throw ArgumentError('channel not found');
  68. }
  69. return RssFeed(
  70. title: channelElement.findElements('title').firstOrNull?.text,
  71. author: channelElement.findElements('author').firstOrNull?.text,
  72. description: channelElement.findElements('description').firstOrNull?.text,
  73. link: channelElement.findElements('link').firstOrNull?.text,
  74. items: (rdf ?? channelElement)
  75. .findElements('item')
  76. .map((e) => RssItem.parse(e))
  77. .toList(),
  78. image: (rdf ?? channelElement)
  79. .findElements('image')
  80. .map((e) => RssImage.parse(e))
  81. .firstOrNull,
  82. cloud: channelElement
  83. .findElements('cloud')
  84. .map((e) => RssCloud.parse(e))
  85. .firstOrNull,
  86. categories: channelElement
  87. .findElements('category')
  88. .map((e) => RssCategory.parse(e))
  89. .toList(),
  90. skipDays: channelElement
  91. .findElements('skipDays')
  92. .firstOrNull
  93. ?.findAllElements('day')
  94. .map((e) => e.text)
  95. .toList() ??
  96. [],
  97. skipHours: channelElement
  98. .findElements('skipHours')
  99. .firstOrNull
  100. ?.findAllElements('hour')
  101. .map((e) => int.tryParse(e.text) ?? 0)
  102. .toList() ??
  103. [],
  104. lastBuildDate:
  105. channelElement.findElements('lastBuildDate').firstOrNull?.text,
  106. language: channelElement.findElements('language').firstOrNull?.text,
  107. generator: channelElement.findElements('generator').firstOrNull?.text,
  108. copyright: channelElement.findElements('copyright').firstOrNull?.text,
  109. docs: channelElement.findElements('docs').firstOrNull?.text,
  110. managingEditor:
  111. channelElement.findElements('managingEditor').firstOrNull?.text,
  112. rating: channelElement.findElements('rating').firstOrNull?.text,
  113. webMaster: channelElement.findElements('webMaster').firstOrNull?.text,
  114. ttl: int.tryParse(
  115. channelElement.findElements('ttl').firstOrNull?.text ?? '0'),
  116. dc: DublinCore.parse(channelElement),
  117. itunes: Itunes.parse(channelElement),
  118. syndication: Syndication.parse(channelElement),
  119. );
  120. }
  121. }