batch batch

This commit is contained in:
salvacybersec
2025-11-13 04:12:05 +03:00
parent bb416e1f37
commit 2dc0eb8b19
6 changed files with 104 additions and 44 deletions

View File

@@ -6,6 +6,7 @@ from typing import Optional
import sys
import os
import yaml
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
@@ -176,13 +177,15 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
extractor = get_extractor()
cleaner = get_cleaner()
# RSS-Bridge'den videoları çek
# 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
try:
videos = fetch_videos_from_rss_bridge(
base_url="https://rss-bridge.org/bridge01",
channel_id=channel_id,
format="Atom",
max_items=max_items
max_items=rss_bridge_limit
)
except Exception as e:
raise Exception(f"RSS-Bridge hatası: {e}")
@@ -193,40 +196,59 @@ def process_channel(channel_id: str, max_items: int = 50) -> dict:
if not db.is_video_processed(video['video_id']):
db.add_video(video)
# Bekleyen videoları işle (YouTube IP blocking'i önlemek için sadece 5 video)
pending_videos = db.get_pending_videos()[:5]
# 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
# max_items: Her istekte kaç video transcript işleneceği (maksimum 100)
batch_size = 20 # Her batch'te işlenecek video sayısı
processed_count = 0 # İşlenen transcript sayısı
for video in pending_videos:
if video['channel_id'] != channel_id:
continue
# 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]
# max_items kadar transcript işlenene kadar batch'ler halinde işle
for batch_start in range(0, len(all_pending_videos), batch_size):
if processed_count >= max_items:
break
batch_videos = all_pending_videos[batch_start:batch_start + batch_size]
# 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")
continue
for video in batch_videos:
if processed_count >= max_items:
break
try:
# Transcript çıkar
transcript = extractor.fetch_transcript(
video['video_id'],
languages=['tr', 'en']
)
if transcript:
# Transcript temizle
raw, clean = cleaner.clean_transcript(transcript, sentences_per_paragraph=3)
# 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")
continue
# Veritabanına kaydet
db.update_video_transcript(
try:
# Transcript çıkar
transcript = extractor.fetch_transcript(
video['video_id'],
raw,
clean,
status=1,
language='tr'
languages=['tr', 'en']
)
except Exception as e:
print(f"Transcript çıkarım hatası {video['video_id']}: {e}")
db.mark_video_failed(video['video_id'], str(e))
if transcript:
# Transcript temizle
raw, clean = cleaner.clean_transcript(transcript, sentences_per_paragraph=3)
# Veritabanına kaydet (her batch hemen kaydedilir)
db.update_video_transcript(
video['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})")
except Exception as e:
print(f"Transcript çıkarım hatası {video['video_id']}: {e}")
db.mark_video_failed(video['video_id'], str(e))
# 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
# İşlenmiş videoları getir
processed_videos = db.get_processed_videos(
@@ -259,9 +281,11 @@ def generate_feed():
channel_url = request.args.get('channel_url')
format_type = request.args.get('format', 'Atom').lower() # Atom veya Rss
try:
max_items = int(request.args.get('max_items', 50))
max_items = int(request.args.get('max_items', 10)) # Default: 10 transcript
# Maksimum 100 transcript (20'şer batch'ler halinde işlenir)
max_items = min(max_items, 100)
except (ValueError, TypeError):
max_items = 50
max_items = 10
# Channel ID'yi normalize et
normalized_channel_id = normalize_channel_id(
@@ -273,12 +297,12 @@ def generate_feed():
if not normalized_channel_id:
return jsonify({
'error': 'Channel ID bulunamadı',
'usage': {
'usage': {
'channel_id': 'UC... (YouTube Channel ID)',
'channel': '@username veya username',
'channel_url': 'https://www.youtube.com/@username veya https://www.youtube.com/channel/UC...',
'format': 'Atom veya Rss (varsayılan: Atom)',
'max_items': 'Maksimum video sayısı (varsayılan: 50)'
'max_items': 'Maksimum transcript sayısı (varsayılan: 10, maksimum: 100, 20\'şer batch\'ler halinde işlenir)'
}
}), 400
@@ -362,12 +386,12 @@ def info():
'channel': '@username veya username',
'channel_url': 'Full YouTube channel URL',
'format': 'Atom veya Rss (varsayılan: Atom)',
'max_items': 'Maksimum video sayısı (varsayılan: 50)'
'max_items': 'Her istekte işlenecek maksimum transcript sayısı (varsayılan: 10, maksimum: 100, 20\'şer batch\'ler halinde işlenir)'
},
'examples': [
'/?channel_id=UC9h8BDcXwkhZtnqoQJ7PggA&format=Atom',
'/?channel=@tavakfi&format=Rss',
'/?channel_url=https://www.youtube.com/@tavakfi&format=Atom&max_items=100'
'/?channel_url=https://www.youtube.com/@tavakfi&format=Atom&max_items=50'
]
})