Detect monitored accounts that are live as guests inside another host's room (linker_map)
Browse files
app.py
CHANGED
|
@@ -280,6 +280,8 @@ def get_json(url,params,user=None):
|
|
| 280 |
_observation_cache={}
|
| 281 |
_observation_locks={}
|
| 282 |
_observation_guard=threading.Lock()
|
|
|
|
|
|
|
| 283 |
|
| 284 |
def _as_dict(x):
|
| 285 |
return x if isinstance(x, dict) else {}
|
|
@@ -295,9 +297,43 @@ def observe_session(user,force=False):
|
|
| 295 |
cached=_observation_cache.get(user)
|
| 296 |
if cached and not force and time.monotonic()-cached[0]<15:return cached[1]
|
| 297 |
result=_observe_session(user)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
_observation_cache[user]=(time.monotonic(),result)
|
| 299 |
return result
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
def _observe_session(user):
|
| 302 |
"""TikTok room lookup. Null liveRoom/user/data must be Offline, not a crash.
|
| 303 |
|
|
@@ -308,6 +344,7 @@ def _observe_session(user):
|
|
| 308 |
j=_as_dict(get_json('https://www.tiktok.com/api-live/user/room',{'aid':1988,'uniqueId':user,'sourceType':54,'staleTime':0},user))
|
| 309 |
d=_as_dict(j.get('data')); live=_as_dict(d.get('liveRoom')); person=_as_dict(d.get('user'))
|
| 310 |
status=live.get('status')
|
|
|
|
| 311 |
room=str(person.get('roomId') or person.get('room_id') or live.get('roomId') or live.get('room_id') or '')
|
| 312 |
nickname=str(person.get('nickname') or live.get('nickname') or '').strip()
|
| 313 |
live_title=str(live.get('title') or '').strip()
|
|
|
|
| 280 |
_observation_cache={}
|
| 281 |
_observation_locks={}
|
| 282 |
_observation_guard=threading.Lock()
|
| 283 |
+
_uid_cache={} # user -> numeric TikTok id (من user/room)
|
| 284 |
+
_guest_logged={} # user -> غرفة المذيع المضيف التي سُجّل الدخول إليها
|
| 285 |
|
| 286 |
def _as_dict(x):
|
| 287 |
return x if isinstance(x, dict) else {}
|
|
|
|
| 297 |
cached=_observation_cache.get(user)
|
| 298 |
if cached and not force and time.monotonic()-cached[0]<15:return cached[1]
|
| 299 |
result=_observe_session(user)
|
| 300 |
+
if result.get('status')!='live':
|
| 301 |
+
# الحساب قد يكون ضيفًا داخل بث مذيع آخر: غرفته الخاصة تبقى «منتهية» بينما البث الحي في غرفة الضيوف
|
| 302 |
+
try:
|
| 303 |
+
guest=_guest_observation(user)
|
| 304 |
+
except Exception:
|
| 305 |
+
guest=None
|
| 306 |
+
if guest:result=guest
|
| 307 |
_observation_cache[user]=(time.monotonic(),result)
|
| 308 |
return result
|
| 309 |
|
| 310 |
+
def _guest_observation(user):
|
| 311 |
+
"""Account is not live in its own room. Check whether TikTok lists it in the linker_map
|
| 312 |
+
of another monitored host that is live right now; if so the live show is that room."""
|
| 313 |
+
uid=str(_uid_cache.get(user) or '')
|
| 314 |
+
if not uid.isdigit():return None
|
| 315 |
+
for other in enabled_accounts():
|
| 316 |
+
if other==user:continue
|
| 317 |
+
cached=_observation_cache.get(other)
|
| 318 |
+
if not cached or (cached[1] or {}).get('status')!='live':continue
|
| 319 |
+
host_room=str((cached[1] or {}).get('room_id') or '')
|
| 320 |
+
if not host_room.isdigit() or host_room=='0':continue
|
| 321 |
+
j=_as_dict(get_json('https://webcast.tiktok.com/webcast/room/info/',{'aid':1988,'room_id':host_room},other))
|
| 322 |
+
info=_as_dict(j.get('data'))
|
| 323 |
+
if str(info.get('status'))!='2':continue
|
| 324 |
+
if str(_as_dict(info.get('owner')).get('id_str') or info.get('owner_user_id') or '')==uid:continue
|
| 325 |
+
linkers=info.get('linker_map')
|
| 326 |
+
if not linkers or uid not in json.dumps(linkers,ensure_ascii=False):continue
|
| 327 |
+
sources=rank_sources(_as_dict(info.get('stream_url')))
|
| 328 |
+
if not sources:continue
|
| 329 |
+
if _guest_logged.get(user)!=host_room:
|
| 330 |
+
_guest_logged[user]=host_room
|
| 331 |
+
log(user,'[GUEST] @%s داخل بث @%s كضيف؛ نرصد غرفة المذيع المضيف'%(user,other))
|
| 332 |
+
return {'status':'live','room_id':host_room,'sources':sources,'nickname':user,
|
| 333 |
+
'live_title':str(info.get('title') or '').strip(),'guest_of':other}
|
| 334 |
+
_guest_logged.pop(user,None)
|
| 335 |
+
return None
|
| 336 |
+
|
| 337 |
def _observe_session(user):
|
| 338 |
"""TikTok room lookup. Null liveRoom/user/data must be Offline, not a crash.
|
| 339 |
|
|
|
|
| 344 |
j=_as_dict(get_json('https://www.tiktok.com/api-live/user/room',{'aid':1988,'uniqueId':user,'sourceType':54,'staleTime':0},user))
|
| 345 |
d=_as_dict(j.get('data')); live=_as_dict(d.get('liveRoom')); person=_as_dict(d.get('user'))
|
| 346 |
status=live.get('status')
|
| 347 |
+
if str(person.get('id') or '').isdigit():_uid_cache[user]=str(person.get('id'))
|
| 348 |
room=str(person.get('roomId') or person.get('room_id') or live.get('roomId') or live.get('room_id') or '')
|
| 349 |
nickname=str(person.get('nickname') or live.get('nickname') or '').strip()
|
| 350 |
live_title=str(live.get('title') or '').strip()
|