channel-worker 1.0.18 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,9 @@ class CommandPoller {
49
49
  case 'save_file':
50
50
  await this.handleSaveFile(command);
51
51
  break;
52
+ case 'set_thumbnail':
53
+ await this.handleSetThumbnail(command);
54
+ break;
52
55
  default:
53
56
  // Other commands (scan_facebook_pages, etc.) handled by extension
54
57
  console.log(`[commands] Skipping ${command.type} — handled by extension`);
@@ -212,6 +215,125 @@ class CommandPoller {
212
215
  }
213
216
  }
214
217
 
218
+ async handleSetThumbnail(command) {
219
+ const { thumbnail_url, profile_id, file_path } = command.payload || {};
220
+ console.log(`[commands] Setting thumbnail for profile: ${profile_id}`);
221
+ try {
222
+ const fs = require('fs');
223
+ const path = require('path');
224
+ const WebSocket = require('ws');
225
+
226
+ // 1. Download thumbnail to disk
227
+ const dir = path.dirname(file_path);
228
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
229
+ const res = await fetch(thumbnail_url);
230
+ if (!res.ok) throw new Error(`Download failed: ${res.status}`);
231
+ const buffer = Buffer.from(await res.arrayBuffer());
232
+ fs.writeFileSync(file_path, buffer);
233
+ console.log(`[commands] Thumbnail saved: ${file_path} (${buffer.length} bytes)`);
234
+
235
+ // 2. Find running browser for this profile
236
+ if (!this.nst) {
237
+ const apiKey = await this.api.getSetting('nst_api_key');
238
+ if (apiKey) this.nst = new NstManager(apiKey);
239
+ }
240
+ const browsersRes = await fetch('http://localhost:8848/api/v2/browsers', {
241
+ headers: { 'x-api-key': this.nst?.apiKey || '' },
242
+ });
243
+ const browsersData = await browsersRes.json();
244
+ const browsers = browsersData?.data || [];
245
+ const browser = browsers.find(b => b.name === profile_id) || browsers[0];
246
+ if (!browser) throw new Error('No running browser found');
247
+
248
+ const debugPort = browser.remoteDebuggingPort;
249
+ console.log(`[commands] Browser debug port: ${debugPort}`);
250
+
251
+ // 3. Get WebSocket URL for YouTube Studio tab
252
+ const pagesRes = await fetch(`http://localhost:${debugPort}/json/list`);
253
+ const pages = await pagesRes.json();
254
+ const studioPage = pages.find(p => p.url?.includes('studio.youtube.com') && p.type === 'page');
255
+ if (!studioPage) throw new Error('YouTube Studio tab not found');
256
+
257
+ const wsUrl = studioPage.webSocketDebuggerUrl;
258
+ console.log(`[commands] Connecting CDP: ${wsUrl}`);
259
+
260
+ // 4. Use CDP to set thumbnail file
261
+ const result = await new Promise((resolve, reject) => {
262
+ const ws = new WebSocket(wsUrl);
263
+ let msgId = 1;
264
+
265
+ function send(method, params = {}) {
266
+ const id = msgId++;
267
+ return new Promise((res, rej) => {
268
+ const handler = (data) => {
269
+ const msg = JSON.parse(data);
270
+ if (msg.id === id) {
271
+ ws.removeListener('message', handler);
272
+ if (msg.error) rej(new Error(msg.error.message));
273
+ else res(msg.result);
274
+ }
275
+ };
276
+ ws.on('message', handler);
277
+ ws.send(JSON.stringify({ id, method, params }));
278
+ });
279
+ }
280
+
281
+ ws.on('open', async () => {
282
+ try {
283
+ await send('DOM.enable');
284
+ const doc = await send('DOM.getDocument');
285
+ const node = await send('DOM.querySelector', {
286
+ nodeId: doc.root.nodeId,
287
+ selector: '#file-loader',
288
+ });
289
+
290
+ if (!node?.nodeId) {
291
+ ws.close();
292
+ resolve({ success: false, error: '#file-loader not found' });
293
+ return;
294
+ }
295
+
296
+ await send('DOM.setFileInputFiles', {
297
+ nodeId: node.nodeId,
298
+ files: [file_path],
299
+ });
300
+
301
+ // Trigger change event
302
+ await send('Runtime.evaluate', {
303
+ expression: `
304
+ const inp = document.querySelector('#file-loader');
305
+ if (inp) {
306
+ inp.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
307
+ inp.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
308
+ }
309
+ 'triggered'
310
+ `,
311
+ });
312
+
313
+ ws.close();
314
+ resolve({ success: true, file_path });
315
+ } catch (e) {
316
+ ws.close();
317
+ resolve({ success: false, error: e.message });
318
+ }
319
+ });
320
+
321
+ ws.on('error', (e) => reject(e));
322
+ setTimeout(() => { ws.close(); reject(new Error('CDP timeout')); }, 15000);
323
+ });
324
+
325
+ console.log(`[commands] Thumbnail CDP result:`, result);
326
+ await this.api.updateCommand(command._id, {
327
+ status: result.success ? 'done' : 'failed',
328
+ result,
329
+ error: result.error || null,
330
+ });
331
+ } catch (err) {
332
+ console.error(`[commands] Set thumbnail failed: ${err.message}`);
333
+ await this.api.updateCommand(command._id, { status: 'failed', error: err.message });
334
+ }
335
+ }
336
+
215
337
  async handleCloseProfile(command) {
216
338
  const { profile_id } = command.payload || {};
217
339
  console.log(`[commands] Closing profile: ${profile_id}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "channel-worker",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "Channel Manager worker daemon — runs on remote machines to execute video pipeline jobs",
5
5
  "main": "lib/daemon.js",
6
6
  "bin": {