-
Notifications
You must be signed in to change notification settings - Fork 3
/
Code.gs
154 lines (120 loc) · 4.49 KB
/
Code.gs
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
const TRIGGER_INTERVAL = 10;
const API_URL = "https://api.mangadex.org";
const LANGUAGE = "en";
function main() {
const webhooks = get_array_from_sheets("webhooks", columns = 2);
const feeds = get_array_from_sheets("feeds", columns = 1).map(x => x[0]);
// !------ DEPRECATED -------!
// const refresh_tokens = get_array_from_sheets("accounts", columns = 1).map(x => x[0]);
const updates = [];
// !------ DEPRECATED -------!
/*
for (const refresh_token of refresh_tokens) {
const access_token = refresh_session(refresh_token);
if (access_token) {
const follow_updates = check_follow_feed_updates(access_token);
updates.push(...follow_updates);
} else {
console.log("failed to log into", refresh_token);
}
}
*/
const custom_updates = check_custom_feed_updates(feeds);
updates.push(...custom_updates);
const unique_updates = updates.filter((x, i) => {
for (let j = i + 1; j < updates.length; j++) {
if (x.id == updates[j].id)
return false;
}
return true;
});
for (const [webhook, message] of webhooks)
post_updates(webhook, unique_updates, message);
}
function post_updates(webhook, updates, message = "")
{
for (const chapter of updates) {
const scanlation_group_url = API_URL + "/group/" + get_type_id(chapter, "scanlation_group");
const scanlation_group = request(scanlation_group_url, "GET");
const manga_url = API_URL + "/manga/" + get_type_id(chapter, "manga");
const manga = request(manga_url, "GET");
const author_url = API_URL + "/author/" + get_type_id(manga.data, "author");
const author = request(author_url, "GET");
const cover_url = API_URL + "/cover/" + get_type_id(manga.data, "cover_art");
const cover = request(cover_url, "GET");
const author_name = author.data.attributes.name;
const scanlation_group_name = scanlation_group != null ? scanlation_group.data.attributes.name : "No Group";
const chapter_title = chapter.attributes.title != null ? chapter.attributes.title : "";
const thumbnail_url = "https://uploads.mangadex.org/covers/" + manga.data.id + "/" + cover.data.attributes.fileName;
const payload = {
"content": message,
"embeds": [
{
"title": "Ch." + chapter.attributes.chapter + " - " + manga.data.attributes.title.en,
"description": chapter_title + "\nAuthor: " + author_name + "\nGroup: " + scanlation_group_name,
"color": 16742144,
"footer": {
"text": "New update available"
},
"url": "https://mangadex.org/chapter/" + chapter.id,
"timestamp": chapter.attributes.createdAt,
"thumbnail": {
"url": thumbnail_url
}
}
]
};
post_webhook(webhook, payload);
}
}
// !------ DEPRECATED -------!
/*
function refresh_session(refresh_token)
{
const login_endpoint = API_URL + "/auth/refresh";
const credentials = { "token": refresh_token };
const login_request = request(login_endpoint, "POST", credentials);
if (login_request != null) {
return login_request.token.session;
} else {
return null;
}
}
function check_follow_feed_updates(access_token)
{
const previous_check = new Date(Date.now() - TRIGGER_INTERVAL * 60000);
const str_previous_check = previous_check.toISOString().substring(0, previous_check.toISOString().indexOf('.'));
const feed_parameters = "?translatedLanguage[]=" + LANGUAGE + "&createdAtSince=" + str_previous_check;
const feed_url = API_URL + "/user/follows/manga/feed" + feed_parameters;
const feed = request(feed_url, "GET", null, true, access_token);
if (feed)
return feed.data;
else
return [];
}
*/
function check_custom_feed_updates(feed_ids)
{
const updates = [];
for (const feed_id of feed_ids) {
const previous_check = new Date(Date.now() - TRIGGER_INTERVAL * 60000);
const str_previous_check = previous_check.toISOString().substring(0, previous_check.toISOString().indexOf('.'));
const feed_parameters = "?translatedLanguage[]=" + LANGUAGE + "&createdAtSince=" + str_previous_check;
const feed_url = API_URL + "/list/" + feed_id + "/feed" + feed_parameters;
const feed = request(feed_url);
if (feed)
updates.push(...feed.data);
}
return updates;
}
function post_webhook(webhook, payload)
{
request(webhook, "POST", payload);
}
function get_type_id(item, type)
{
for (const relationship of item.relationships)
if (relationship.type == type)
return relationship.id;
return null;
}