youtube-video-summary / utils /connectivity.py
fmarky's picture
refactor: move libs to utils
d419635
#!/usr/bin/env python3
from __future__ import annotations
from utils.youtube_transcript_alt import (
get_youtube_title_description_tool,
get_youtube_transcript_tool,
extract_video_id,
)
# --- Minimal connectivity + tools test ---------------------------------------
def _connectivity_and_tools_test(video_url: str) -> str:
import socket
import requests
import time
import traceback
logs = []
def log(msg: str):
logs.append(f"[{time.strftime('%H:%M:%S')}] {msg}")
def check_dns(host: str):
try:
addrs = socket.getaddrinfo(host, 443)
uniq = sorted({a[4][0] for a in addrs})
log(f"DNS {host}: OK -> {', '.join(uniq)}")
except Exception as e:
log(f"DNS {host}: ERROR -> {repr(e)}")
def http_get(url: str):
try:
r = requests.get(url, timeout=6)
log(f"GET {url}: {r.status_code}")
except Exception as e:
log(f"GET {url}: ERROR -> {repr(e)}")
# ---- Réseau de base
for host in ["www.youtube.com", "huggingface.co", "api.ipify.org"]:
check_dns(host)
for url in [
"https://api.ipify.org?format=json",
"https://huggingface.co",
"https://www.youtube.com",
]:
http_get(url)
# ---- Outils YouTube (2 appels)
# 1) title/description avec l'URL fournie
try:
td = get_youtube_title_description_tool(video_url)
# pas d'analyse poussée: on confirme juste que ça répond
log(f"get_youtube_title_description_tool: OK (len={len(td)})")
except Exception as e:
log(f"get_youtube_title_description_tool: ERROR -> {repr(e)}")
log("Traceback:\n" + traceback.format_exc())
# 2) transcript avec l'ID extrait (si possible)
try:
vid = extract_video_id(video_url)
except Exception as e:
vid = None
log(f"extract_video_id: ERROR -> {repr(e)}")
if vid:
try:
tr = get_youtube_transcript_tool(vid)
log(f"get_youtube_transcript_tool: OK (len={len(tr)})")
except Exception as e:
log(f"get_youtube_transcript_tool: ERROR -> {repr(e)}")
log("Traceback:\n" + traceback.format_exc())
else:
log("get_youtube_transcript_tool: SKIPPED (no video_id)")
return "\n".join(logs)