@danhachuel/thunderbolt 0.7.6 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/main.py CHANGED
@@ -2625,17 +2625,19 @@ def _channel_average_video_time(channel: dict[str, Any], *, prompt_master: str =
2625
2625
 
2626
2626
 
2627
2627
  def _refresh_youtube_channel_metrics(channel: dict[str, Any], youtube: YouTubeAdapter) -> tuple[bool, str]:
2628
- result = youtube.fetch_channel_public(str(channel.get("url") or channel.get("handle") or ""))
2628
+ channel_ref = str(channel.get("youtube_channel_id") or channel.get("youtube_id") or channel.get("url") or channel.get("handle") or "")
2629
+ result = youtube.fetch_channel(channel_ref) if youtube.api_key else youtube.fetch_channel_public(channel_ref)
2629
2630
  if not result.ok or not isinstance(result.data, dict):
2630
2631
  return False, result.message
2631
2632
  data = result.data
2632
- update_channel(str(channel["id"]), {
2633
- "subscriber_count": data.get("subscriber_count"),
2634
- "video_count": data.get("video_count"),
2635
- "view_count": data.get("view_count"),
2633
+ updates = {
2636
2634
  "metrics_source": data.get("metrics_source", "youtube_public_page"),
2637
2635
  "last_public_lookup_at": data.get("last_public_lookup_at", now()),
2638
- })
2636
+ }
2637
+ for field in ("youtube_id", "name", "handle", "url", "thumbnail_url", "subscriber_count", "video_count", "view_count"):
2638
+ if data.get(field) is not None and data.get(field) != "":
2639
+ updates[field] = data[field]
2640
+ update_channel(str(channel["id"]), updates)
2639
2641
  return True, "Métricas YouTube actualizadas."
2640
2642
 
2641
2643
 
@@ -81,7 +81,7 @@ def _parse_public_count(value: Any) -> int | None:
81
81
  text = _text_from_node(value).lower().replace(" ", "")
82
82
  if not text:
83
83
  return None
84
- match = re.search(r"([0-9][0-9.,]*)(mil|milhões|mi|bi|[kmb])?", text)
84
+ match = re.search(r"([0-9][0-9.,]*)(milhões|mil|mi|bi|[kmb])?", text)
85
85
  if not match:
86
86
  return None
87
87
  number = match.group(1)
@@ -89,6 +89,16 @@ def _parse_public_count(value: Any) -> int | None:
89
89
  if suffix == "milhões":
90
90
  suffix = "m"
91
91
  try:
92
+ if suffix:
93
+ # YouTube pode devolver "193 mil", "193,4 mil" ou "1.234 mil".
94
+ # Um ponto seguido de três algarismos é separador de milhares;
95
+ # vírgula/ponto com 1–2 algarismos é a parte decimal.
96
+ if "." in number and "," in number:
97
+ number = number.replace(".", "").replace(",", ".")
98
+ elif "," in number:
99
+ number = number.replace(",", ".")
100
+ elif re.fullmatch(r"\d+\.\d{3}", number):
101
+ number = number.replace(".", "")
92
102
  if suffix in {"k", "mil"}:
93
103
  return int(float(number.replace(",", ".")) * 1_000)
94
104
  if suffix in {"m", "mi"}:
@@ -412,6 +422,7 @@ class YouTubeAdapter:
412
422
  break
413
423
  subscriber_value = _find_first_key(initial_data, "subscriberCountText")
414
424
  video_value = _find_first_key(initial_data, "videoCountText")
425
+ view_value = _find_first_key(initial_data, "viewCountText")
415
426
  data = {
416
427
  "youtube_id": youtube_id or feed.get("youtube_id", ""),
417
428
  "name": title,
@@ -421,7 +432,7 @@ class YouTubeAdapter:
421
432
  "thumbnail_url": thumbnail_url,
422
433
  "subscriber_count": _parse_public_count(subscriber_value),
423
434
  "video_count": _parse_public_count(video_value) or feed.get("video_count"),
424
- "view_count": None,
435
+ "view_count": _parse_public_count(view_value),
425
436
  "metrics_source": "youtube_public_page" if (page_title or page_description or page_thumbnail) else ("youtube_public_feed" if feed.get("name") else "youtube_public_page"),
426
437
  "public_lookup": True,
427
438
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danhachuel/thunderbolt",
3
- "version": "0.7.06",
3
+ "version": "0.7.07",
4
4
  "description": "Thunderbolt — interface local para operação de canais faceless e motor MoneyPrinterTurbo",
5
5
  "license": "MIT",
6
6
  "main": "scripts/cli.mjs",