110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Truth: player API for Studio video IDs + channel /live via youtubei player."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
REMOTE = r'''
|
|
import json, re, urllib.request, socket
|
|
|
|
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()
|
|
|
|
def player(vid):
|
|
body=json.dumps({
|
|
"context":{"client":{"clientName":"WEB","clientVersion":"2.20250301.00.00","hl":"en","gl":"US"}},
|
|
"videoId":vid
|
|
}).encode()
|
|
req=urllib.request.Request(
|
|
"https://www.youtube.com/youtubei/v1/player?prettyPrint=false",
|
|
data=body,
|
|
headers={"Content-Type":"application/json","User-Agent":"Mozilla/5.0","X-YouTube-Client-Name":"1","X-YouTube-Client-Version":"2.20250301.00.00"},
|
|
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 "")[:60],
|
|
"channel": (vd.get("author") or "")[:40],
|
|
"channelId": vd.get("channelId"),
|
|
"isLive": vd.get("isLive"),
|
|
"isLiveContent": vd.get("isLiveContent"),
|
|
"liveBroadcastDetails": mf.get("liveBroadcastDetails"),
|
|
"status": play.get("status"),
|
|
"reason": (play.get("reason") or "")[:80],
|
|
}
|
|
|
|
vids=[
|
|
"zblaCyR1AY8","XW-3kA3XdfI","aG0zIkmCJ2E","1cQY6jHytxQ","L1sF_kZvzIk",
|
|
]
|
|
print("proxy", proxy or "direct")
|
|
for vid in vids:
|
|
try:
|
|
print(vid, json.dumps(player(vid), ensure_ascii=False))
|
|
except Exception as e:
|
|
print(vid, "ERR", e)
|
|
|
|
# also scrape watch?v= pages for isLiveNow
|
|
print("--- watch html ---")
|
|
for vid in vids:
|
|
try:
|
|
req=urllib.request.Request("https://www.youtube.com/watch?v="+vid, headers={"User-Agent":"Mozilla/5.0","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))
|
|
print(vid, "isLiveNow", now, "isLive", live, "len", len(html))
|
|
except Exception as e:
|
|
print(vid, "ERR", e)
|
|
|
|
# check if youtube.mjs has /live resolve
|
|
print("--- youtube.mjs snippet ---")
|
|
import os
|
|
p="/opt/douyinWEB/youtube-svc/src/youtube.mjs"
|
|
txt=open(p,encoding="utf-8",errors="ignore").read()
|
|
print("has /live resolve", "/live" in txt and "getBasicInfo" in txt)
|
|
# print function around live
|
|
idx=txt.find("channel/${")
|
|
print("file size", len(txt))
|
|
for key in ["resolveChannelLive","/live","getBasicInfo","isLiveNow"]:
|
|
print(key, txt.count(key))
|
|
'''
|
|
|
|
|
|
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[-500:])
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(22, "root", "Xiao4669805", "vps")
|