140 lines
5.6 KiB
Python
140 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Android/TV Innertube player — bypass WEB bot wall for live check."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
REMOTE = r'''
|
|
import json, urllib.request, socket, re
|
|
|
|
proxy=""
|
|
s=socket.socket(); s.settimeout(0.4)
|
|
try:
|
|
s.connect(("127.0.0.1",7890)); proxy="http://127.0.0.1:7890"
|
|
except Exception:
|
|
proxy=""
|
|
finally:
|
|
s.close()
|
|
|
|
def opener(px):
|
|
handlers=[]
|
|
if px:
|
|
handlers.append(urllib.request.ProxyHandler({"http":px,"https":px}))
|
|
return urllib.request.build_opener(*handlers) if handlers else urllib.request.build_opener()
|
|
|
|
CLIENTS={
|
|
"WEB": {"clientName":"WEB","clientVersion":"2.20250301.00.00","hl":"en","gl":"US"},
|
|
"ANDROID": {"clientName":"ANDROID","clientVersion":"19.28.35","androidSdkVersion":30,"hl":"en","gl":"US"},
|
|
"TVHTML5": {"clientName":"TVHTML5_SIMPLY_EMBEDDED_PLAYER","clientVersion":"2.0","hl":"en","gl":"US"},
|
|
"IOS": {"clientName":"IOS","clientVersion":"19.29.1","deviceModel":"iPhone14,3","hl":"en","gl":"US"},
|
|
}
|
|
|
|
def player(vid, client_key):
|
|
ctx={"client":CLIENTS[client_key]}
|
|
body=json.dumps({"context":ctx,"videoId":vid}).encode()
|
|
# api key for android often needed; try without first
|
|
url="https://www.youtube.com/youtubei/v1/player?prettyPrint=false"
|
|
if client_key=="ANDROID":
|
|
url="https://youtubei.googleapis.com/youtubei/v1/player?key=AIzaSyA8eiZmM1FaDVz_Vx_WRwVIgm1QAo7Lrm4&prettyPrint=false"
|
|
req=urllib.request.Request(url, data=body, headers={
|
|
"Content-Type":"application/json",
|
|
"User-Agent":"com.google.android.youtube/19.28.35 (Linux; U; Android 11) gzip" if client_key=="ANDROID" else "Mozilla/5.0",
|
|
"X-YouTube-Client-Name":"3" if client_key=="ANDROID" else "1",
|
|
"X-YouTube-Client-Version":CLIENTS[client_key]["clientVersion"],
|
|
}, method="POST")
|
|
raw=opener(proxy).open(req, timeout=25).read().decode("utf-8","ignore")
|
|
obj=json.loads(raw)
|
|
vd=obj.get("videoDetails") or {}
|
|
mf=((obj.get("microformat") or {}).get("playerMicroformatRenderer") or {})
|
|
play=obj.get("playabilityStatus") or {}
|
|
return {
|
|
"title": (vd.get("title") or "")[:50],
|
|
"author": (vd.get("author") or "")[:30],
|
|
"cid": vd.get("channelId"),
|
|
"isLive": vd.get("isLive"),
|
|
"isLiveContent": vd.get("isLiveContent"),
|
|
"liveNow": (mf.get("liveBroadcastDetails") or {}).get("isLiveNow"),
|
|
"status": play.get("status"),
|
|
"reason": (play.get("reason") or "")[:60],
|
|
}
|
|
|
|
vids={
|
|
"asian_PF16":"PF16pG4VmSk",
|
|
"asian_1cQY":"1cQY6jHytxQ",
|
|
"hack_XW":"XW-3kA3XdfI",
|
|
"nomad_zbla":"zblaCyR1AY8",
|
|
"nomad_aG0z":"aG0zIkmCJ2E",
|
|
"vlog_L1sF":"L1sF_kZvzIk",
|
|
}
|
|
print("proxy", proxy or "direct")
|
|
for name,vid in vids.items():
|
|
for ck in ["ANDROID","WEB"]:
|
|
try:
|
|
r=player(vid, ck)
|
|
print(name, vid, ck, json.dumps(r, ensure_ascii=False))
|
|
except Exception as e:
|
|
print(name, vid, ck, "ERR", type(e).__name__, str(e)[:120])
|
|
|
|
# browse channel for live via android
|
|
print("--- android browse ---")
|
|
for cid in ["UCXFErW9DeE8EXrInlnyCnVw","UCjQ8jnBm2N4FGRowNXcXThw","UCYnwXkCMmVFf8Vr6MlWb-Jw","UCf-uYs6f5yg3iWylnHkIKug"]:
|
|
body=json.dumps({
|
|
"context":{"client":CLIENTS["ANDROID"]},
|
|
"browseId":cid,
|
|
"params":"EgZzZXR0aW5ncw%3D%3D"
|
|
}).encode()
|
|
# live tab params from yt: EgdzZXR0aW5ncw%3D%3D is settings; live is often EgJwaQ%3D%3D or similar
|
|
# try resolve /live via get_transcript no - use search
|
|
try:
|
|
# use player for channel live redirect via webpage android
|
|
url="https://m.youtube.com/channel/%s/live" % cid
|
|
req=urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0 (Linux; Android 11) AppleWebKit/537.36 Mobile Safari/537.36","Accept-Language":"en-US"})
|
|
html=opener(proxy).open(req, timeout=25).read().decode("utf-8","ignore")
|
|
now=bool(re.search(r'"isLiveNow"\s*:\s*true', html, re.I))
|
|
live=bool(re.search(r'"isLive"\s*:\s*true', html, re.I))
|
|
status=""
|
|
m=re.search(r'"status"\s*:\s*"([A-Z_]+)"', html)
|
|
if m: status=m.group(1)
|
|
# extract videoId near player
|
|
vid=""
|
|
m=re.search(r'ytInitialPlayerResponse\s*=\s*', html)
|
|
if m:
|
|
try:
|
|
obj,_=json.JSONDecoder().raw_decode(html[m.end():])
|
|
vd=obj.get("videoDetails") or {}
|
|
vid=vd.get("videoId") or ""
|
|
now=bool((obj.get("microformat") or {}).get("playerMicroformatRenderer",{}).get("liveBroadcastDetails",{}).get("isLiveNow")) or now
|
|
play=(obj.get("playabilityStatus") or {}).get("status")
|
|
print(cid[:12], "m.live", "vid", vid, "isLiveNow", now, "isLive_re", live, "play", play, "title", (vd.get("title") or "")[:40])
|
|
continue
|
|
except Exception as e:
|
|
print(cid[:12], "parse", e)
|
|
print(cid[:12], "m.live", "no_player", "isLiveNow", now, "isLive", live, "status", status, "len", len(html), "canon", (re.search(r'rel="canonical" href="([^"]+)"', html) or type("x",(object,),{"group":lambda s,i:""})()).group(1)[:60])
|
|
except Exception as e:
|
|
print(cid[:12], "ERR", e)
|
|
'''
|
|
|
|
|
|
def run(port, user, pwd, label):
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("107.173.30.245", port=port, username=user, password=pwd, timeout=25)
|
|
stdin, stdout, stderr = c.exec_command("python3 -", timeout=180)
|
|
stdin.write(REMOTE)
|
|
stdin.channel.shutdown_write()
|
|
print("########", label, "########")
|
|
print(stdout.read().decode("utf-8", "replace"))
|
|
err = stderr.read().decode("utf-8", "replace")
|
|
if err.strip():
|
|
print("STDERR", err[-600:])
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(22, "root", "Xiao4669805", "vps")
|