-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatops.py
More file actions
99 lines (85 loc) · 3.32 KB
/
chatops.py
File metadata and controls
99 lines (85 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytchat
import threading
import yt_dlp
import os
import shutil
from songprocessor import download_and_process
from firebase_sync import get_firebase_sync
def cleanup_on_start():
"""Clean up temporary files and Firebase data on startup."""
# Clear temp music folder
temp_folder = "temp_music"
if os.path.exists(temp_folder):
shutil.rmtree(temp_folder)
print(f"Cleared {temp_folder}/")
os.makedirs(temp_folder, exist_ok=True)
# Clear output music folder
output_folder = "output_music"
if os.path.exists(output_folder):
shutil.rmtree(output_folder)
print(f"Cleared {output_folder}/")
os.makedirs(output_folder, exist_ok=True)
# Clear Firebase database
firebase = get_firebase_sync()
if firebase:
firebase.clear_all()
print("Cleared Firebase database")
def get_live_video_id(channel_url):
"""Get the current live stream video ID from a channel"""
ydl_opts = {
'quiet': True,
'no_warnings': True,
'extract_flat': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(f"{channel_url}/live", download=False)
if info:
return info.get('id')
return None
# Replace 'channelname' with the actual channel name
channel_url = "https://www.youtube.com/@squidud"
# Clean up on startup
cleanup_on_start()
#initialize da chat
video_id = get_live_video_id(channel_url)
if not video_id:
raise Exception(f"No live stream found for {channel_url}")
chat = pytchat.create(video_id=video_id)
chat.video_id = video_id # Store video_id for GUI access
print(f"Connected to live chat of video ID: {video_id}")
processing_requests = [] #queue of requests to process or are currently processing
ready_requests = [] #songs that are done processing and ready to play
# Initialize Firebase sync
firebase_sync = get_firebase_sync()
# Background worker to process songs (so cool :D)
def process_worker():
while True:
if processing_requests:
item = processing_requests[0]
if 'location' not in item:
result = download_and_process(item['search'], processing_queue=processing_requests, position_in_queue=0)
if result:
ready_requests.append(result)
# Update Firebase when queue changes
if firebase_sync:
firebase_sync.update_queue(processing_requests, ready_requests)
else:
import time
time.sleep(0.5)
# Start worker thread
threading.Thread(target=process_worker, daemon=True).start()
#loops, reading the chat
def chat_listener():
while chat.is_alive():
for c in chat.get().sync_items():
print(f"message received: {c.datetime} [{c.author.name}]- {c.message}")
if "/request" in c.message:
#get message content after /request
search = c.message.split("/request",1)[1].strip()
print(f"searching for {search}")
processing_requests.append({"user": c.author.name, "search": search})
# Update Firebase when new request added
if firebase_sync:
firebase_sync.update_queue(processing_requests, ready_requests)
# Start chat listener thread
threading.Thread(target=chat_listener, daemon=True).start()