-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
189 lines (157 loc) · 5.57 KB
/
main.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import sys
import requests
import json
import time
import datetime
import platform
import subprocess
with open('config.json', 'r') as f:
config = json.load(f)
USER_NAME = config['user_name']
USER_PASSWORD = config['user_password']
cookie_dict = None
server_domain = "sooplive.co.kr"
def id_or_login_detect(value):
return value.isnumeric()
def get_id_from_login(user_login):
get_cookie()
url = f'https://live.{server_domain}/afreeca/player_live_api.php?bid={user_login}'
data = {
'bid': user_login,
'type': 'live'
}
req = requests.post(url, data=data, cookies=cookie_dict)
result = req.json()
return result['CHANNEL']['BNO']
def get_login_from_id(user_id):
get_cookie()
url = f'https://live.{server_domain}/afreeca/player_live_api.php?bno={user_id}'
data = {
'bno': user_id,
'type': 'live'
}
req = requests.post(url, data=data, cookies=cookie_dict)
result = req.json()
return result['CHANNEL']['BJID']
def console_print(message):
time = datetime.datetime.today().strftime('%Y-%m-%dT%H-%M-%S')
print("[{}] {}".format(time, message))
def get_cookie():
global cookie_dict
url = "https://login.{server_domain}/app/LoginAction.php"
data = {
"szWork": "login",
"szType": "json",
"szUid": USER_NAME,
"szPassword": USER_PASSWORD,
"isSaveId": "true",
"isSavePw": "false",
"isSaveJoin": "false",
"isLoginRetain": "Y"
}
req = requests.post(url, data=data)
cookies = req.cookies
cookie_dict = requests.utils.dict_from_cookiejar(cookies)
def stream_detect(user_id):
url = f'https://live.{server_domain}/afreeca/player_live_api.php'
data = {
'bno': user_id,
'type': 'live'
}
req = requests.post(url, data=data)
result = json.loads(req.text)
if 'RESOLUTION' in result['CHANNEL']:
return True
else:
return False
def get_stream_m3u8_direct(user_id):
url = 'https://live.{server_domain}/afreeca/player_live_api.php'
data = {
"bid": user_id,
"quality": "original",
"type": "aid",
"pwd": "",
"stream_type": "common",
}
req = requests.post(url, data=data, cookies=cookie_dict)
result = req.json()
if result['CHANNEL']['RESULT'] == 0:
# 방송중이 아님
return None
elif result['CHANNEL']['RESULT'] == 1:
# 방송중
m3u8_url = "https://live-global-cdn-v02.{server_domain}/live-stm-16/auth_playlist.m3u8?aid=" + result['CHANNEL']['AID']
return m3u8_url
elif result['CHANNEL']['RESULT'] == -6:
# 로그인 필요
get_cookie()
return get_stream_m3u8_direct(user_id)
else:
# 알 수 없는 오류
return None
def basic_file_info(user_login, extension):
date = datetime.datetime.today().strftime('%Y-%m-%dT%H-%M-%S')
path = './' + user_login + '/' + date + '.' + extension
return path
def download_stream_m3u8_legacy(user_login, m3u8_url, extension):
path = basic_file_info(user_login, extension)
if platform.system() == "Windows":
CREATE_NO_WINDOW = 0x08000000
subprocess.run(["streamlink", m3u8_url, "best", "-o", path], creationflags=CREATE_NO_WINDOW)
else:
subprocess.run(["streamlink", m3u8_url, "best", "-o", path])#, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return
def download_stream_legay(user_login, extension):
path = basic_file_info(user_login, extension)
stream_url = 'https://play.{server_domain}/' + user_login
if platform.system() == "Windows":
CREATE_NO_WINDOW = 0x08000000
subprocess.run(["streamlink", stream_url, "best", "-o", path, "--afreeca-username", USER_NAME, "--afreeca-password", USER_PASSWORD, "--afreeca-purge-credentials"], creationflags=CREATE_NO_WINDOW)
else:
subprocess.run(["streamlink", stream_url, "best", "-o", path, "--afreeca-username", USER_NAME, "--afreeca-password", USER_PASSWORD, "--afreeca-purge-credentials"])
return
console_print("Program started")
try:
user_info_list = sys.argv[1:]
user_info = user_info_list[0]
except:
console_print("Please input user login")
if len(user_info_list) == 0:
user_info = input("Input streamer nickname(if you want to exit, press enter): ")
if user_info == "":
exit()
if len(user_info_list) > 1:
console_print("only one streamer nickname is allowed")
exit()
# if id_or_login_detect(user_info):
# user_id = user_info
# user_login = get_login_from_id(user_id)
# else:
# user_login = user_info
# user_id = get_id_from_login(user_login)
user_login = user_info
repeat_check = True
latest_error = ""
if not os.path.exists("{}".format(user_login)):
os.makedirs("{}".format(user_login))
while True:
try:
if repeat_check:
console_print("[{user_login}] Waiting to start streaming".format(user_login=user_login))
repeat_check = False
stream_m3u8 = get_stream_m3u8_direct(user_login)
if stream_m3u8 is not None:
console_print("[{user_login}] Stream started".format(user_login=user_login))
try:
download_stream_m3u8_legacy(user_login, stream_m3u8, "ts")
except Exception as e:
# print("Error: {}".format(e))
continue
console_print("[{user_login}] Stream ended".format(user_login=user_login))
repeat_check = True
except Exception as e:
if latest_error != e or 1==1:
console_print("Error: {}".format(e))
latest_error = e
time.sleep(2)