This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (67 loc) · 2.34 KB
/
app.js
File metadata and controls
72 lines (67 loc) · 2.34 KB
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
import 'dotenv/config'
import RssFeedEmitter from 'rss-feed-emitter'
import Koa from 'koa'
import bodyparser from 'koa-bodyparser'
import { WebSocketServer } from 'ws'
import mongoose from 'mongoose'
import User from './models/User.js'
import api from './routes/api/index.js'
const MONGODB_URI = process.env.MONGODB_URI ?? ''
const PORT = Number(process.env.PORT ?? 3000)
// Database
mongoose.connect(MONGODB_URI).catch(console.error)
// API
const app = new Koa()
app.use(bodyparser())
app.use(api.routes(), api.allowedMethods())
app.use(ctx => {
ctx.status = 404
ctx.body = { error: 'API not found' }
})
// Websocket
const wss = new WebSocketServer({ noServer: true })
const server = app.listen(PORT, '127.0.0.1', () => console.log(`Listening at http://localhost:${PORT}`))
server.on('upgrade', (request, socket, head) => { wss.handleUpgrade(request, socket, head, () => { }) })
// RSS Feed
const feeder = new RssFeedEmitter({ skipFirstLoad: true })
feeder.add({ url: 'https://scantrad.net/rss/', eventName: 'chapitres' })
feeder.on('chapitres', function (item) {
const links = item.description.match(/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm)
const chapter = {
manga: {
id: links[0].split('/').pop(),
name: getGroup(item.title, /Scan - (.*?) Chapitre/g),
thumbnail: links[1]
},
title: getGroup(item.description, /">(.*?)<\/a>/g),
number: Number(item.link.match(/[^\/]+$/g)[0])
}
// ws-sf
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN)
client.send(JSON.stringify(chapter))
})
// Mobile app
User.find({ follows: chapter.manga.id }).exec().then(ret => ret.map(e => e.token)).then(tokens => {
if (!tokens.length) return
fetch('https://exp.host/--/api/v2/push/send', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'accept-encoding': 'gzip, deflate',
'host': 'exp.host'
},
body: JSON.stringify({
to: tokens,
title: `${chapter.manga.name} - ${chapter.number}`,
body: chapter.title,
priority: 'default',
sound: 'default',
channelId: 'default',
})
}).catch(console.error)
}).catch(console.error)
})
feeder.on('error', () => { })
function getGroup(str, regex) { return Array.from(str.matchAll(regex)).pop()[1] }