-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (89 loc) · 5.62 KB
/
server.js
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
console.log('Anilist-notifications started!')
const fetch = require('node-fetch')
const fs = require('fs')
const { configClass, query } = require('./config.js')
const { notificationManager, notificationBuilder } = require('./ntfy.js')
const config = new configClass();
const manager = new notificationManager(config.ntfy.address, config.ntfy.port, config.ntfy.topic, config.ntfy.authType, config.ntfy.username, config.ntfy.password)
async function main() {
let url = 'https://graphql.anilist.co';
let options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${config.token.token}`
},
body: JSON.stringify({
'query': query
})
};
let res_raw = await fetch(url, options); let res = await res_raw.json();
if (res.errors) {
for (let i in res.errors) { console.error(`Anilist API error: \"${res.errors[i].message}\"; Code: ${res.errors[i].status}`); }
} else {
let notifications = res.data.page.notifications;
let notificationIDs = notifications.map(({ id }) => id)
fs.readFile('./notifications.json', {encoding:'utf8'}, (err, data) => {
if (err) {
console.error(err)
let notificationsFile = [];
fs.writeFile('./notifications.json', JSON.stringify(notificationIDs), (err) => {
for (let i = 0;i < notifications.length;i++) {
if (notificationsFile.indexOf(notifications[i].id) == -1) {
let apiNotification = notifications[i];
let notification = new notificationBuilder();
if (apiNotification.type == 'AIRING') {
notification.setTitle(`A new episode of ${apiNotification.media.title.userPreferred} has aired.`)
notification.setBody(apiNotification.contexts[0]+apiNotification.episode.toString()+apiNotification.contexts[1]+apiNotification.media.title.userPreferred+apiNotification.contexts[2])
notification.setIcon(apiNotification.media.coverImage.medium)
notification.setClickEvent(apiNotification.media.siteUrl)
manager.send(notification)
} else {
notification.setTitle("New AniList notification.")
notification.setBody("You have recieved a new anilist notification.")
notification.setIcon('https://anilist.co/img/icons/android-chrome-512x512.png')
notification.setClickEvent("https://anilist.co/notifications")
notification.setPriority('low')
manager.send(notification)
}
}
}
})
} else {
let notificationsFile = JSON.parse(data); if (notificationsFile == undefined) { notificationsFile = [] };
fs.writeFile('./notifications.json', JSON.stringify(notificationIDs), (err) => {
for (let i = 0;i < notifications.length;i++) {
if (notificationsFile.indexOf(notifications[i].id) == -1) {
let apiNotification = notifications[i];
let notification = new notificationBuilder();
if (apiNotification.type === 'AIRING') {
notification.setTitle(`A new episode of ${apiNotification.media.title.userPreferred} has aired.`)
notification.setBody(apiNotification.contexts[0]+apiNotification.episode.toString()+apiNotification.contexts[1]+apiNotification.media.title.userPreferred+apiNotification.contexts[2])
notification.setIcon(apiNotification.media.coverImage.medium)
notification.setClickEvent(apiNotification.media.siteUrl)
manager.send(notification)
} else if (apiNotification.type === 'RELATED_MEDIA_ADDITION') {
notification.setTitle(`A new ${(apiNotification.media.type) === 'ANIME' ? 'anime' : 'manga'} was announced.`)
notification.setBody(`${apiNotification.media.title.userPreferred} was added to anilist.`)
notification.setIcon(apiNotification.media.coverImage.medium)
notification.setClickEvent(apiNotification.media.siteUrl)
manager.send(notification)
} else {
notification.setTitle("New AniList notification.")
notification.setBody("You have recieved a new anilist notification.")
notification.setIcon('https://anilist.co/img/icons/android-chrome-512x512.png')
notification.setClickEvent("https://anilist.co/notifications")
notification.setPriority('low')
manager.send(notification)
}
}
}
})
}
})
}
}
setTimeout(() => {if (config.token.token) {config.token_verify(manager)}}, 1000)
setInterval(() => {if (config.token.token) {main()}}, config.refresh);
setInterval(() => {if (config.token.token) {config.token_verify(manager)}}, 86400000);