Skip to content

Commit

Permalink
fix marco; update libs
Browse files Browse the repository at this point in the history
  • Loading branch information
RedyAu committed May 15, 2024
1 parent d63c7d6 commit 64a21e0
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 122 deletions.
144 changes: 74 additions & 70 deletions bin/scrapecast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,79 +22,80 @@ void main() async {
}

Future buildPodcast(Podcast podcast) async {
print("\n\nBuilding ${podcast.properties.title}\n");
try {
print("\n\nBuilding ${podcast.properties.title}\n");

List<Episode> episodes = [];
List<Episode> errors = [];
List<Episode> episodes = [];
List<Episode> errors = [];

print('Loading existing data from json');
if (!podcast.dataFile.existsSync()) {
podcast.dataFile.createSync();
podcast.dataFile.writeAsStringSync("[]");
}
List jsonEntries = jsonDecode(podcast.dataFile.readAsStringSync());
for (Map entry in jsonEntries) {
episodes.add(podcast.fromJson(entry));
}

List<Episode> newIts = (await podcast.scraper())
.where((element) => (!episodes.contains(element)))
.toList();
episodes.insertAll(0, newIts);
episodes.sort((a, b) => a.date.isBefore(b.date) ? 1 : -1);

save() {
print("Saving database");
podcast.dataFile.createSync();
podcast.dataFile.writeAsStringSync(JsonEncoder.withIndent(' ')
.convert(episodes.map((e) => podcast.toJson(e)).toList()));
}
print('Loading existing data from json');
if (!podcast.dataFile.existsSync()) {
podcast.dataFile.createSync();
podcast.dataFile.writeAsStringSync("[]");
}
List jsonEntries = jsonDecode(podcast.dataFile.readAsStringSync());
for (Map entry in jsonEntries) {
episodes.add(podcast.fromJson(entry));
}

int i = 0;
for (Episode item in episodes) {
if (item.length == null || item.fileSize == null) {
Uri downloadUri = Uri.parse(item.download);

item.length ??= await getLength(
(downloadUri.host.isEmpty ? podcast.properties.baseUrl : "") +
item.download);
item.fileSize ??= await getSize(
(downloadUri.host.isEmpty ? podcast.properties.baseUrl : "") +
item.download);
print(
"Properties of ${item.date} | ${item.title} is\n Length: ${item.length ?? "!!! NULL"} seconds\n Size: ${item.fileSize ?? "!!! NULL"} bytes");
i++;
if (i > 30) {
print("\n\nThrottling and saving progress...\n\n");
await Future.delayed(Duration(seconds: 5));
i = 0;
save();
}
List<Episode> newIts = (await podcast.scraper())
.where((element) => (!episodes.contains(element)))
.toList();
episodes.insertAll(0, newIts);
episodes.sort((a, b) => a.date.isBefore(b.date) ? 1 : -1);

save() {
print("Saving database");
podcast.dataFile.createSync();
podcast.dataFile.writeAsStringSync(JsonEncoder.withIndent(' ')
.convert(episodes.map((e) => podcast.toJson(e)).toList()));
}

if (item.length == null) {
//! If length request failed
print("##> Error while getting length!\n");
errors.add(item);
int i = 0;
for (Episode item in episodes) {
if (item.length == null || item.fileSize == null) {
Uri downloadUri = Uri.parse(item.download);

item.length ??= await getLength(
(downloadUri.host.isEmpty ? podcast.properties.baseUrl : "") +
item.download);
item.fileSize ??= await getSize(
(downloadUri.host.isEmpty ? podcast.properties.baseUrl : "") +
item.download);
print(
"Properties of ${item.date} | ${item.title} is\n Length: ${item.length ?? "!!! NULL"} seconds\n Size: ${item.fileSize ?? "!!! NULL"} bytes");
i++;
if (i > 30) {
print("\n\nThrottling and saving progress...\n\n");
await Future.delayed(Duration(seconds: 5));
i = 0;
save();
}

if (item.length == null) {
//! If length request failed
print("##> Error while getting length!\n");
errors.add(item);
}
}
}
}

episodes.removeWhere((element) => errors.contains(element));
episodes.removeWhere((element) => errors.contains(element));

save();
save();

print("Building rss feed");
podcast.rssFile.createSync();
podcast.rssFile.writeAsStringSync(getFeed(episodes, podcast));
print("Building rss feed");
podcast.rssFile.createSync();
podcast.rssFile.writeAsStringSync(getFeed(episodes, podcast));

print(errors.isNotEmpty ? 'Errors:' : '');
for (Episode item in errors) {
print(
"${item.uuid} | ${item.date} | ${item.title} | ${item.download} | ${item.length}");
}
print(errors.isNotEmpty ? 'Errors:' : '');
for (Episode item in errors) {
print(
"${item.uuid} | ${item.date} | ${item.title} | ${item.download} | ${item.length}");
}

statusMdString +=
"""## [${podcast.properties.title}](${podcast.properties.link})
statusMdString +=
"""## [${podcast.properties.title}](${podcast.properties.link})
_${podcast.properties.description.replaceAll("\n", "\\\n")}_
✅ Legutóbb frissítve: ${getRfcDate(DateTime.now())}
Expand All @@ -103,14 +104,17 @@ Epizódok száma: ${episodes.length}
**Elérhető:**
""";
for (String key in podcast.links.keys) {
statusMdString += " - [$key](${podcast.links[key]})\n";
}
for (String key in podcast.links.keys) {
statusMdString += " - [$key](${podcast.links[key]})\n";
}

statusMdString += "\n**Legutóbbi epizódok:**\n";
for (Episode episode in episodes.sublist(0, 6)) {
statusMdString +=
" - ${dateFormat.format(episode.date)} - ${episode.title} - ${episode.author}\n";
statusMdString += "\n**Legutóbbi epizódok:**\n";
for (Episode episode in episodes.sublist(0, 6)) {
statusMdString +=
" - ${dateFormat.format(episode.date)} - ${episode.title} - ${episode.author}\n";
}
statusMdString += "\n---\n\n";
} catch (e) {
print("Error while building ${podcast.properties.title}!\n$e");
}
statusMdString += "\n---\n\n";
}
12 changes: 9 additions & 3 deletions bin/specifics/marco.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Map<String, String?> marcoPages = {
"Bizonyságtétel": "https://marko.reformatus.hu/Biztetel.shtml",
"Evangelizáció": "https://marko.reformatus.hu/Evang.shtml",
"Ismeretterjesztés": "https://marko.reformatus.hu/Ismeretterjesztes.shtml",
"Mi Atyánk": "https://marko.reformatus.hu/MiAtyank.shtml",
"Mi Atyánk": "https://marko.reformatus.hu/MiAtyank.shtml",
}
};
/*
Expand All @@ -203,11 +203,18 @@ Future<List<Episode>> marcoScraper() async {
);
var document = parse(resp.body);

var rows = [];

try {

var rows = document
.querySelectorAll("##object > table > tbody > tr")
.where((element) =>
((element.nodes.first.nodes.first as Element).localName == "a"))
.toList();
} catch (e) {
print("Error while parsing page: $e");
}

String errorsOnPage = "";

Expand Down Expand Up @@ -356,8 +363,7 @@ String marcoDescriptionBuilder(Podcast podcast, Episode element) {
});
builder.element('p', nest: () {
builder.text('Generálta: ');
builder.element('a',
attributes: {"href": "https://reflabs.hu/scrapecast"},
builder.element('a', attributes: {"href": "https://reflabs.hu/scrapecast"},
nest: () {
builder.text('ScrapeCast');
});
Expand Down
Loading

0 comments on commit 64a21e0

Please sign in to comment.