log all
This commit is contained in:
@@ -7,8 +7,12 @@ import sys
|
||||
import os
|
||||
import yaml
|
||||
import time
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
# Logger oluştur
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from src.database import Database
|
||||
@@ -180,6 +184,8 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
|
||||
# RSS-Bridge'den videoları çek (max_items'ın 2 katı kadar çek, böylece yeterli video olur)
|
||||
# RSS-Bridge'den daha fazla video çekiyoruz çünkü bazıları transcript'siz olabilir
|
||||
rss_bridge_limit = max(max_items * 2, 50) # En az 50 video çek
|
||||
logger.info(f"[PROCESS] Channel {channel_id} için RSS-Bridge'den video listesi çekiliyor (limit: {rss_bridge_limit})")
|
||||
|
||||
try:
|
||||
videos = fetch_videos_from_rss_bridge(
|
||||
base_url="https://rss-bridge.org/bridge01",
|
||||
@@ -187,14 +193,23 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
|
||||
format="Atom",
|
||||
max_items=rss_bridge_limit
|
||||
)
|
||||
logger.info(f"[PROCESS] RSS-Bridge'den {len(videos)} video alındı")
|
||||
except Exception as e:
|
||||
logger.error(f"[PROCESS] ❌ RSS-Bridge hatası: {type(e).__name__} - {str(e)}")
|
||||
raise Exception(f"RSS-Bridge hatası: {e}")
|
||||
|
||||
# Yeni videoları veritabanına ekle
|
||||
new_videos_count = 0
|
||||
for video in videos:
|
||||
video['channel_id'] = channel_id
|
||||
if not db.is_video_processed(video['video_id']):
|
||||
db.add_video(video)
|
||||
new_videos_count += 1
|
||||
|
||||
if new_videos_count > 0:
|
||||
logger.info(f"[PROCESS] {new_videos_count} yeni video veritabanına eklendi")
|
||||
else:
|
||||
logger.debug(f"[PROCESS] Tüm videolar zaten veritabanında")
|
||||
|
||||
# Bekleyen videoları işle (max_items kadar, 20'şer batch'ler halinde)
|
||||
# YouTube IP blocking'i önlemek için her batch'te 20 video işlenir
|
||||
@@ -204,51 +219,82 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
|
||||
|
||||
# Tüm bekleyen videoları al (channel_id'ye göre filtrele)
|
||||
all_pending_videos = [v for v in db.get_pending_videos() if v['channel_id'] == channel_id]
|
||||
logger.info(f"[PROCESS] Channel {channel_id} için {len(all_pending_videos)} bekleyen video bulundu (max_items: {max_items})")
|
||||
|
||||
# max_items kadar transcript işlenene kadar batch'ler halinde işle
|
||||
total_batches = (len(all_pending_videos) + batch_size - 1) // batch_size
|
||||
current_batch = 0
|
||||
|
||||
for batch_start in range(0, len(all_pending_videos), batch_size):
|
||||
if processed_count >= max_items:
|
||||
logger.info(f"[PROCESS] Maksimum transcript sayısına ulaşıldı ({processed_count}/{max_items})")
|
||||
break
|
||||
|
||||
|
||||
current_batch += 1
|
||||
batch_videos = all_pending_videos[batch_start:batch_start + batch_size]
|
||||
logger.info(f"[BATCH] Batch {current_batch}/{total_batches} başlatılıyor ({len(batch_videos)} video, Toplam işlenen: {processed_count}/{max_items})")
|
||||
|
||||
batch_processed = 0
|
||||
batch_cached = 0
|
||||
batch_failed = 0
|
||||
|
||||
for video in batch_videos:
|
||||
if processed_count >= max_items:
|
||||
break
|
||||
|
||||
video_id = video['video_id']
|
||||
video_title = video.get('video_title', 'N/A')[:50]
|
||||
|
||||
# Cache kontrolü: 3 gün içinde işlenmiş transcript varsa atla
|
||||
if db.is_transcript_cached(video['video_id'], cache_days=3):
|
||||
print(f"Video {video['video_id']} transcript'i cache'de (3 gün içinde işlenmiş), atlanıyor")
|
||||
if db.is_transcript_cached(video_id, cache_days=3):
|
||||
logger.debug(f"[CACHE] Video {video_id} ({video_title}) transcript'i cache'de, atlanıyor")
|
||||
batch_cached += 1
|
||||
continue
|
||||
|
||||
try:
|
||||
logger.info(f"[VIDEO] Video işleniyor: {video_id} - {video_title}")
|
||||
|
||||
# Transcript çıkar
|
||||
transcript = extractor.fetch_transcript(
|
||||
video['video_id'],
|
||||
video_id,
|
||||
languages=['tr', 'en']
|
||||
)
|
||||
|
||||
if transcript:
|
||||
# Transcript temizle
|
||||
logger.debug(f"[VIDEO] Video {video_id} transcript'i temizleniyor...")
|
||||
raw, clean = cleaner.clean_transcript(transcript, sentences_per_paragraph=3)
|
||||
|
||||
# Veritabanına kaydet (her batch hemen kaydedilir)
|
||||
db.update_video_transcript(
|
||||
video['video_id'],
|
||||
video_id,
|
||||
raw,
|
||||
clean,
|
||||
status=1,
|
||||
language='tr'
|
||||
)
|
||||
processed_count += 1
|
||||
print(f"Video {video['video_id']} transcript'i işlendi ve veritabanına kaydedildi ({processed_count}/{max_items})")
|
||||
batch_processed += 1
|
||||
logger.info(f"[VIDEO] ✅ Video {video_id} başarıyla işlendi ve kaydedildi ({processed_count}/{max_items})")
|
||||
else:
|
||||
logger.warning(f"[VIDEO] ⚠️ Video {video_id} transcript'i alınamadı (None döndü)")
|
||||
batch_failed += 1
|
||||
db.mark_video_failed(video_id, "Transcript None döndü")
|
||||
except Exception as e:
|
||||
print(f"Transcript çıkarım hatası {video['video_id']}: {e}")
|
||||
db.mark_video_failed(video['video_id'], str(e))
|
||||
error_type = type(e).__name__
|
||||
error_msg = str(e)[:200]
|
||||
logger.error(f"[VIDEO] ❌ Video {video_id} işleme hatası: {error_type} - {error_msg}")
|
||||
db.mark_video_failed(video_id, str(e))
|
||||
batch_failed += 1
|
||||
|
||||
# Batch özeti
|
||||
logger.info(f"[BATCH] Batch {current_batch}/{total_batches} tamamlandı - İşlenen: {batch_processed}, Cache: {batch_cached}, Başarısız: {batch_failed}")
|
||||
|
||||
# Batch tamamlandı, kısa bir bekleme (rate limiting için)
|
||||
if processed_count < max_items and batch_start + batch_size < len(all_pending_videos):
|
||||
time.sleep(2) # Batch'ler arası 2 saniye bekleme
|
||||
wait_time = 2
|
||||
logger.debug(f"[BATCH] Batch'ler arası bekleme: {wait_time} saniye")
|
||||
time.sleep(wait_time)
|
||||
|
||||
# İşlenmiş videoları getir
|
||||
processed_videos = db.get_processed_videos(
|
||||
@@ -256,6 +302,8 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
|
||||
channel_id=channel_id
|
||||
)
|
||||
|
||||
logger.info(f"[PROCESS] ✅ Channel {channel_id} işleme tamamlandı - {len(processed_videos)} işlenmiş video döndürülüyor")
|
||||
|
||||
return {
|
||||
'videos': processed_videos,
|
||||
'channel_id': channel_id,
|
||||
|
||||
Reference in New Issue
Block a user