Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,16 +19,15 @@ from google.genai import types
|
|
| 19 |
from groq import Groq
|
| 20 |
|
| 21 |
# ▼▼▼ [DNS解決パッチ] ▼▼▼
|
| 22 |
-
# IPv6対応のため、DNS解決ロジックを少し調整
|
| 23 |
_original_getaddrinfo = socket.getaddrinfo
|
| 24 |
|
| 25 |
def patched_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
|
| 26 |
if host and isinstance(host, str) and ("youtube" in host or "youtu.be" in host or "googlevideo" in host):
|
| 27 |
-
# まずは通常解決を試みる
|
| 28 |
try:
|
|
|
|
| 29 |
return _original_getaddrinfo(host, port, family, type, proto, flags)
|
| 30 |
except Exception:
|
| 31 |
-
# 失敗
|
| 32 |
try:
|
| 33 |
url = f"https://8.8.8.8/resolve?name={host}&type=A"
|
| 34 |
r = requests.get(url, timeout=2)
|
|
@@ -46,7 +45,6 @@ socket.getaddrinfo = patched_getaddrinfo
|
|
| 46 |
load_dotenv()
|
| 47 |
|
| 48 |
# === Cookie復元 ===
|
| 49 |
-
# Cookieファイルは生成しますが、今回の主戦略では「Cookieなし」を優先します。
|
| 50 |
try:
|
| 51 |
cookie_content = os.getenv("YOUTUBE_COOKIES")
|
| 52 |
if cookie_content:
|
|
@@ -162,42 +160,42 @@ class StreamWorker:
|
|
| 162 |
'socket_timeout': 30,
|
| 163 |
'retries': 10,
|
| 164 |
'fragment_retries': 10,
|
| 165 |
-
|
| 166 |
-
'force_ipv4': False,
|
| 167 |
-
# ▲▲▲ 修正ここまで ▲▲▲
|
| 168 |
'live_from_start': False,
|
| 169 |
'wait_for_video': (1, 15),
|
| 170 |
'ignoreerrors': True,
|
| 171 |
'js_runtimes': {'node': {}},
|
|
|
|
|
|
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
-
# ▼▼▼ 修正: IP規制回避のための「Cookieなし・特殊クライアント」戦略 ▼▼▼
|
| 175 |
strategies = []
|
| 176 |
|
| 177 |
-
# 1. No Cookie +
|
| 178 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
opts_tv = base_opts.copy()
|
| 180 |
opts_tv['cookiefile'] = None
|
| 181 |
opts_tv['extractor_args'] = {'youtube': {'player_client': ['tv_embedded']}}
|
| 182 |
-
strategies.append(("NoCookie(
|
| 183 |
-
|
| 184 |
-
# 2. No Cookie + Android Music (音楽アプリとして振る舞う)
|
| 185 |
-
opts_am = base_opts.copy()
|
| 186 |
-
opts_am['cookiefile'] = None
|
| 187 |
-
opts_am['extractor_args'] = {'youtube': {'player_client': ['android_music']}}
|
| 188 |
-
strategies.append(("NoCookie(AndroidMusic)", opts_am))
|
| 189 |
|
| 190 |
-
# 3. No Cookie + Android
|
| 191 |
opts_android = base_opts.copy()
|
| 192 |
opts_android['cookiefile'] = None
|
| 193 |
opts_android['extractor_args'] = {'youtube': {'player_client': ['android']}}
|
| 194 |
strategies.append(("NoCookie(Android)", opts_android))
|
| 195 |
-
|
| 196 |
# 4. Cookie + Web (Chrome) - 最後の手段
|
| 197 |
-
# 上記の特殊クライアントが全てダメで、かつCookieがある場合のみ試行
|
| 198 |
if os.path.exists("cookies.txt"):
|
| 199 |
opts_cookie = base_opts.copy()
|
| 200 |
opts_cookie['cookiefile'] = 'cookies.txt'
|
|
|
|
| 201 |
opts_cookie['extractor_args'] = {'youtube': {'impersonate': ['chrome']}}
|
| 202 |
strategies.append(("Cookie(Web)", opts_cookie))
|
| 203 |
|
|
@@ -222,7 +220,7 @@ class StreamWorker:
|
|
| 222 |
continue
|
| 223 |
|
| 224 |
if not success:
|
| 225 |
-
print("❌ 全ての接続方法で失敗しました。
|
| 226 |
self.socketio.emit("error_msg", {"msg": "動画にアクセスできませんでした。(IP規制)"}, room=self.sid)
|
| 227 |
self.is_running = False
|
| 228 |
|
|
|
|
| 19 |
from groq import Groq
|
| 20 |
|
| 21 |
# ▼▼▼ [DNS解決パッチ] ▼▼▼
|
|
|
|
| 22 |
_original_getaddrinfo = socket.getaddrinfo
|
| 23 |
|
| 24 |
def patched_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
|
| 25 |
if host and isinstance(host, str) and ("youtube" in host or "youtu.be" in host or "googlevideo" in host):
|
|
|
|
| 26 |
try:
|
| 27 |
+
# まずはIPv6/IPv4問わず解決を試みる
|
| 28 |
return _original_getaddrinfo(host, port, family, type, proto, flags)
|
| 29 |
except Exception:
|
| 30 |
+
# 失敗時はGoogle DNS(IPv4)へフォールバック
|
| 31 |
try:
|
| 32 |
url = f"https://8.8.8.8/resolve?name={host}&type=A"
|
| 33 |
r = requests.get(url, timeout=2)
|
|
|
|
| 45 |
load_dotenv()
|
| 46 |
|
| 47 |
# === Cookie復元 ===
|
|
|
|
| 48 |
try:
|
| 49 |
cookie_content = os.getenv("YOUTUBE_COOKIES")
|
| 50 |
if cookie_content:
|
|
|
|
| 160 |
'socket_timeout': 30,
|
| 161 |
'retries': 10,
|
| 162 |
'fragment_retries': 10,
|
| 163 |
+
'force_ipv4': False, # IPv6優先
|
|
|
|
|
|
|
| 164 |
'live_from_start': False,
|
| 165 |
'wait_for_video': (1, 15),
|
| 166 |
'ignoreerrors': True,
|
| 167 |
'js_runtimes': {'node': {}},
|
| 168 |
+
# ▼▼▼ 修正: キャッシュ無効化 & User-Agent固定の廃止 ▼▼▼
|
| 169 |
+
'cachedir': False, # キャッシュを使わない(429情報の残留防止)
|
| 170 |
+
# 'http_headers' は削除しました。各クライアントのデフォルトに任せます。
|
| 171 |
}
|
| 172 |
|
|
|
|
| 173 |
strategies = []
|
| 174 |
|
| 175 |
+
# 1. No Cookie + iOS (現在、共有IPで最も通りやすい)
|
| 176 |
+
# iOSアプリとして振る舞います。Cookieは使いません。
|
| 177 |
+
opts_ios = base_opts.copy()
|
| 178 |
+
opts_ios['cookiefile'] = None
|
| 179 |
+
opts_ios['extractor_args'] = {'youtube': {'player_client': ['ios']}}
|
| 180 |
+
strategies.append(("NoCookie(iOS)", opts_ios))
|
| 181 |
+
|
| 182 |
+
# 2. No Cookie + TV Embedded (次に通りやすい)
|
| 183 |
opts_tv = base_opts.copy()
|
| 184 |
opts_tv['cookiefile'] = None
|
| 185 |
opts_tv['extractor_args'] = {'youtube': {'player_client': ['tv_embedded']}}
|
| 186 |
+
strategies.append(("NoCookie(TV)", opts_tv))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
# 3. No Cookie + Android
|
| 189 |
opts_android = base_opts.copy()
|
| 190 |
opts_android['cookiefile'] = None
|
| 191 |
opts_android['extractor_args'] = {'youtube': {'player_client': ['android']}}
|
| 192 |
strategies.append(("NoCookie(Android)", opts_android))
|
| 193 |
+
|
| 194 |
# 4. Cookie + Web (Chrome) - 最後の手段
|
|
|
|
| 195 |
if os.path.exists("cookies.txt"):
|
| 196 |
opts_cookie = base_opts.copy()
|
| 197 |
opts_cookie['cookiefile'] = 'cookies.txt'
|
| 198 |
+
# Web偽装の時だけはUser-Agentを指定した方が良い場合もあるが、yt-dlpに任せる
|
| 199 |
opts_cookie['extractor_args'] = {'youtube': {'impersonate': ['chrome']}}
|
| 200 |
strategies.append(("Cookie(Web)", opts_cookie))
|
| 201 |
|
|
|
|
| 220 |
continue
|
| 221 |
|
| 222 |
if not success:
|
| 223 |
+
print("❌ 全ての接続方法で失敗しました。")
|
| 224 |
self.socketio.emit("error_msg", {"msg": "動画にアクセスできませんでした。(IP規制)"}, room=self.sid)
|
| 225 |
self.is_running = False
|
| 226 |
|