-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
154 lines (130 loc) · 5.95 KB
/
app.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
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
const Messenger = require('./lib/messenger')
const mongoose = require('mongoose')
const config = require('./config')
const express = require("express")
const randomLinks = require("./config/randomLinks.json");
const TempsDeFlorsService = require('./services/tempsDeFlors')
const TwitchService = require("./services/twitch");
const ScreenshotService = require("./services/screenshot");
const HAService = require("./services/ha");
const moment = require('moment-timezone')
const EventSub = require('./lib/eventSub')
const handlers = require('./handlers')
mongoose.connect(config.database).then(() => {
const messenger = new Messenger()
messenger.init()
.then(async (bot) => {
console.log('Connected')
const app = express()
app.set('view engine', 'ejs');
app.use(express.static('public'))
var num = 0;
app.use(function (req, res, next) {
const method = req.method;
const url = req.url;
console.log((++num) + " " + method + " " + url);
next();
});
app.get('/transcribe', async function(req, res) {
await HAService.hibernateTranscriberPC()
await handlers.openAI.uploadStreamToOpenai(`#${config.twitch.channels}`, bot)
const response = {
message: 'transcription started',
status: 'success'
};
res.json(response);
});
app.get('/p/:id', (req, res) => {
const spotNumber = parseInt(req.params.id)
if (typeof spotNumber === 'number') {
TempsDeFlorsService.getTFSpot(config.twitch.roomId, spotNumber).then((spot) => {
res.redirect(`https://www.google.com/maps?q=${spot.coordinates}`)
})
}
});
app.get('/listado', function(req, res) {
TempsDeFlorsService.getTFSpots(config.twitch.roomId).then((spots) => {
const percentage = parseFloat((spots.filter((s) => s.visited).length / spots.length * 100).toFixed(2))
res.render('pages/list',{
spots: spots,
url: config.externalUrl,
channel: config.twitch.channels,
percentage: percentage,
bgClass: percentage > 50 ? 'bg-success' : (percentage > 25 ? 'bg-warning' : 'bg-danger')
});
})
});
app.get('/fotos', function(req, res) {
TempsDeFlorsService.getTFSpots(config.twitch.roomId).then((spots) => {
res.render('pages/gallery',{
spots: spots.filter((s) => s.screenshot !== null),
url: config.externalUrl,
channel: config.twitch.channels
});
})
});
app.get('/stream', function(req, res) {
TwitchService.getChannel().then(async (channel) => {
if (channel) {
const screenshots = await ScreenshotService.getScreenshots(channel.streamId)
res.render('pages/stream',{
screenshots: screenshots,
url: config.externalUrl,
channel: config.twitch.channels,
moment: moment,
title: channel.title
});
}
});
});
app.get('/comandos', function(req, res) {
res.render('pages/comandos',{
channel: config.twitch.channels
});
});
app.get('/rutas', function(req, res) {
res.render('pages/rutas',{
channel: config.twitch.channels
});
});
app.use('/images', express.static('images'));
app.get('/i/:id', async(req, res) => {
const { id } = req.params;
TwitchService.getChannel().then((channel) => {
if (channel) {
ScreenshotService.getScreenshots(channel.streamId).then ((screenshots) => {
if (screenshots.length > 0) {
const image = screenshots.find((s) => s.name === id)
if (image) {
res.redirect(config.externalUrl + `/stream/#lg=1&slide=${id}`);
} else {
res.sendFile(__dirname + `/public/images/${id}.jpg`)
}
} else {
res.sendFile(__dirname + `/public/images/${id}.jpg`)
}
});
} else {
res.sendFile(__dirname + `/public/images/${id}.jpg`)
}
});
});
app.get('/img/:id', (req, res) => {
res.sendFile(__dirname + `/public/images/${req.params.id}.jpg`)
});
app.get('/OF/:id', (req, res) => {
const position = parseInt(req.params.id.replace(config.twitch.channels, ''))
const link = randomLinks.links[position]
res.redirect(link.link);
})
const eventSub = new EventSub();
await eventSub.init(bot)
eventSub.apply(app);
const listener = app.listen(process.env.PORT, async ()=> {
console.log('Listening on port ', + listener.address().port)
await eventSub.markAsReady()
await eventSub.subscribeEvent(config.twitch.roomId)
app.get('/', (req, res) => res.redirect('/stream'))
})
})
})