The Webfeed Flutter package for Appeto SDK.
Você não pode selecionar mais de 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.

115 linhas
3.7 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/xml.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 = findFirstElement(document, 'rss');
  61. var rdf = findFirstElement(document, 'rdf:RDF');
  62. if (rss == null && rdf == null) {
  63. throw ArgumentError('not a rss feed');
  64. }
  65. var channelElement = findFirstElement(rss ?? rdf, 'channel');
  66. if (channelElement == null) {
  67. throw ArgumentError('channel not found');
  68. }
  69. return RssFeed(
  70. title: findFirstElement(channelElement, 'title')?.text,
  71. author: findFirstElement(channelElement, 'author')?.text,
  72. description: findFirstElement(channelElement, 'description')?.text,
  73. link: findFirstElement(channelElement, 'link')?.text,
  74. items: (rss != null ? channelElement : rdf)!
  75. .findElements('item')
  76. .map((e) => RssItem.parse(e))
  77. .toList(),
  78. image: RssImage.parse(
  79. findFirstElement(rss != null ? channelElement : rdf, 'image')),
  80. cloud: RssCloud.parse(findFirstElement(channelElement, 'cloud')),
  81. categories: channelElement
  82. .findElements('category')
  83. .map((e) => RssCategory.parse(e))
  84. .toList(),
  85. skipDays: findFirstElement(channelElement, 'skipDays')
  86. ?.findAllElements('day')
  87. .map((e) => e.text)
  88. .toList() ??
  89. [],
  90. skipHours: findFirstElement(channelElement, 'skipHours')
  91. ?.findAllElements('hour')
  92. .map((e) => int.tryParse(e.text))
  93. .toList() ??
  94. [],
  95. lastBuildDate: findFirstElement(channelElement, 'lastBuildDate')?.text,
  96. language: findFirstElement(channelElement, 'language')?.text,
  97. generator: findFirstElement(channelElement, 'generator')?.text,
  98. copyright: findFirstElement(channelElement, 'copyright')?.text,
  99. docs: findFirstElement(channelElement, 'docs')?.text,
  100. managingEditor: findFirstElement(channelElement, 'managingEditor')?.text,
  101. rating: findFirstElement(channelElement, 'rating')?.text,
  102. webMaster: findFirstElement(channelElement, 'webMaster')?.text,
  103. ttl: int.tryParse(findFirstElement(channelElement, 'ttl')?.text ?? '0'),
  104. dc: DublinCore.parse(channelElement),
  105. itunes: Itunes.parse(channelElement),
  106. syndication: Syndication.parse(channelElement),
  107. );
  108. }
  109. }