-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.gs
More file actions
81 lines (69 loc) · 2.39 KB
/
Code.gs
File metadata and controls
81 lines (69 loc) · 2.39 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
function trackAllChannels() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName('Channels');
if (!mainSheet) {
throw new Error('Main sheet "Channels" not found.');
}
var rows = mainSheet.getDataRange().getValues();
rows.shift(); // Remove header row
rows.forEach(function (row) {
var channelId = row[0];
var playlistId = row[1] || ''; // Optional per channel
if (!channelId) return;
// Get channel title
var channelInfo = YouTube.Channels.list('snippet', { id: channelId });
if (!channelInfo.items || channelInfo.items.length === 0) {
Logger.log('Channel not found: ' + channelId);
return;
}
var channelTitle = channelInfo.items[0].snippet.title;
var safeTitle = channelTitle.replace(/[\\\/\?\*\[\]]/g, '_'); // Sheet-safe name
// Create or get the channel's dedicated sheet
var channelSheet = ss.getSheetByName(safeTitle);
if (!channelSheet) {
channelSheet = ss.insertSheet(safeTitle);
channelSheet.appendRow(['Video ID', 'Title', 'URL', 'Date Added']);
}
// Search for live videos
var searchResponse = YouTube.Search.list('snippet', {
channelId: channelId,
eventType: 'live',
type: 'video',
maxResults: 5
});
if (!searchResponse.items || searchResponse.items.length === 0) {
Logger.log('No livestreams for ' + channelTitle);
return;
}
// Get logged IDs
var lastRow = channelSheet.getLastRow();
var loggedIds = lastRow > 1 ? channelSheet.getRange(2, 1, lastRow - 1, 1).getValues().flat() : [];
searchResponse.items.forEach(function (item) {
var videoId = item.id.videoId;
var title = item.snippet.title;
var url = 'https://www.youtube.com/watch?v=' + videoId;
if (loggedIds.indexOf(videoId) !== -1) {
Logger.log('Already logged: ' + videoId);
return;
}
// Add to playlist if provided
if (playlistId) {
YouTube.PlaylistItems.insert(
{
snippet: {
playlistId: playlistId,
resourceId: {
kind: 'youtube#video',
videoId: videoId
}
}
},
'snippet'
);
}
// Log in channel sheet
channelSheet.appendRow([videoId, title, url, new Date()]);
Logger.log('Added livestream for ' + channelTitle + ': ' + title);
});
});
}