forked from shalvah/DownloadThisVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
89 lines (77 loc) · 3.42 KB
/
handler.js
File metadata and controls
89 lines (77 loc) · 3.42 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
const { finish, getRelativeTime, getSponsoredLink } = require('./src/utils');
const sns = require('./src/services/sns');
const cloudwatch = require('./src/services/cloudwatch');
const ops = require('./src/services/tweet_operations');
const makeCache = require('./src/services/factory.cache');
const makeTwitter = require('./src/services/factory.twitter');
module.exports.fetchTweetsToDownload = async (event, context, callback) => {
const cache = await makeCache();
const twitter = makeTwitter(cache);
let lastTweetRetrieved = null;
let count = 0;
let mentions = await twitter.getMentions();
while (mentions.length) {
await sns.sendToSns(mentions);
lastTweetRetrieved = mentions[0].id;
count += mentions.length;
mentions = await twitter.getMentions(lastTweetRetrieved);
}
if (lastTweetRetrieved) {
await cache.setAsync('lastTweetRetrieved', lastTweetRetrieved);
}
finish(callback, cache).success(`Published ${count} tweets`);
};
module.exports.sendDownloadLink = async (event, context, callback) => {
const cache = await makeCache();
const twitter = makeTwitter(cache);
const tweets = sns.getPayloadFromSnsEvent(event);
const tweetObjects = await twitter.getActualTweetsReferenced(tweets);
let results = await Promise.all(tweetObjects.map((tweetObject) => {
let tweet = tweets.find(t => t.referencing_tweet === tweetObject.id_str);
return ops.extractVideoLink(tweetObject, {cache, twitter})
.then(link => ops.handleTweetProcessingSuccess(tweet, link, {cache, twitter}))
.catch(e => ops.handleTweetProcessingError(e, tweet, {cache, twitter, tweetObject}));
}));
results = results.filter(r => r !== null);
cloudwatch.logResults(results);
finish(callback, cache).success(`Processed ${tweets.length} tasks`);
};
module.exports.retryFailedTasks = async (event, context, callback) => {
const cache = await makeCache();
const tweets = await cache.lrangeAsync('Fail', 0, -1);
if (!tweets.length) {
finish(callback, cache).success(`No tasks for retrying`);
return;
}
await sns.sendToSns(tweets.map(JSON.parse));
await cache.delAsync('Fail');
finish(callback, cache).success(`Sent ${tweets.length} tasks for retrying`);
};
module.exports.getDownloads = async (event, context, callback) => {
const username = event.pathParameters.username;
switch (username) {
case null:
case undefined:
case '':
finish(callback).render('home', { link: getSponsoredLink() });
return;
case 'faq':
const faqs = require('./faqs');
finish(callback).render('faq', { faqs, link: getSponsoredLink() });
return;
default:
const cache = await makeCache();
let downloads = await cache.lrangeAsync(`user-${username}`, 0, -1);
const prepareDownloadforFrontend = (d) => {
return JSON.parse(d, function convertTimeToRelative(key, value) {
return key === 'time' ? getRelativeTime(value) : value;
})
};
downloads = downloads.map(prepareDownloadforFrontend);
finish(callback, cache).render('downloads', {username, downloads, link: getSponsoredLink()});
}
};
module.exports.getHomePage = (event, context, callback) => {
finish(callback).render('home', { link: getSponsoredLink() });
};