tsacdop-podcast-app-android/lib/service/ompl_build.dart

87 lines
2.5 KiB
Dart
Raw Normal View History

2020-07-13 09:41:59 +02:00
import 'dart:io';
2020-07-12 14:54:14 +02:00
import 'package:xml/xml.dart' as xml;
import '../state/podcast_group.dart';
2020-07-13 09:41:59 +02:00
class OmplOutline {
final String text;
final String xmlUrl;
OmplOutline({this.text, this.xmlUrl});
factory OmplOutline.parse(xml.XmlElement element) {
if (element == null) return null;
return OmplOutline(
text: element.getAttribute("text")?.trim(),
xmlUrl: element.getAttribute("xmlUrl")?.trim(),
);
}
}
2020-07-12 14:54:14 +02:00
class PodcastsBackup {
///Group list for backup.
final List<PodcastGroup> groups;
PodcastsBackup(this.groups) : assert(groups.length > 0);
2020-07-13 09:41:59 +02:00
omplBuilder() {
2020-07-12 14:54:14 +02:00
var builder = xml.XmlBuilder();
builder.processing('xml', 'version="1.0" encoding="UTF-8"');
builder.element('ompl', nest: () {
builder.attribute('version', '1.0');
builder.element('head', nest: () {
builder.element('title', nest: 'Tsacdop Feed Groups');
2020-07-12 14:54:14 +02:00
});
builder.element('body', nest: () {
2020-07-13 09:41:59 +02:00
for (var group in groups) {
builder.element('outline', nest: () {
2020-07-12 14:54:14 +02:00
builder.attribute('text', '${group.name}');
builder.attribute('title', '${group.name}');
2020-07-26 12:20:42 +02:00
for (var e in group.podcasts) {
2020-07-13 09:41:59 +02:00
builder.element(
'outline',
nest: () {
builder.attribute('type', 'rss');
builder.attribute('text', '${e.title}');
builder.attribute('title', '${e.title}');
builder.attribute('xmlUrl', '${e.rssUrl}');
},
isSelfClosing: true,
);
2020-07-26 12:20:42 +02:00
}
2020-07-12 14:54:14 +02:00
});
2020-07-13 09:41:59 +02:00
}
2020-07-12 14:54:14 +02:00
});
});
return builder.build();
}
2020-07-13 09:41:59 +02:00
static parseOMPL(File file) {
2020-07-26 12:20:42 +02:00
var data = <String, List<OmplOutline>>{};
var opml = file.readAsStringSync();
2020-07-13 09:41:59 +02:00
var content = xml.XmlDocument.parse(opml);
2020-07-26 12:20:42 +02:00
var title =
2020-07-13 09:41:59 +02:00
content.findAllElements('head').first.findElements('title').first.text;
print(title);
2020-07-13 09:41:59 +02:00
var groups = content.findAllElements('body').first.findElements('outline');
if (title != 'Tsacdop Feed Groups') {
2020-07-26 12:20:42 +02:00
var total = content
2020-07-13 09:41:59 +02:00
.findAllElements('outline')
.map((ele) => OmplOutline.parse(ele))
.toList();
2020-07-13 09:41:59 +02:00
data['Home'] = total;
print(data.toString());
2020-07-13 09:41:59 +02:00
return data;
}
for (var element in groups) {
2020-07-26 12:20:42 +02:00
var title = element.getAttribute('title');
2020-07-13 09:41:59 +02:00
var total = element
.findElements('outline')
.map((ele) => OmplOutline.parse(ele))
.toList();
2020-07-13 09:41:59 +02:00
data[title] = total;
}
print(data.toString());
2020-07-13 09:41:59 +02:00
return data;
}
2020-07-12 14:54:14 +02:00
}