Guest detection via PirateTok host-room events (GUEST_WATCH_HOSTS)
Browse files- guest_watch.py: keyless WebSocket watcher on host rooms (piratetok-live-py 0.3.0).
A guest is active while its userId keeps appearing in link_mic_method events.
- app.py: when an enabled account is offline in its own room but is a guest on a
watched host, relay the host stream and show LIVE on host. Switch back when the
guest signal expires. Own-room behaviour is unchanged otherwise.
- Dockerfile: GUEST_WATCH_HOSTS=talolil, GUEST_ACTIVE_SECONDS=120.
- test_guest_watch.py: unit checks.
- Dockerfile +5 -3
- app.py +41 -6
- guest_watch.py +142 -0
- requirements.txt +1 -0
- test_guest_watch.py +44 -0
Dockerfile
CHANGED
|
@@ -5,8 +5,8 @@ RUN ffmpeg -hide_banner -h protocol=rtmps 2>&1 | grep -q rtmp_enhanced_codecs
|
|
| 5 |
WORKDIR /app
|
| 6 |
COPY requirements.txt .
|
| 7 |
RUN pip install --no-cache-dir -U -r requirements.txt
|
| 8 |
-
COPY app.py relay.py youtube_api.py youtube_manager.py flv_bridge.py stream_monitor.py tiktok_auth.py ingest.py index.html ./
|
| 9 |
-
COPY verify.sh verify_tokens.py probe_live.py test_watchdog.py test_lifecycle.py test_relay.py test_source_reconnect.py test_ingest_routes.py test_transition_ingest.py test_concurrency.py test_flv_bridge.py test_adaptive_format.py test_regressions.py test_completion.py test_channel_switch.py test_dashboard.js ./
|
| 10 |
|
| 11 |
# TikTok Live Monitor → Direct Relay → YouTube. No local video files.
|
| 12 |
ENV TZ=Asia/Baghdad \
|
|
@@ -60,7 +60,9 @@ ENV TZ=Asia/Baghdad \
|
|
| 60 |
HLS_START_INDEX=-3 \
|
| 61 |
PORT=7860 \
|
| 62 |
PYTHONUNBUFFERED=1 \
|
| 63 |
-
YOUTUBE_VIDEO_MODE=original
|
|
|
|
|
|
|
| 64 |
|
| 65 |
EXPOSE 7860
|
| 66 |
CMD ["sh", "-c", "exec uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860} --timeout-graceful-shutdown 65 --no-access-log"]
|
|
|
|
| 5 |
WORKDIR /app
|
| 6 |
COPY requirements.txt .
|
| 7 |
RUN pip install --no-cache-dir -U -r requirements.txt
|
| 8 |
+
COPY app.py relay.py youtube_api.py youtube_manager.py flv_bridge.py stream_monitor.py tiktok_auth.py ingest.py guest_watch.py index.html ./
|
| 9 |
+
COPY verify.sh verify_tokens.py probe_live.py test_guest_watch.py test_watchdog.py test_lifecycle.py test_relay.py test_source_reconnect.py test_ingest_routes.py test_transition_ingest.py test_concurrency.py test_flv_bridge.py test_adaptive_format.py test_regressions.py test_completion.py test_channel_switch.py test_dashboard.js ./
|
| 10 |
|
| 11 |
# TikTok Live Monitor → Direct Relay → YouTube. No local video files.
|
| 12 |
ENV TZ=Asia/Baghdad \
|
|
|
|
| 60 |
HLS_START_INDEX=-3 \
|
| 61 |
PORT=7860 \
|
| 62 |
PYTHONUNBUFFERED=1 \
|
| 63 |
+
YOUTUBE_VIDEO_MODE=original \
|
| 64 |
+
GUEST_WATCH_HOSTS=talolil \
|
| 65 |
+
GUEST_ACTIVE_SECONDS=120
|
| 66 |
|
| 67 |
EXPOSE 7860
|
| 68 |
CMD ["sh", "-c", "exec uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860} --timeout-graceful-shutdown 65 --no-access-log"]
|
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from pydantic import BaseModel
|
|
| 9 |
from curl_cffi import requests
|
| 10 |
import tiktok_auth
|
| 11 |
from ingest import hub, env_rtmp_destination, rtmp_configured, format_session_title, APP_VERSION, max_concurrent_relays
|
|
|
|
| 12 |
|
| 13 |
MSG=('تيك توك يحجب روابط البث لهذا الحساب (غرفة مقيدة بعمر 18+ أو جلسة تحتاج تحققًا)؛ '
|
| 14 |
'الصق كوكيز الجلسة الكامل في لوحة «توكن تيك توك» ثم أعد المحاولة')
|
|
@@ -454,6 +455,21 @@ _direct_backoff={}
|
|
| 454 |
_direct_unstable={}
|
| 455 |
_direct_guard=threading.RLock()
|
| 456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
def _oauth_automation_on():
|
| 458 |
try:return bool(youtube.journal.read().get('enabled'))
|
| 459 |
except Exception:return False
|
|
@@ -495,9 +511,15 @@ def _direct_loop(user, dest, stop):
|
|
| 495 |
log(user,'[MONITOR] @%s observe failed during relay: %s'%(user, msg[:120]))
|
| 496 |
if stop.wait(min(30, _direct_backoff.get(user, 2))):return
|
| 497 |
continue
|
|
|
|
| 498 |
if obs.get('status')!='live':
|
| 499 |
-
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
if room and obs.get('room_id') and obs['room_id']!=room:
|
| 502 |
log(user,'[MONITOR] @%s room changed; clean source transition'%user)
|
| 503 |
_direct_unstable.pop(user, None)
|
|
@@ -553,7 +575,7 @@ def _direct_loop(user, dest, stop):
|
|
| 553 |
with _direct_guard:_direct_handles.pop(user, None)
|
| 554 |
log(user,'[MONITOR] @%s waiting'%user)
|
| 555 |
|
| 556 |
-
def _maybe_start_direct(user, obs):
|
| 557 |
"""Start RTMPS relay when OAuth automation is off but an ingest destination exists."""
|
| 558 |
if _oauth_automation_on():
|
| 559 |
return
|
|
@@ -579,7 +601,7 @@ def _maybe_start_direct(user, obs):
|
|
| 579 |
th=threading.Thread(target=_direct_loop, args=(user, dest, stop), daemon=True, name='direct-'+user)
|
| 580 |
hub.update(user, thread=th, stop=stop)
|
| 581 |
with _direct_guard:
|
| 582 |
-
_direct_handles[user]={'thread':th,'stop':stop,'sender':None}
|
| 583 |
log(user,'[RELAY] @%s starting (env RTMPS, OAuth not required)'%user)
|
| 584 |
update(user, state='STARTING', status='بدء Relay')
|
| 585 |
th.start()
|
|
@@ -778,6 +800,11 @@ def _check_account(user):
|
|
| 778 |
out=throttle_failure(user,msg)
|
| 779 |
if out:log(user,'[MONITOR] @%s %s'%(user,out))
|
| 780 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
if obs.get('status')!='live':
|
| 782 |
prev=None
|
| 783 |
with lock:prev=(tasks.get(user) or {}).get('state')
|
|
@@ -821,8 +848,15 @@ def _check_account(user):
|
|
| 821 |
if top:log(user,'[SOURCE] @%s selected %s'%(user,brief))
|
| 822 |
if prev_room and prev_room!=obs.get('room_id') and relaying:
|
| 823 |
log(user,'[MONITOR] @%s session/room changed; source will be refreshed'%user)
|
| 824 |
-
|
| 825 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
if _oauth_automation_on() and not hub.busy(user) and (obs.get('sources') or []):
|
| 827 |
try:
|
| 828 |
youtube.nudge_live(user, obs)
|
|
@@ -1082,6 +1116,7 @@ def startup():
|
|
| 1082 |
except ValueError:continue
|
| 1083 |
tasks[u]=_blank_task();tasks[u]['enabled']=bool(enabled)
|
| 1084 |
if not enabled:tasks[u]['status']='متوقف'
|
|
|
|
| 1085 |
threading.Thread(target=monitor_loop,daemon=True,name='global-monitor').start()
|
| 1086 |
youtube.restore()
|
| 1087 |
log('system','[MONITOR] waiting')
|
|
|
|
| 9 |
from curl_cffi import requests
|
| 10 |
import tiktok_auth
|
| 11 |
from ingest import hub, env_rtmp_destination, rtmp_configured, format_session_title, APP_VERSION, max_concurrent_relays
|
| 12 |
+
from guest_watch import GuestWatch, parse_hosts
|
| 13 |
|
| 14 |
MSG=('تيك توك يحجب روابط البث لهذا الحساب (غرفة مقيدة بعمر 18+ أو جلسة تحتاج تحققًا)؛ '
|
| 15 |
'الصق كوكيز الجلسة الكامل في لوحة «توكن تيك توك» ثم أعد المحاولة')
|
|
|
|
| 455 |
_direct_unstable={}
|
| 456 |
_direct_guard=threading.RLock()
|
| 457 |
|
| 458 |
+
GUEST=GuestWatch(parse_hosts(os.getenv('GUEST_WATCH_HOSTS','')), log=log)
|
| 459 |
+
|
| 460 |
+
def _guest_source(user, own):
|
| 461 |
+
"""(obs, host) when @user is a guest on a watched host and that host is live; else None."""
|
| 462 |
+
if not GUEST.running:return None
|
| 463 |
+
host=GUEST.active_host(user)
|
| 464 |
+
if not host:return None
|
| 465 |
+
try:hobs=observe_session(host, force=True)
|
| 466 |
+
except Exception as e:
|
| 467 |
+
log(user,'[GUEST] @%s host observe failed: %s'%(user,type(e).__name__))
|
| 468 |
+
return None
|
| 469 |
+
if hobs.get('status')!='live' or not hobs.get('sources'):return None
|
| 470 |
+
nick=str((own or {}).get('nickname') or hobs.get('nickname') or user)
|
| 471 |
+
return {**hobs,'nickname':nick}, host
|
| 472 |
+
|
| 473 |
def _oauth_automation_on():
|
| 474 |
try:return bool(youtube.journal.read().get('enabled'))
|
| 475 |
except Exception:return False
|
|
|
|
| 511 |
log(user,'[MONITOR] @%s observe failed during relay: %s'%(user, msg[:120]))
|
| 512 |
if stop.wait(min(30, _direct_backoff.get(user, 2))):return
|
| 513 |
continue
|
| 514 |
+
src_host=''
|
| 515 |
if obs.get('status')!='live':
|
| 516 |
+
gs=_guest_source(user, obs)
|
| 517 |
+
if not gs:
|
| 518 |
+
log(user,'[MONITOR] @%s OFFLINE'%user)
|
| 519 |
+
return
|
| 520 |
+
obs,src_host=gs
|
| 521 |
+
with _direct_guard:
|
| 522 |
+
if user in _direct_handles:_direct_handles[user]['src']=src_host
|
| 523 |
if room and obs.get('room_id') and obs['room_id']!=room:
|
| 524 |
log(user,'[MONITOR] @%s room changed; clean source transition'%user)
|
| 525 |
_direct_unstable.pop(user, None)
|
|
|
|
| 575 |
with _direct_guard:_direct_handles.pop(user, None)
|
| 576 |
log(user,'[MONITOR] @%s waiting'%user)
|
| 577 |
|
| 578 |
+
def _maybe_start_direct(user, obs, src_host=''):
|
| 579 |
"""Start RTMPS relay when OAuth automation is off but an ingest destination exists."""
|
| 580 |
if _oauth_automation_on():
|
| 581 |
return
|
|
|
|
| 601 |
th=threading.Thread(target=_direct_loop, args=(user, dest, stop), daemon=True, name='direct-'+user)
|
| 602 |
hub.update(user, thread=th, stop=stop)
|
| 603 |
with _direct_guard:
|
| 604 |
+
_direct_handles[user]={'thread':th,'stop':stop,'sender':None,'src':src_host}
|
| 605 |
log(user,'[RELAY] @%s starting (env RTMPS, OAuth not required)'%user)
|
| 606 |
update(user, state='STARTING', status='بدء Relay')
|
| 607 |
th.start()
|
|
|
|
| 800 |
out=throttle_failure(user,msg)
|
| 801 |
if out:log(user,'[MONITOR] @%s %s'%(user,out))
|
| 802 |
return
|
| 803 |
+
src_host=''
|
| 804 |
+
if obs.get('status')!='live':
|
| 805 |
+
gs=_guest_source(user, obs)
|
| 806 |
+
if gs:
|
| 807 |
+
obs,src_host=gs
|
| 808 |
if obs.get('status')!='live':
|
| 809 |
prev=None
|
| 810 |
with lock:prev=(tasks.get(user) or {}).get('state')
|
|
|
|
| 848 |
if top:log(user,'[SOURCE] @%s selected %s'%(user,brief))
|
| 849 |
if prev_room and prev_room!=obs.get('room_id') and relaying:
|
| 850 |
log(user,'[MONITOR] @%s session/room changed; source will be refreshed'%user)
|
| 851 |
+
if relaying:
|
| 852 |
+
with _direct_guard:cur_src=(_direct_handles.get(user) or {}).get('src','')
|
| 853 |
+
if cur_src!=src_host:
|
| 854 |
+
_stop_direct(user, 'guest source switch')
|
| 855 |
+
log(user,'[GUEST] @%s source switch: %s'%(user, ('guest on @%s'%src_host) if src_host else 'own room'))
|
| 856 |
+
if src_host:
|
| 857 |
+
log(user,'[GUEST] @%s is a guest on @%s; relaying host stream'%(user,src_host))
|
| 858 |
+
_set_state(user,'LIVE','LIVE' if not src_host else 'LIVE · ضيف عند @%s'%src_host,**extra)
|
| 859 |
+
_maybe_start_direct(user, obs, src_host)
|
| 860 |
if _oauth_automation_on() and not hub.busy(user) and (obs.get('sources') or []):
|
| 861 |
try:
|
| 862 |
youtube.nudge_live(user, obs)
|
|
|
|
| 1116 |
except ValueError:continue
|
| 1117 |
tasks[u]=_blank_task();tasks[u]['enabled']=bool(enabled)
|
| 1118 |
if not enabled:tasks[u]['status']='متوقف'
|
| 1119 |
+
GUEST.start()
|
| 1120 |
threading.Thread(target=monitor_loop,daemon=True,name='global-monitor').start()
|
| 1121 |
youtube.restore()
|
| 1122 |
log('system','[MONITOR] waiting')
|
guest_watch.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Guest (LinkMic / co-host) detection from host rooms.
|
| 2 |
+
|
| 3 |
+
Uses the public room WebSocket through piratetok-live-py (no API key, no signing
|
| 4 |
+
server). A guest is treated as "on stage" while the guest's userId keeps appearing
|
| 5 |
+
in link_mic_method events of a watched host room. The signal expires after
|
| 6 |
+
GUEST_ACTIVE_SECONDS of silence.
|
| 7 |
+
|
| 8 |
+
Hosts to watch come from GUEST_WATCH_HOSTS (comma separated). Guests are matched
|
| 9 |
+
against the accounts that are enabled in this project. A guest does not need to be
|
| 10 |
+
listed as a host; the host room where the guest appears must be listed.
|
| 11 |
+
"""
|
| 12 |
+
import os, re, threading, time
|
| 13 |
+
|
| 14 |
+
DEFAULT_ACTIVE_SECONDS = 120.0
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def parse_hosts(raw):
|
| 18 |
+
out = []
|
| 19 |
+
for h in re.split(r'[,\s;]+', raw or ''):
|
| 20 |
+
h = h.strip().lstrip('@').lower()
|
| 21 |
+
if h and h not in out:
|
| 22 |
+
out.append(h)
|
| 23 |
+
return out
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def _active_seconds():
|
| 27 |
+
try:
|
| 28 |
+
return max(30.0, float(os.getenv('GUEST_ACTIVE_SECONDS', str(DEFAULT_ACTIVE_SECONDS))))
|
| 29 |
+
except ValueError:
|
| 30 |
+
return DEFAULT_ACTIVE_SECONDS
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class GuestWatch:
|
| 34 |
+
def __init__(self, hosts, log=None, active_seconds=None):
|
| 35 |
+
self.hosts = list(hosts or [])
|
| 36 |
+
self.active_seconds = float(active_seconds) if active_seconds else _active_seconds()
|
| 37 |
+
self._log = log or (lambda user, msg: None)
|
| 38 |
+
self._lock = threading.Lock()
|
| 39 |
+
self._seen = {} # guest uid -> (host, last_ts)
|
| 40 |
+
self._uids = {} # username -> (uid, looked_up_ts)
|
| 41 |
+
self._names = {} # uid -> username
|
| 42 |
+
self._stop = threading.Event()
|
| 43 |
+
self._threads = []
|
| 44 |
+
self._profiles = None
|
| 45 |
+
self.running = False
|
| 46 |
+
|
| 47 |
+
# ---- identity -------------------------------------------------------
|
| 48 |
+
def _profile_cache(self):
|
| 49 |
+
if self._profiles is None:
|
| 50 |
+
from piratetok_live import ProfileCache
|
| 51 |
+
self._profiles = ProfileCache()
|
| 52 |
+
return self._profiles
|
| 53 |
+
|
| 54 |
+
def uid_for(self, username):
|
| 55 |
+
"""TikTok numeric userId for a username (cached). Empty string when unknown."""
|
| 56 |
+
username = (username or '').strip().lstrip('@').lower()
|
| 57 |
+
if not username:
|
| 58 |
+
return ''
|
| 59 |
+
now = time.time()
|
| 60 |
+
with self._lock:
|
| 61 |
+
hit = self._uids.get(username)
|
| 62 |
+
if hit and (hit[0] or now - hit[1] < 600):
|
| 63 |
+
return hit[0]
|
| 64 |
+
uid = ''
|
| 65 |
+
try:
|
| 66 |
+
uid = str(self._profile_cache().fetch(username).user_id or '')
|
| 67 |
+
except Exception as e:
|
| 68 |
+
self._log('system', '[GUEST] profile lookup failed for @%s: %s' % (username, type(e).__name__))
|
| 69 |
+
with self._lock:
|
| 70 |
+
self._uids[username] = (uid, now)
|
| 71 |
+
if uid:
|
| 72 |
+
self._names[uid] = username
|
| 73 |
+
return uid
|
| 74 |
+
|
| 75 |
+
# ---- signal ---------------------------------------------------------
|
| 76 |
+
def _label(self, uid):
|
| 77 |
+
with self._lock:
|
| 78 |
+
return self._names.get(uid) or ('id…' + uid[-4:])
|
| 79 |
+
|
| 80 |
+
def _mark(self, uid, host):
|
| 81 |
+
now = time.time()
|
| 82 |
+
with self._lock:
|
| 83 |
+
prev = self._seen.get(uid)
|
| 84 |
+
self._seen[uid] = (host, now)
|
| 85 |
+
if not prev or now - prev[1] > self.active_seconds:
|
| 86 |
+
self._log('system', '[GUEST] @%s guest activity detected on @%s' % (self._label(uid), host))
|
| 87 |
+
|
| 88 |
+
def active_host(self, username):
|
| 89 |
+
"""Host name if @username is currently a guest on a watched host, else ''."""
|
| 90 |
+
if not self.running:
|
| 91 |
+
return ''
|
| 92 |
+
uid = self.uid_for(username)
|
| 93 |
+
if not uid:
|
| 94 |
+
return ''
|
| 95 |
+
with self._lock:
|
| 96 |
+
hit = self._seen.get(uid)
|
| 97 |
+
if hit and time.time() - hit[1] <= self.active_seconds:
|
| 98 |
+
return hit[0]
|
| 99 |
+
return ''
|
| 100 |
+
|
| 101 |
+
# ---- watchers -------------------------------------------------------
|
| 102 |
+
def _run_host(self, host):
|
| 103 |
+
from piratetok_live import TikTokLiveClient, EventType
|
| 104 |
+
host_uid = ''
|
| 105 |
+
while not self._stop.is_set():
|
| 106 |
+
try:
|
| 107 |
+
if not host_uid:
|
| 108 |
+
host_uid = self.uid_for(host)
|
| 109 |
+
client = TikTokLiveClient(host).cdn_eu()
|
| 110 |
+
|
| 111 |
+
def on_link(evt, host=host, host_uid=host_uid):
|
| 112 |
+
d = evt.data if isinstance(evt.data, dict) else {}
|
| 113 |
+
uid = str(d.get('userId') or '')
|
| 114 |
+
if uid and uid != '0' and uid != host_uid:
|
| 115 |
+
self._mark(uid, host)
|
| 116 |
+
|
| 117 |
+
client.on(EventType.link_mic_method)(on_link)
|
| 118 |
+
self._log('system', '[GUEST] watching host @%s' % host)
|
| 119 |
+
client.run()
|
| 120 |
+
self._log('system', '[GUEST] @%s watcher ended; reconnecting' % host)
|
| 121 |
+
except Exception as e:
|
| 122 |
+
self._log('system', '[GUEST] @%s watcher error: %s' % (host, type(e).__name__))
|
| 123 |
+
self._stop.wait(30)
|
| 124 |
+
|
| 125 |
+
def start(self):
|
| 126 |
+
if not self.hosts:
|
| 127 |
+
self._log('system', '[GUEST] GUEST_WATCH_HOSTS empty; guest detection off')
|
| 128 |
+
return False
|
| 129 |
+
try:
|
| 130 |
+
import piratetok_live # noqa: F401
|
| 131 |
+
except Exception as e:
|
| 132 |
+
self._log('system', '[GUEST] piratetok unavailable (%s); guest detection off' % type(e).__name__)
|
| 133 |
+
return False
|
| 134 |
+
self.running = True
|
| 135 |
+
for h in self.hosts:
|
| 136 |
+
t = threading.Thread(target=self._run_host, args=(h,), daemon=True, name='guest-' + h[:24])
|
| 137 |
+
t.start()
|
| 138 |
+
self._threads.append(t)
|
| 139 |
+
return True
|
| 140 |
+
|
| 141 |
+
def stop(self):
|
| 142 |
+
self._stop.set()
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ fastapi==0.142.2
|
|
| 3 |
uvicorn==0.54.0
|
| 4 |
curl_cffi==0.16.3
|
| 5 |
pydantic==2.13.5
|
|
|
|
|
|
| 3 |
uvicorn==0.54.0
|
| 4 |
curl_cffi==0.16.3
|
| 5 |
pydantic==2.13.5
|
| 6 |
+
piratetok-live-py==0.3.0
|
test_guest_watch.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Unit checks for guest_watch.GuestWatch (no network)."""
|
| 3 |
+
import sys, time, pathlib
|
| 4 |
+
|
| 5 |
+
ROOT = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else '.').resolve()
|
| 6 |
+
sys.path.insert(0, str(ROOT))
|
| 7 |
+
import guest_watch
|
| 8 |
+
from guest_watch import GuestWatch, parse_hosts
|
| 9 |
+
|
| 10 |
+
assert parse_hosts(' @Talolil, muhajer_iraqi ;talolil\n') == ['talolil', 'muhajer_iraqi'], 'host parsing'
|
| 11 |
+
assert parse_hosts('') == []
|
| 12 |
+
|
| 13 |
+
logs = []
|
| 14 |
+
g = GuestWatch(['talolil'], log=lambda u, m: logs.append(m), active_seconds=60)
|
| 15 |
+
g.running = True
|
| 16 |
+
uids = {'ahmadfamouss': '7577974697217475597', 'talolil': '6923684579661169666'}
|
| 17 |
+
g.uid_for = lambda name: uids.get((name or '').lstrip('@').lower(), '')
|
| 18 |
+
|
| 19 |
+
# Not seen yet
|
| 20 |
+
assert g.active_host('ahmadfamouss') == ''
|
| 21 |
+
|
| 22 |
+
# Guest seen on talolil -> active
|
| 23 |
+
g._mark('7577974697217475597', 'talolil')
|
| 24 |
+
assert g.active_host('ahmadfamouss') == 'talolil', 'guest should be active after event'
|
| 25 |
+
assert g.active_host('@AhmadFamouss') == 'talolil', 'username normalisation'
|
| 26 |
+
|
| 27 |
+
# Unrelated account stays inactive
|
| 28 |
+
assert g.active_host('someone_else') == ''
|
| 29 |
+
|
| 30 |
+
# Silence longer than active_seconds -> inactive
|
| 31 |
+
with g._lock:
|
| 32 |
+
host, _ = g._seen['7577974697217475597']
|
| 33 |
+
g._seen['7577974697217475597'] = (host, time.time() - 61)
|
| 34 |
+
assert g.active_host('ahmadfamouss') == '', 'guest should expire after silence'
|
| 35 |
+
|
| 36 |
+
# Not running -> never active
|
| 37 |
+
g.running = False
|
| 38 |
+
g._mark('7577974697217475597', 'talolil')
|
| 39 |
+
assert g.active_host('ahmadfamouss') == ''
|
| 40 |
+
|
| 41 |
+
# Host userId must be ignored by the watcher callback logic (checked via uid compare)
|
| 42 |
+
assert GuestWatch(['x']).start() in (False, True) # must not crash when piratetok is missing or present
|
| 43 |
+
|
| 44 |
+
print('PASS guest_watch: join/expire/normalise/hosts')
|