-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
170 lines (145 loc) · 4.8 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Feed, parseFeed } from "https://deno.land/x/[email protected]/mod.ts";
import { FeedEntry } from "https://deno.land/x/[email protected]/src/types/mod.ts";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import {hourly} from 'https://deno.land/x/[email protected]/cron.ts';
import { Feed as OutFeed, Item} from "npm:[email protected]";
import { Series } from "./series.ts";
const http_port = 9000;
const mangaupdates_api_endpoint = "https://api.mangaupdates.com";
const genre_filter = ["Ecchi", "Hentai", "Lolicon", "Shotacon", "Smut", "Adult", "Harem"];
async function get_feed_finished() : Promise<Array<FeedEntry> | null> {
let http_response:Response;
try {
http_response = await fetch(`${mangaupdates_api_endpoint}/v1/releases/rss`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}
});
} catch (error) {
console.log(`error: ${error}`);
return null;
}
if (!http_response.ok) {
console.log(`error in api request to /v1/releases/rss`)
console.log(http_response)
return null;
}
let feed : Feed;
try {
const xml = await http_response.text();
feed = await parseFeed(xml);
} catch (error) {
console.log(`error parsing feed: ${error}`);
return null;
}
const finished = feed.entries.reduce((acc, x) => {
if (x.title?.value?.includes("(end)")) acc.push(x)
return acc;
}, new Array<FeedEntry>());
return finished;
}
async function get_series(id: string) : Promise<Series | null> {
let http_response:Response;
try {
http_response = await fetch(`${mangaupdates_api_endpoint}/v1/series/${id}`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}
});
} catch (error) {
console.log(`error: ${error}`);
return null;
}
if (!http_response.ok) {
console.log(`error in api request to /series/${id}`)
console.log(http_response)
const body = await http_response.text();
console.log(body);
return null;
}
let series:Record<string, unknown>;
try {
series = await http_response.json();
} catch (error) {
console.log(`error: could not parse json: ${error}`);
return null;
}
return (series as unknown) as Series;
}
function make_rss_entry(series: Series): Item {
const content = `Genres: ${series.genres.reduce((acc: string, x) => {
if (acc == "") return x.genre;
else return `${acc}, ${x.genre}`;
}, "")}<br>
Chapters: ${series.latest_chapter}<br>
Year: ${series.year}<br>
Rating: ${series.bayesian_rating} (${series.rating_votes} votes)<br>
Status: ${series.status}<br>
Authors: ${series.authors.reduce((acc: string, x) => `${acc} ${x.name}`, "")}<br><br>
${series.description}`;
return {
title: `Completed - ${series.title}`,
link: series.url,
date: new Date(series.last_updated.timestamp * 1000),
description: series.description,
image: series.image.url.original,
author: series.authors.map(x => {return {name: x.name}}),
content: content,
}
}
function filter_genres(series: Series) : boolean {
for (const genre of series.genres) {
if (genre_filter.includes(genre.genre)) {
return true;
}
}
return false;
}
let rss_feed : string;
async function update_feed() {
console.log("updating feed...");
const finished = await get_feed_finished();
if (finished === null) {
console.log(`error!!`);
return;
}
const feed = new OutFeed({
title: "Completed Manga",
id: "completed manga",
copyright: "none",
});
for (const entry of finished) {
const desc = entry.description?.value;
if (desc === undefined) {
console.log("error: description was undefined");
continue;
}
const id_regex = /(?<=\?id=).+?(?=\&)/;
const id = id_regex.exec(desc);
if (id?.[0] === undefined) {
console.log("error: regex found no id matches");
continue;
}
const series = await get_series(id?.[0]);
if (series === null) continue;
if (filter_genres(series)) continue;
feed.addItem(make_rss_entry(series));
}
rss_feed = feed.rss2();
}
await update_feed();
hourly(() => {
update_feed();
});
const http_handler = (request: Request): Response => {
const response = new Response(rss_feed, {
status: 200,
});
response.headers.append("Content-Type", "application/rss+xml")
return response;
}
await serve(http_handler, { port: http_port });