-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmchad.py
55 lines (46 loc) · 1.49 KB
/
mchad.py
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
from urllib.parse import urlparse
import sseclient
import json
import requests
MCHAD = 'https://repo.mchatx.org'
def video_id(value: str):
query = urlparse(value)
if query.hostname == 'youtu.be':
return query.path[1:]
if query.hostname in ('www.youtube.com', 'youtube.com'):
if query.path == '/watch':
p = urlparse(query.query)
return p['v'][0]
if query.path[:7] == '/embed/':
return query.path.split('/')[2]
if query.path[:3] == '/v/':
return query.path.split('/')[2]
return None
def getRoomList():
return requests.get(f'{MCHAD}/Room/').json()
def getRoom(id, idonly = True):
if idonly:
id = 'YT_' + id
try:
return requests.get(f'{MCHAD}/Room/?link={id}').json()[0]
except IndexError:
return None
def getRoomByName(name):
return requests.get(f'{MCHAD}/Room/?name={name}').json()[0]
def getListenerByName(name):
return sseclient.SSEClient(f'{MCHAD}/Listener/?room={name}')
def getListnerByID(id):
return getListenerByName(getRoom(id)['Nick'])
def roomGeneratorbyID(id):
for x in getListnerByID(id):
data = json.loads(x.data)
if data == {}:
continue
elif data['flag'] == 'insert':
yield data['content']['Stext']
if __name__ == "__main__":
print('mchad test')
print(video_id('https://youtu.be/dQw4w9WgXcQ'))
print(getRoomList())
print(getRoom('0BBfB9N_VFs'))
print(getRoomByName('Translator Vee'))