Phoe2004 commited on
Commit
f1a99a3
·
verified ·
1 Parent(s): 459ae25

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -22
app.py CHANGED
@@ -320,6 +320,39 @@ def api_cookie_check():
320
  'keys': keys,
321
  })
322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  def auto_poster_worker():
324
  global bot_running
325
  add_log('🤖 Auto Poster started')
@@ -328,28 +361,24 @@ def auto_poster_worker():
328
 
329
  while bot_running:
330
  try:
331
- import feedparser
332
- feed_url = f'https://www.youtube.com/feeds/videos.xml?channel_id={CHANNEL_ID}'
333
- feed = feedparser.parse(feed_url)
334
-
335
- if feed.entries:
336
- latest = feed.entries[0]
337
- video_id = latest.get('yt_videoid', '')
338
- title = latest.get('title', '')
339
-
340
- if last_id is None:
341
- last_id = video_id
342
- add_log(f'📌 Initialized. Latest: {video_id} — {title[:40]}')
343
- elif video_id != last_id:
344
- add_log(f'📹 New video detected: {title[:50]}')
345
- video_path = download_video(f'https://youtube.com/watch?v={video_id}', '720')
346
- if video_path:
347
- upload_to_tiktok(video_path, title)
348
- try: os.remove(video_path)
349
- except: pass
350
- last_id = video_id
351
- else:
352
- add_log(f'💤 No new video. Last: {video_id}')
353
  except Exception as e:
354
  add_log(f'⚠️ Worker error: {e}')
355
 
 
320
  'keys': keys,
321
  })
322
 
323
+ def get_latest_youtube_video(channel_id):
324
+ """Use yt-dlp to get latest video ID & title — bypasses RSS cache delay"""
325
+ try:
326
+ cmd = [
327
+ 'yt-dlp',
328
+ '--flat-playlist',
329
+ '--playlist-end', '1',
330
+ '--print', '%(id)s|||%(title)s',
331
+ f'https://www.youtube.com/channel/{channel_id}/videos'
332
+ ]
333
+ if os.path.exists(COOKIES_FILE):
334
+ cmd = ['yt-dlp', '--cookies', COOKIES_FILE,
335
+ '--flat-playlist', '--playlist-end', '1',
336
+ '--print', '%(id)s|||%(title)s',
337
+ f'https://www.youtube.com/channel/{channel_id}/videos']
338
+
339
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
340
+ if result.returncode == 0 and result.stdout.strip():
341
+ line = result.stdout.strip().splitlines()[0]
342
+ parts = line.split('|||', 1)
343
+ if len(parts) == 2:
344
+ vid_id = parts[0].strip()
345
+ title = parts[1].strip()
346
+ if vid_id:
347
+ return vid_id, title
348
+ add_log(f'⚠️ yt-dlp stderr: {result.stderr[:200]}')
349
+ except subprocess.TimeoutExpired:
350
+ add_log('⚠️ yt-dlp channel check timed out')
351
+ except Exception as e:
352
+ add_log(f'⚠️ yt-dlp channel check error: {e}')
353
+ return None, None
354
+
355
+
356
  def auto_poster_worker():
357
  global bot_running
358
  add_log('🤖 Auto Poster started')
 
361
 
362
  while bot_running:
363
  try:
364
+ video_id, title = get_latest_youtube_video(CHANNEL_ID)
365
+
366
+ if not video_id:
367
+ add_log('⚠️ Could not fetch latest video — retrying next cycle')
368
+ elif last_id is None:
369
+ last_id = video_id
370
+ add_log(f'📌 Initialized. Latest: {video_id} {title[:40]}')
371
+ elif video_id != last_id:
372
+ add_log(f'📹 New video detected: {title[:50]}')
373
+ video_path = download_video(f'https://youtube.com/watch?v={video_id}', '720')
374
+ if video_path:
375
+ upload_to_tiktok(video_path, title)
376
+ try: os.remove(video_path)
377
+ except: pass
378
+ last_id = video_id
379
+ else:
380
+ add_log(f'💤 No new video. Last: {video_id}')
381
+
 
 
 
 
382
  except Exception as e:
383
  add_log(f'⚠️ Worker error: {e}')
384