-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
85 lines (76 loc) · 2.73 KB
/
index.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
/*
This Google Script will find matching videos on Youtube
and send them in an email digest
Tutorial: www.labnol.org/youtube-email-alerts-201219
Author: Amit Agarwal https://digitalinspiration.com/
*/
const CONFIG = {
emailAddress: 'amit@labnol.org',
searchQueries: ['tesla model y', "the queen's gambit", 'minecraft tutorial'],
negativeWords: ['sponsored', 'money'],
geographicRegion: 'US',
preferredVideoLanguage: 'en',
maxVideosPerQuery: 10,
emailAlertHour: 11,
emailAlertTimezone: 'GMT',
};
const getLastRunDate_ = () => {
const dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
const formatDate = (date) => Utilities.formatDate(date, 'UTC', dateFormat);
const service = PropertiesService.getUserProperties();
const lastRun = service.getProperty('LAST_RUN_DATE') || '';
service.setProperty('LAST_RUN_DATE', String(Date.now()));
if (lastRun) return formatDate(new Date(parseInt(lastRun, 10)));
const date = new Date();
date.setDate(date.getDate() - 3);
return formatDate(date);
};
const fetchYouTubeVideos_ = (query, lastRunDate) => {
const data = YouTube.Search.list(['snippet'], {
maxResults: CONFIG.maxVideosPerQuery,
regionCode: CONFIG.geographicRegion,
publishedAfter: lastRunDate,
relevanceLanguage: CONFIG.preferredVideoLanguage,
q: query,
type: ['video'],
fields: 'items(id(videoId),snippet(title, channelTitle, channelId))',
});
return data.items
.map((item) => {
const { id: { videoId } = {}, snippet: { title, channelTitle, channelId } = {} } = item;
return { videoId, title, channelTitle, channelId };
})
.filter(({ channelTitle, title }) => {
const input = [channelTitle, title].join(' ').toLowerCase();
return CONFIG.negativeWords.filter((word) => input.indexOf(word.toLowerCase()) !== -1).length === 0;
});
};
const triggerYouTubeAlerts = () => {
const lastRunDate = getLastRunDate_();
const videos = CONFIG.searchQueries
.map((query) => fetchYouTubeVideos_(query, lastRunDate))
.reduce((arr, input) => {
return arr.concat(input);
}, []);
if (videos.length) {
const template = HtmlService.createTemplateFromFile('index');
template.videos = videos;
MailApp.sendEmail(CONFIG.emailAddress, `[YouTube Alerts] ${videos.length} videos found`, '', {
name: 'YouTube Email Alerts',
htmlBody: template.evaluate().getContent(),
});
}
};
const initialize = () => {
const triggers = ScriptApp.getProjectTriggers();
for (let i = 0; i < triggers.length; i += 1) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger(triggerYouTubeAlerts.name)
.timeBased()
.everyDays(1)
.atHour(parseInt(CONFIG.emailAlertHour, 10))
.inTimezone(CONFIG.emailAlertTimezone)
.create();
triggerYouTubeAlerts();
};