|
| 1 | +import 'package:webfeed/util/helpers.dart'; |
| 2 | +import 'package:xml/xml.dart'; |
| 3 | + |
| 4 | +import 'rss_itunes_category.dart'; |
| 5 | +import 'rss_itunes_image.dart'; |
| 6 | + |
| 7 | +class RssItemItunes { |
| 8 | + final int episode; |
| 9 | + final Duration duration; |
| 10 | + final String episodeType; |
| 11 | + final String author; |
| 12 | + final String summary; |
| 13 | + final bool explicit; |
| 14 | + final String subtitle; |
| 15 | + final List<String> keywords; |
| 16 | + final RssItunesImage image; |
| 17 | + final RssItunesCategory category; |
| 18 | + |
| 19 | + RssItemItunes({ |
| 20 | + this.episode, |
| 21 | + this.duration, |
| 22 | + this.episodeType, |
| 23 | + this.author, |
| 24 | + this.summary, |
| 25 | + this.explicit, |
| 26 | + this.subtitle, |
| 27 | + this.keywords, |
| 28 | + this.image, |
| 29 | + this.category, |
| 30 | + }); |
| 31 | + |
| 32 | + factory RssItemItunes.parse(XmlElement element) { |
| 33 | + if (element == null) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + var explicitStr = |
| 37 | + findElementOrNull(element, "itunes:explicit")?.text?.toLowerCase()?.trim(); |
| 38 | + var episodeStr = findElementOrNull(element, "itunes:episode")?.text?.trim(); |
| 39 | + var durationStr = findElementOrNull(element, "itunes:duration")?.text?.trim(); |
| 40 | + |
| 41 | + return RssItemItunes( |
| 42 | + episode: episodeStr == null ? null : int.parse(episodeStr), |
| 43 | + duration: durationStr == null ? null : parseDuration(durationStr), |
| 44 | + episodeType: findElementOrNull(element, "itunes:episodeType")?.text?.trim(), |
| 45 | + author: findElementOrNull(element, "itunes:author")?.text?.trim(), |
| 46 | + summary: findElementOrNull(element, "itunes:summary")?.text?.trim(), |
| 47 | + explicit: explicitStr == null |
| 48 | + ? null |
| 49 | + : explicitStr == "yes" || explicitStr == "true", |
| 50 | + subtitle: findElementOrNull(element, "itunes:subtitle")?.text?.trim(), |
| 51 | + keywords: findElementOrNull(element, "itunes:keywords")?.text?.split(",")?.map((keyword) => keyword.trim())?.toList(), |
| 52 | + image: RssItunesImage.parse(findElementOrNull(element, "itunes:image")), |
| 53 | + category: RssItunesCategory.parse( |
| 54 | + findElementOrNull(element, "itunes:category")), |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +Duration parseDuration(String s) { |
| 60 | + var hours = 0; |
| 61 | + var minutes = 0; |
| 62 | + var seconds = 0; |
| 63 | + var parts = s.split(':'); |
| 64 | + if (parts.length > 2) { |
| 65 | + hours = int.parse(parts[parts.length - 3]); |
| 66 | + } |
| 67 | + if (parts.length > 1) { |
| 68 | + minutes = int.parse(parts[parts.length - 2]); |
| 69 | + } |
| 70 | + seconds = int.parse(parts[parts.length - 1]); |
| 71 | + return Duration( |
| 72 | + hours: hours, |
| 73 | + minutes: minutes, |
| 74 | + seconds: seconds, |
| 75 | + ); |
| 76 | +} |
0 commit comments