-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
196 lines (182 loc) · 5.41 KB
/
Code.gs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var SheetLogger = BetterLog
var IgnoreTitleRegex = /會員限定|會限|會員場/
var PAST_LIMIT = 3 * 3600 * 1000 // 3 hours
var CHAT_LIMIT = 30 * 86400 * 1000 // 30 days
var LIVE_LENGTH = 3600 * 1000 // 1 hour
var subscribedChannels_ = [];
var calendarId_ = null;
var targetCalendar_ = null;
/**
* Set your Calendar ID and subscribed channels. It needs to be called before any other function
*
* @param {string} calendarId Calendar ID
* @param {array} subscribedChannels List of your subscribed channels
* @returns {YouTubeLiveCalendar} This object
*/
function init(calendarId, subscribedChannels) {
targetCalendar_ = CalendarApp.getCalendarById(calendarId)
subscribedChannels_ = subscribedChannels;
return this;
}
/**
* Update upcoming live events for single channel.
*
* @param {string} key Channel key defined in subscribedChannels
* @returns {YouTubeLiveCalendar} This object
*/
function runChannel(key) {
var channel = getChannelByKey_(key);
var videoIds = getUpcommingVideoIds_(channel);
SheetLogger.log('Run %s, %s upcoming lives', key, videoIds.length.toString());
runVideos(videoIds);
}
/**
* Update upcoming live events for all channels in subscribedChannels.
*
* @returns {YouTubeLiveCalendar} This object
*/
function runAllChannels() {
var videoIds = [];
for (var subscribedChannel of subscribedChannels_) {
videoIds = videoIds.concat(getUpcommingVideoIds_(subscribedChannel));
}
runVideos(videoIds);
}
/**
* Process specific videos
*
* @param {string[]} videoIds YouTube video IDs list
* @returns {YouTubeLiveCalendar} This object
*/
function runVideos(videoIds) {
if (videoIds.length === 0) {
return;
}
// Logger.log(videoIds)
var results = YouTube.Videos.list('snippet,liveStreamingDetails,status', {
id: videoIds,
});
var oldEvents = getOldEvents_();
for (var i in results.items) {
var item = results.items[i];
var now = new Date();
var past_limit = new Date(now.getTime() - PAST_LIMIT);
var chat_limit = new Date(now.getTime() + CHAT_LIMIT);
var startTime = new Date(item.liveStreamingDetails.scheduledStartTime);
var publishedAt = new Date(item.snippet.publishedAt);
var endTime = new Date(startTime.getTime() + LIVE_LENGTH);
var channel = getChannelById_(item.snippet.channelId);
var shortName = channel ? channel.shortName : null;
var url = 'https://youtu.be/' + item.id;
var title = (shortName ? shortName + '-' : '') + item.snippet.title;
var description = url;
var status;
if (IgnoreTitleRegex.test(title)) {
status = 'member';
} else if (startTime > chat_limit) {
status = 'chat';
} else if (startTime < past_limit) {
status = 'past';
} else if (item.id in oldEvents) {
var event = oldEvents[item.id];
event.setTime(startTime, endTime);
event.setTitle(title);
event.setDescription(description);
status = 'old';
} else {
var event = targetCalendar_.createEvent(title, startTime, endTime);
event.setDescription(description);
status = 'new';
}
SheetLogger.log(
'%s | %s | %s | %s | %s | %s',
status,
item.id,
startTime.toLocaleString('zh-tw', { hourCycle: 'h23' }),
item.snippet.channelTitle,
item.snippet.title,
publishedAt.toLocaleString('zh-tw', { hourCycle: 'h23' })
);
}
}
function getUpcommingVideoIds_(subscribedChannel) {
var videoIds = [];
// Logger.log(subscribedChannel)
var results = YouTube.Search.list('snippet', {
channelId: subscribedChannel.id,
eventType: 'upcoming',
type: ['video'],
});
for (var i in results.items) {
var item = results.items[i];
videoIds.push(item.id.videoId);
// Logger.log(item)
// Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title)
}
return videoIds;
}
function getChannelById_(channelId) {
for (channel of subscribedChannels_) {
if (channel.id == channelId) {
return channel;
}
}
return null;
// throw new Error('Cannot found channel ' + channelId)
}
function getChannelByKey_(channelKey) {
for (channel of subscribedChannels_) {
if (channel.key == channelKey) {
return channel;
}
}
throw new Error('Cannot found channel ' + channelKey);
}
function getOldEvents_() {
var now = new Date();
var start = new Date(now.getTime() - 86400 * 1000);
var end = new Date('2030-12-31 23:59:59');
var events = targetCalendar_.getEvents(start, end);
var results = {};
for (var event of events) {
var m = event.getDescription().match(/https:\/\/youtu\.be\/(.{11})/);
if (!m) {
SheetLogger.log('%s | %s', event.getTitle(), event.getDescription());
continue;
}
var videoId = m[1];
results[videoId] = event;
// Logger.log('[%s] %s - %s', videoId, event.getTitle(), event.getDescription())
}
return results;
}
/**
* List your subscriptions with channel ID and Channel name
*/
function listSubscriptions() {
var cnt = 0;
var pageToken = '';
while (true) {
var results = YouTube.Subscriptions.list('snippet', {
maxResults: 50,
mine: true,
pageToken: pageToken,
});
for (var i in results.items) {
cnt += 1;
var item = results.items[i];
// Logger.log(item)
Logger.log(
'#%s [%s] %s',
cnt.toString(),
item.snippet.resourceId.channelId,
item.snippet.title
);
}
if (results.nextPageToken) {
pageToken = results.nextPageToken;
} else {
break;
}
}
}