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.

118 rivejä
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 =
  48. element.findElements('itunes:episode').firstOrNull?.text ?? '';
  49. final seasonStr =
  50. element.findElements('itunes:season').firstOrNull?.text ?? '';
  51. final durationStr =
  52. element.findElements('itunes:duration').firstOrNull?.text ?? '';
  53. return Itunes(
  54. author: element.findElements('itunes:author').firstOrNull?.text,
  55. summary: element.findElements('itunes:summary').firstOrNull?.text,
  56. explicit: parseBoolLiteral(element, 'itunes:explicit'),
  57. title: element.findElements('itunes:title').firstOrNull?.text,
  58. subtitle: element.findElements('itunes:subtitle').firstOrNull?.text,
  59. owner: element
  60. .findElements('itunes:owner')
  61. .map((e) => ItunesOwner.parse(e))
  62. .firstOrNull,
  63. keywords: element
  64. .findElements('itunes:keywords')
  65. .firstOrNull
  66. ?.text
  67. .split(',')
  68. .map((keyword) => keyword.trim())
  69. .toList() ??
  70. [],
  71. image: element
  72. .findElements('itunes:image')
  73. .map((e) => ItunesImage.parse(e))
  74. .firstOrNull,
  75. categories: element
  76. .findElements('itunes:category')
  77. .map((e) => ItunesCategory.parse(e))
  78. .toList(),
  79. type: element
  80. .findElements('itunes:type')
  81. .map((e) => newItunesType(e))
  82. .firstOrNull,
  83. newFeedUrl: element.findElements('itunes:new-feed-url').firstOrNull?.text,
  84. block: parseBoolLiteral(element, 'itunes:block'),
  85. complete: parseBoolLiteral(element, 'itunes:complete'),
  86. episode: episodeStr.isNotEmpty ? int.tryParse(episodeStr) : null,
  87. season: seasonStr.isNotEmpty ? int.tryParse(seasonStr) : null,
  88. duration: durationStr.isNotEmpty ? _parseDuration(durationStr) : null,
  89. episodeType: element
  90. .findElements('itunes:episodeType')
  91. .map((e) => newItunesEpisodeType(e))
  92. .firstOrNull,
  93. );
  94. }
  95. static Duration _parseDuration(String s) {
  96. var hours = 0;
  97. var minutes = 0;
  98. var seconds = 0;
  99. var parts = s.split(':');
  100. if (parts.length > 2) {
  101. hours = int.tryParse(parts[parts.length - 3]) ?? 0;
  102. }
  103. if (parts.length > 1) {
  104. minutes = int.tryParse(parts[parts.length - 2]) ?? 0;
  105. }
  106. seconds = int.tryParse(parts[parts.length - 1]) ?? 0;
  107. return Duration(
  108. hours: hours,
  109. minutes: minutes,
  110. seconds: seconds,
  111. );
  112. }
  113. }