-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflow_Fetch.js
More file actions
209 lines (177 loc) · 7.18 KB
/
Workflow_Fetch.js
File metadata and controls
209 lines (177 loc) · 7.18 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
// Version: 1.3
// Purpose: Main logic for fetching videos via RSS, getting details via API, and running predictions.
/**
* Main orchestration function.
* 1. Fetches new video feeds via RSS.
* 2. Filters for videos newer than the last run.
* 3. Fetches details (duration) via YouTube API.
* 4. Predicts destination playlists using the history model.
* 5. Writes results to the 'Videos' sheet.
*
* No parameters.
* No return value.
*/
function checkNewVideos() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName(SHEET_SETTINGS);
const videoSheet = ss.getSheetByName(SHEET_VIDEOS);
// 1. Refresh Playlist Config
const playlistMap = refreshPlaylistConfig(settingsSheet);
const playlistNames = Object.keys(playlistMap);
if (playlistNames.length === 0) {
SpreadsheetApp.getUi().alert('No playlists found. Please check Settings.');
return;
}
// --- BUILD PREDICTION MODEL ---
ss.toast('Analyzing history...', 'Step 0/4');
const model = buildPredictionModel(ss);
// 2. Get Last Run Time
let lastRunStr = settingsSheet.getRange('B1').getValue();
let lastRunDate = new Date(lastRunStr);
if (isNaN(lastRunDate.getTime())) {
if (!lastRunStr || lastRunStr.toString().trim() === '') {
lastRunDate = new Date();
lastRunDate.setDate(lastRunDate.getDate() - 7);
ss.toast('B1 empty. Defaulting to fetching videos from the last 7 days.', 'First Run');
} else {
SpreadsheetApp.getUi().alert('Invalid date in Settings!B1. Please use a format like "YYYY-MM-DD HH:MM:SS" or leave it blank.');
return;
}
}
// 3. Get All Subscriptions
ss.toast('Fetching subscription list...', 'Step 1/4');
const channels = getAllSubscriptions();
if (channels.length === 0) {
SpreadsheetApp.getUi().alert('No subscriptions found.');
return;
}
// --- RSS SCANNING ---
const BATCH_SIZE = 20;
let potentialVideos = [];
const ns = XmlService.getNamespace('http://www.w3.org/2005/Atom');
const ytNs = XmlService.getNamespace('http://www.youtube.com/xml/schemas/2015');
const mediaNs = XmlService.getNamespace('http://search.yahoo.com/mrss/');
for (let i = 0; i < channels.length; i += BATCH_SIZE) {
const currentEnd = Math.min(i + BATCH_SIZE, channels.length);
ss.toast(`Scanning channels ${i + 1} to ${currentEnd}...`, 'Step 2/4: Scanning');
const batch = channels.slice(i, i + BATCH_SIZE);
const requests = batch.map(id => ({
url: `https://www.youtube.com/feeds/videos.xml?channel_id=${id}`,
muteHttpExceptions: true
}));
try {
const responses = UrlFetchApp.fetchAll(requests);
for (let j = 0; j < responses.length; j++) {
const response = responses[j];
if (response.getResponseCode() !== 200) {
const errMsg = `HTTP Error ${response.getResponseCode()} fetching feed for channel ${batch[j]}. Scan aborted.`;
console.error(errMsg);
SpreadsheetApp.getUi().alert('Fetch Error: ' + errMsg);
return;
}
try {
const xml = XmlService.parse(response.getContentText());
const root = xml.getRootElement();
const entries = root.getChildren('entry', ns);
for (const entry of entries) {
const publishedStr = entry.getChild('published', ns).getText();
const publishedDate = new Date(publishedStr);
if (publishedDate > lastRunDate) {
const videoId = entry.getChild('videoId', ytNs).getText();
const title = entry.getChild('title', ns).getText();
const channelName = entry.getChild('author', ns).getChild('name', ns).getText();
let thumbUrl = '';
const group = entry.getChild('group', mediaNs);
if (group) {
const thumb = group.getChild('thumbnail', mediaNs);
if (thumb) thumbUrl = thumb.getAttribute('url').getValue();
}
potentialVideos.push({
channel: channelName,
title: title,
id: videoId,
date: publishedDate,
thumb: thumbUrl,
duration: '',
suggestion: '' // For our AI guess
});
}
}
} catch (e) {
const errMsg = `XML Error parsing feed for channel ${batch[j]}: ${e.message}. Scan aborted.`;
console.error(errMsg);
SpreadsheetApp.getUi().alert('Fetch Error: ' + errMsg);
return;
}
}
} catch (e) {
const errMsg = `Network/Batch Error: ${e.message}. Scan aborted.`;
console.error(errMsg);
SpreadsheetApp.getUi().alert('Fetch Error: ' + errMsg);
return;
}
Utilities.sleep(1000);
}
if (potentialVideos.length === 0) {
ss.toast('No new videos found.', 'Complete', 5);
return;
}
// --- FETCH DURATIONS ---
ss.toast(`Fetching details...`, 'Step 3/4');
const videoIds = potentialVideos.map(v => v.id);
const durationMap = {};
for (let i = 0; i < videoIds.length; i += 50) {
const idBatch = videoIds.slice(i, i + 50).join(',');
try {
const response = YouTube.Videos.list('contentDetails', { id: idBatch });
if (response.items) {
response.items.forEach(item => {
durationMap[item.id] = parseDuration(item.contentDetails.duration);
});
}
} catch (e) {
console.error('API Error: ' + e.message);
ss.toast('Error fetching video durations. Some details may be missing.', 'API Error', 5);
}
}
// --- APPLY PREDICTIONS ---
potentialVideos.forEach(v => {
v.duration = durationMap[v.id] || 'N/A';
v.suggestion = predictPlaylist(v, model);
});
// 4. Sort and Write
ss.toast('Sorting and writing...', 'Step 4/4');
potentialVideos.sort((a, b) => a.date - b.date);
const rows = potentialVideos.map(v => [
escapeFormula(v.suggestion),
v.thumb ? SpreadsheetApp.newCellImage().setSourceUrl(v.thumb).build() : '',
escapeFormula(v.duration),
escapeFormula(v.channel),
escapeFormula(v.title),
escapeFormula(v.id),
v.date
]);
const startRow = videoSheet.getLastRow() + 1;
const targetRange = videoSheet.getRange(startRow, 1, rows.length, 7);
targetRange.setValues(rows);
// Apply Validation
const rule = SpreadsheetApp.newDataValidation()
.requireValueInList(playlistNames, true)
.setAllowInvalid(false)
.build();
videoSheet.getRange(startRow, 1, rows.length, 1).setDataValidation(rule);
// Highlighting Suggestions
const backgrounds = rows.map(row => {
const hasSuggestion = (row[0] !== '' && row[0] !== null);
return hasSuggestion
? ['#fff2cc', null, null, null, null, null, null]
: [null, null, null, null, null, null, null];
});
targetRange.setBackgrounds(backgrounds);
videoSheet.setRowHeights(startRow, rows.length, 95);
const newestDate = potentialVideos[potentialVideos.length - 1].date;
settingsSheet.getRange('B1').setValue(Utilities.formatDate(newestDate, Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss"));
ss.toast(`Done! Found ${potentialVideos.length} new videos.`, 'Complete', 5);
// NEW LINE: Select cell A2 so you can start typing immediately
videoSheet.getRange('A2').activate();
}