-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua-bot-function.js
135 lines (129 loc) · 4.63 KB
/
ua-bot-function.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
const fli = require('fli-webtask');
const util = require('util');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const express = fli.npm.express;
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
router
.all('/call', async (req, res) => {
const feed = req.body;
console.log(feed, req.query);
if(_.includes(feed, req.webtaskContext.secrets.topic)) {
const entry = feed.split('<entry>')[1] || '';
const videoId = (entry.match(/<yt:videoId>(.*)<\/yt:videoId>/) || [])[1];
const channelId = (entry.match(/<yt:channelId>(.*)<\/yt:channelId>/) || [])[1];
const published = (entry.match(/<published>(.*)<\/published>/) || [])[1];
const title = (entry.match(/<title>(.*)<\/title>/) || [])[1];
const channelName = (entry.match(/<name>(.*)<\/name>/) || [])[1];
const ignore = req.webtaskContext.secrets.ignore.split(',');
console.log({videoId, channelId, published, title, channelName});
if(!_.includes(ignore, channelId) && videoId && channelId && published && title && channelName) {
const yesterday = new Date(Date.now() - 86400000);
const publishedTime = (new Date(published)).getTime();
if(publishedTime > yesterday) {
let store = await util.promisify((next) => req.webtaskContext.storage.get(next))();
store.id = store.id || [];
store.next = store.next || [];
if(!_.includes(store.id, channelId)) {
store.id.unshift(channelId);
store.next.unshift(videoId);
// do actions
loader({
method: 'post',
url: `${req.webtaskContext.secrets.telegramFunction}/publish`,
qs: {
token: req.webtaskContext.secrets.token
},
json: {
_id: "ua-youtube",
db: "ua-youtube",
payload: {
as: 'link',
shortUrl: `https://youtu.be/${videoId}`,
info: {
title: title
}
}
}
}, () => {});
store.id.length = Math.min(store.id.length, 100);
store.next.length = Math.min(store.next.length, 100);
await util.promisify((data, next) => req.webtaskContext.storage.set(data, next))(store);
}
}
}
}
res.send(_.get(req.query, 'hub.challenge', ''));
})
.all('/next', async (req, res) => {
let store = await util.promisify((next) => req.webtaskContext.storage.get(next))();
store.next = _.compact(store.next || []);
let nextID = store.next.shift();
await util.promisify((data, next) => req.webtaskContext.storage.set(data, next))(store);
res.json({next: nextID});
})
.all('/subscribe', (req, res) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
return res.status(400).send('No token');
}
const page = req.query.page || 1;
const ignore = req.webtaskContext.secrets.ignore.split(',');
console.log("Subscribe page ", page);
as.waterfall([
(next) => loader({
method: 'get',
url: `${req.webtaskContext.secrets.manifest}/${page}.txt`
}, next),
(result, next) => {
result.data.map(id => {
if(!_.includes(ignore, id)) {
console.log("Unsubscribe id ", id);
request.post({
url: req.webtaskContext.secrets.pubsubhubbub,
formData: {
'hub.callback': req.webtaskContext.secrets.callback,
'hub.topic': req.webtaskContext.secrets.topic + id,
'hub.verify': 'async',
'hub.mode': 'unsubscribe'
}
}, () => {
request.post({
url: req.webtaskContext.secrets.pubsubhubbub,
formData: {
'hub.callback': req.webtaskContext.secrets.callback,
'hub.topic': req.webtaskContext.secrets.topic + id,
'hub.verify': 'async',
'hub.mode': 'subscribe'
}
}, () => {
console.log("Subscribe id ", id);
});
});
}
});
if(result.next) {
loader({
method: 'get',
url: req.webtaskContext.secrets.subscribe,
qs: {
token: req.webtaskContext.secrets.token,
page: result.next
}
}, () => {});
}
next();
}
], (error, result) => {
res.json({error, result});
});
});
app
.use(bodyParser.text({type: 'application/atom+xml'}))
.use('/', router);
module.exports = wt.fromExpress(app);