The Webfeed Flutter package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 regels
3.6 KiB

  1. import 'package:webfeed/domain/itunes/itunes_category.dart';
  2. import 'package:webfeed/domain/itunes/itunes_episode_type.dart';
  3. import 'package:webfeed/domain/itunes/itunes_image.dart';
  4. import 'package:webfeed/domain/itunes/itunes_owner.dart';
  5. import 'package:webfeed/domain/itunes/itunes_type.dart';
  6. import 'package:webfeed/util/iterable.dart';
  7. import 'package:webfeed/util/xml.dart';
  8. import 'package:xml/xml.dart';
  9. class Itunes {
  10. final String? author;
  11. final String? summary;
  12. final bool? explicit;
  13. final String? title;
  14. final String? subtitle;
  15. final ItunesOwner? owner;
  16. final List<String>? keywords;
  17. final ItunesImage? image;
  18. final List<ItunesCategory>? categories;
  19. final ItunesType? type;
  20. final String? newFeedUrl;
  21. final bool? block;
  22. final bool? complete;
  23. final int? episode;
  24. final int? season;
  25. final Duration? duration;
  26. final ItunesEpisodeType? episodeType;
  27. Itunes({
  28. this.author,
  29. this.summary,
  30. this.explicit,
  31. this.title,
  32. this.subtitle,
  33. this.owner,
  34. this.keywords,
  35. this.image,
  36. this.categories,
  37. this.type,
  38. this.newFeedUrl,
  39. this.block,
  40. this.complete,
  41. this.episode,
  42. this.season,
  43. this.duration,
  44. this.episodeType,
  45. });
  46. factory Itunes.parse(XmlElement element) {
  47. final episodeStr = element.findElements('itunes:episode').firstOrNull?.text;
  48. final seasonStr = element.findElements('itunes:season').firstOrNull?.text;
  49. final durationStr =
  50. element.findElements('itunes:duration').firstOrNull?.text;
  51. return Itunes(
  52. author: element.findElements('itunes:author').firstOrNull?.text,
  53. summary: element.findElements('itunes:summary').firstOrNull?.text,
  54. explicit: parseBoolLiteral(element, 'itunes:explicit'),
  55. title: element.findElements('itunes:title').firstOrNull?.text,
  56. subtitle: element.findElements('itunes:subtitle').firstOrNull?.text,
  57. owner: element
  58. .findElements('itunes:owner')
  59. .map((e) => ItunesOwner.parse(e))
  60. .firstOrNull,
  61. keywords: element
  62. .findElements('itunes:keywords')
  63. .firstOrNull
  64. ?.text
  65. .split(',')
  66. .map((keyword) => keyword.trim())
  67. .toList() ??
  68. [],
  69. image: element
  70. .findElements('itunes:image')
  71. .map((e) => ItunesImage.parse(e))
  72. .firstOrNull,
  73. categories: element
  74. .findElements('itunes:category')
  75. .map((e) => ItunesCategory.parse(e))
  76. .toList(),
  77. type: element
  78. .findElements('itunes:type')
  79. .map((e) => newItunesType(e))
  80. .firstOrNull,
  81. newFeedUrl: element.findElements('itunes:new-feed-url').firstOrNull?.text,
  82. block: parseBoolLiteral(element, 'itunes:block'),
  83. complete: parseBoolLiteral(element, 'itunes:complete'),
  84. episode: episodeStr == null ? null : int.tryParse(episodeStr),
  85. season: seasonStr == null ? null : int.tryParse(seasonStr),
  86. duration: durationStr == null ? null : _parseDuration(durationStr),
  87. episodeType: element
  88. .findElements('itunes:episodeType')
  89. .map((e) => newItunesEpisodeType(e))
  90. .firstOrNull,
  91. );
  92. }
  93. static Duration _parseDuration(String s) {
  94. var hours = 0;
  95. var minutes = 0;
  96. var seconds = 0;
  97. var parts = s.split(':');
  98. if (parts.length > 2) {
  99. hours = int.tryParse(parts[parts.length - 3]) ?? 0;
  100. }
  101. if (parts.length > 1) {
  102. minutes = int.tryParse(parts[parts.length - 2]) ?? 0;
  103. }
  104. seconds = int.tryParse(parts[parts.length - 1]) ?? 0;
  105. return Duration(
  106. hours: hours,
  107. minutes: minutes,
  108. seconds: seconds,
  109. );
  110. }
  111. }