#12 Added examples
This commit is contained in:
committed by
firedotguy
parent
f2e18e08c0
commit
a3a3c012ff
47
examples/stream/stop_stream.py
Normal file
47
examples/stream/stop_stream.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Пример остановки SSE потока из кода
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||
|
||||
import threading
|
||||
import time
|
||||
from itd import ITDClient, StreamConnect, StreamNotification
|
||||
|
||||
def main():
|
||||
cookies = 'YOUR_COOKIES_HERE'
|
||||
|
||||
if cookies == 'YOUR_COOKIES_HERE':
|
||||
print('! Укажите cookies в переменной cookies')
|
||||
return
|
||||
|
||||
client = ITDClient(cookies=cookies)
|
||||
|
||||
# Функция для прослушивания в отдельном потоке
|
||||
def listen():
|
||||
print('! Начинаем прослушивание...')
|
||||
try:
|
||||
for event in client.stream_notifications():
|
||||
if isinstance(event, StreamConnect):
|
||||
print(f'-- Подключено! User ID: {event.user_id}')
|
||||
else:
|
||||
print(f'🔔 {event.type.value}: {event.actor.username}')
|
||||
except Exception as e:
|
||||
print(f'! Ошибка: {e}')
|
||||
|
||||
# В отдельном потоке
|
||||
thread = threading.Thread(target=listen, daemon=True)
|
||||
thread.start()
|
||||
|
||||
print('Прослушивание запущено. Нажмите Enter для остановки...')
|
||||
input()
|
||||
|
||||
print('!! Останавливаем прослушивание...')
|
||||
client.stop_stream()
|
||||
|
||||
thread.join(timeout=5)
|
||||
print('! Остановлено')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user