DivYonko commited on
Commit
d85beaa
·
1 Parent(s): c34e010

fix: add return None in except, improve scraper diagnostics logging

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -106,6 +106,7 @@ def _get_live_chat_id(video_id: str, api_key: str) -> str | None:
106
  """Fetch the liveChatId for a given video using YouTube Data API v3."""
107
  import urllib.request
108
  import urllib.parse
 
109
 
110
  url = (
111
  "https://www.googleapis.com/youtube/v3/videos"
@@ -114,15 +115,20 @@ def _get_live_chat_id(video_id: str, api_key: str) -> str | None:
114
  try:
115
  with urllib.request.urlopen(url, timeout=10) as resp:
116
  data = json.loads(resp.read())
 
117
  items = data.get("items", [])
118
  if not items:
119
- logger.error("No video found for id=%s", video_id)
120
  return None
121
  live_details = items[0].get("liveStreamingDetails", {})
122
  chat_id = live_details.get("activeLiveChatId")
123
  if not chat_id:
124
- logger.error("No active live chat for video id=%s", video_id)
125
  return chat_id
 
 
 
 
126
  except Exception as exc:
127
  logger.error("Failed to get liveChatId: %s", exc)
128
  return None
@@ -152,6 +158,7 @@ def _fetch_chat_messages(live_chat_id: str, api_key: str, page_token: str | None
152
  messages = data.get("items", [])
153
  next_token = data.get("nextPageToken")
154
  poll_interval = data.get("pollingIntervalMillis", 5000)
 
155
  return messages, next_token, poll_interval
156
  except Exception as exc:
157
  logger.error("Failed to fetch chat messages: %s", exc)
@@ -161,8 +168,11 @@ def _fetch_chat_messages(live_chat_id: str, api_key: str, page_token: str | None
161
  def _scraper_thread_fn(video_id: str, redis_key: str, stop_event: threading.Event) -> None:
162
  """Background thread — scrapes live chat via YouTube Data API v3."""
163
  api_key = os.getenv("YOUTUBE_API_KEY", "")
 
164
  if not api_key:
165
- logger.error("YOUTUBE_API_KEY env var not set. Cannot start scraper.")
 
 
166
  return
167
 
168
  logger.info("Scraper thread starting — video=%s key=%s", video_id, redis_key)
 
106
  """Fetch the liveChatId for a given video using YouTube Data API v3."""
107
  import urllib.request
108
  import urllib.parse
109
+ import urllib.error
110
 
111
  url = (
112
  "https://www.googleapis.com/youtube/v3/videos"
 
115
  try:
116
  with urllib.request.urlopen(url, timeout=10) as resp:
117
  data = json.loads(resp.read())
118
+ logger.info("YouTube API response for %s: %s", video_id, json.dumps(data)[:500])
119
  items = data.get("items", [])
120
  if not items:
121
+ logger.error("No video found for id=%s (items empty). Check if video ID is correct and API key is valid.", video_id)
122
  return None
123
  live_details = items[0].get("liveStreamingDetails", {})
124
  chat_id = live_details.get("activeLiveChatId")
125
  if not chat_id:
126
+ logger.error("No activeLiveChatId for video=%s. liveStreamingDetails=%s", video_id, live_details)
127
  return chat_id
128
+ except urllib.error.HTTPError as exc:
129
+ body = exc.read().decode("utf-8", errors="replace")[:500]
130
+ logger.error("HTTP %d from YouTube API for video=%s: %s", exc.code, video_id, body)
131
+ return None
132
  except Exception as exc:
133
  logger.error("Failed to get liveChatId: %s", exc)
134
  return None
 
158
  messages = data.get("items", [])
159
  next_token = data.get("nextPageToken")
160
  poll_interval = data.get("pollingIntervalMillis", 5000)
161
+ logger.info("Fetched %d chat messages (nextPageToken=%s)", len(messages), bool(next_token))
162
  return messages, next_token, poll_interval
163
  except Exception as exc:
164
  logger.error("Failed to fetch chat messages: %s", exc)
 
168
  def _scraper_thread_fn(video_id: str, redis_key: str, stop_event: threading.Event) -> None:
169
  """Background thread — scrapes live chat via YouTube Data API v3."""
170
  api_key = os.getenv("YOUTUBE_API_KEY", "")
171
+ logger.info("YOUTUBE_API_KEY present: %s (length=%d)", bool(api_key), len(api_key))
172
  if not api_key:
173
+ msg = "YOUTUBE_API_KEY env var not set. Cannot start scraper."
174
+ logger.error(msg)
175
+ _META["scraper_error"] = msg
176
  return
177
 
178
  logger.info("Scraper thread starting — video=%s key=%s", video_id, redis_key)