Reoo88 commited on
Commit
2d9145f
·
1 Parent(s): ca2843b

Restore project to original state (tree of 23e9e2b); revert guest detection commits 0ab7aa3 and ca2843b

Browse files
Files changed (5) hide show
  1. Dockerfile +3 -5
  2. app.py +6 -58
  3. guest_watch.py +0 -142
  4. requirements.txt +0 -1
  5. test_guest_watch.py +0 -44
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 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,9 +60,7 @@ 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"]
 
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
  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"]
app.py CHANGED
@@ -9,7 +9,6 @@ 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
- from guest_watch import GuestWatch, parse_hosts
13
 
14
  MSG=('تيك توك يحجب روابط البث لهذا الحساب (غرفة مقيدة بعمر 18+ أو جلسة تحتاج تحققًا)؛ '
15
  'الصق كوكيز الجلسة الكامل في لوحة «توكن تيك توك» ثم أعد المحاولة')
@@ -455,32 +454,6 @@ _direct_backoff={}
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
- _guest_logged={}
474
-
475
- def _guest_note(user, host):
476
- """Log guest transitions only (the monitor runs every cycle)."""
477
- if host:
478
- if _guest_logged.get(user)!=host:
479
- _guest_logged[user]=host
480
- log(user,'[GUEST] @%s is a guest on @%s'%(user,host))
481
- else:
482
- _guest_logged.pop(user,None)
483
-
484
  def _oauth_automation_on():
485
  try:return bool(youtube.journal.read().get('enabled'))
486
  except Exception:return False
@@ -522,15 +495,9 @@ def _direct_loop(user, dest, stop):
522
  log(user,'[MONITOR] @%s observe failed during relay: %s'%(user, msg[:120]))
523
  if stop.wait(min(30, _direct_backoff.get(user, 2))):return
524
  continue
525
- src_host=''
526
  if obs.get('status')!='live':
527
- gs=_guest_source(user, obs)
528
- if not gs:
529
- log(user,'[MONITOR] @%s OFFLINE'%user)
530
- return
531
- obs,src_host=gs
532
- with _direct_guard:
533
- if user in _direct_handles:_direct_handles[user]['src']=src_host
534
  if room and obs.get('room_id') and obs['room_id']!=room:
535
  log(user,'[MONITOR] @%s room changed; clean source transition'%user)
536
  _direct_unstable.pop(user, None)
@@ -586,7 +553,7 @@ def _direct_loop(user, dest, stop):
586
  with _direct_guard:_direct_handles.pop(user, None)
587
  log(user,'[MONITOR] @%s waiting'%user)
588
 
589
- def _maybe_start_direct(user, obs, src_host=''):
590
  """Start RTMPS relay when OAuth automation is off but an ingest destination exists."""
591
  if _oauth_automation_on():
592
  return
@@ -612,7 +579,7 @@ def _maybe_start_direct(user, obs, src_host=''):
612
  th=threading.Thread(target=_direct_loop, args=(user, dest, stop), daemon=True, name='direct-'+user)
613
  hub.update(user, thread=th, stop=stop)
614
  with _direct_guard:
615
- _direct_handles[user]={'thread':th,'stop':stop,'sender':None,'src':src_host}
616
  log(user,'[RELAY] @%s starting (env RTMPS, OAuth not required)'%user)
617
  update(user, state='STARTING', status='بدء Relay')
618
  th.start()
@@ -811,11 +778,6 @@ def _check_account(user):
811
  out=throttle_failure(user,msg)
812
  if out:log(user,'[MONITOR] @%s %s'%(user,out))
813
  return
814
- src_host=''
815
- if obs.get('status')!='live':
816
- gs=_guest_source(user, obs)
817
- if gs:
818
- obs,src_host=gs
819
  if obs.get('status')!='live':
820
  prev=None
821
  with lock:prev=(tasks.get(user) or {}).get('state')
@@ -859,21 +821,8 @@ def _check_account(user):
859
  if top:log(user,'[SOURCE] @%s selected %s'%(user,brief))
860
  if prev_room and prev_room!=obs.get('room_id') and relaying:
861
  log(user,'[MONITOR] @%s session/room changed; source will be refreshed'%user)
862
- if src_host and hub.busy(src_host):
863
- # Host is already published by this project on the single RTMPS key: never publish twice.
864
- if relaying:
865
- _stop_direct(user, 'guest on @%s (host already relayed)'%src_host)
866
- _guest_note(user, src_host)
867
- _set_state(user,'LIVE','LIVE · ضيف عند @%s (البث عبر @%s)'%(src_host,src_host),**extra)
868
- return
869
- if relaying:
870
- with _direct_guard:cur_src=(_direct_handles.get(user) or {}).get('src','')
871
- if cur_src!=src_host:
872
- _stop_direct(user, 'guest source switch')
873
- log(user,'[GUEST] @%s source switch: %s'%(user, ('guest on @%s'%src_host) if src_host else 'own room'))
874
- _guest_note(user, src_host)
875
- _set_state(user,'LIVE','LIVE' if not src_host else 'LIVE · ضيف عند @%s'%src_host,**extra)
876
- _maybe_start_direct(user, obs, src_host)
877
  if _oauth_automation_on() and not hub.busy(user) and (obs.get('sources') or []):
878
  try:
879
  youtube.nudge_live(user, obs)
@@ -1133,7 +1082,6 @@ def startup():
1133
  except ValueError:continue
1134
  tasks[u]=_blank_task();tasks[u]['enabled']=bool(enabled)
1135
  if not enabled:tasks[u]['status']='متوقف'
1136
- GUEST.start()
1137
  threading.Thread(target=monitor_loop,daemon=True,name='global-monitor').start()
1138
  youtube.restore()
1139
  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
 
13
  MSG=('تيك توك يحجب روابط البث لهذا الحساب (غرفة مقيدة بعمر 18+ أو جلسة تحتاج تحققًا)؛ '
14
  'الصق كوكيز الجلسة الكامل في لوحة «توكن تيك توك» ثم أعد المحاولة')
 
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
  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
+ log(user,'[MONITOR] @%s OFFLINE'%user)
500
+ return
 
 
 
 
 
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
  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
  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
  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
  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
+ _set_state(user,'LIVE','LIVE',**extra)
825
+ _maybe_start_direct(user, obs)
 
 
 
 
 
 
 
 
 
 
 
 
 
826
  if _oauth_automation_on() and not hub.busy(user) and (obs.get('sources') or []):
827
  try:
828
  youtube.nudge_live(user, obs)
 
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')
guest_watch.py DELETED
@@ -1,142 +0,0 @@
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,4 +3,3 @@ fastapi==0.142.2
3
  uvicorn==0.54.0
4
  curl_cffi==0.16.3
5
  pydantic==2.13.5
6
- piratetok-live-py==0.3.0
 
3
  uvicorn==0.54.0
4
  curl_cffi==0.16.3
5
  pydantic==2.13.5
 
test_guest_watch.py DELETED
@@ -1,44 +0,0 @@
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')