This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (76 loc) · 3.4 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
import simple_term_menu
import streamlink
import requests
import os
import time
import argparse
# import our own modules
from verify import verify
from utils.formatBytes import format_bytes
from utils.formatDuration import format_duration
from utils.manual_quality import manual_quality
def flagsInit():
parser = argparse.ArgumentParser(description="Afreeca TV Downloader :D")
parser.add_argument("-m", "--manual", default=False, action="store_true", help="Manually select download quality")
parser.add_argument("-o", "--output", default=False, help="File output, defaults to ")
parser.add_argument("-su", "--susername", default=False, help="Streamer username")
parser.add_argument("-u", "--username", default=False, help="Afreeca Username")
parser.add_argument("-p", "--password", default=False, help="Afreeca Password")
args = parser.parse_args()
return args
def downloadStream(username, streamUrl, qualityName, user, password):
session = streamlink.Streamlink()
session.set_option("stream-segment-threads", 10)
session.set_plugin_option("afreeca", "username", user)
session.set_plugin_option("afreeca", "password", password)
streams = session.streams(streamUrl)
stream = streams[qualityName]
print("Downloading stream using " + qualityName + " quality")
now = time.strftime("%Y-%m-%d_%H:%M", time.localtime())
if os.path.exists("downloads/" + username) is False:
os.makedirs("downloads/" + username)
output_path: str = username + "-" + now + "-afreeca.ts"
output_path_absolute: str = "downloads/" + username + "/" + username + "-" + now + "-afreeca.ts"
# if outputFlag is not False:
# outputPath = outputFlag
with stream.open() as fd:
with open(output_path_absolute, 'wb') as output:
file_size = 0
start_time = time.time()
while True:
try:
data = fd.read(1024)
if not data:
break
output.write(data)
file_size += len(data)
elapsed_time = time.time() - start_time
# ugly but works
print("\r" + f"Downloading to {output_path} || {format_duration(elapsed_time)} @ {format_bytes(file_size)} \x1b[?25l", end="", flush=True)
# loop !
except Exception as e:
fd.close()
print("An error occured: " + str(e))
if verify(username, urlStruct) is not None:
downloadStream(username, streamUrl, qualityName, output)
if __name__ == '__main__':
args = flagsInit()
# check if dl folder exists :D
if os.path.exists("downloads") is False:
os.makedirs("downloads")
username = args.susername
user = args.username
password = args.password
if username is False:
username = input("Enter streamer username:\n")
urlStruct: str = 'https://bjapi.afreecatv.com/api/' + username + '/station'
streamUrl = verify(username, urlStruct)
output = args.output
print(streamUrl)
qualityName: str = 'best'
if args.manual is True:
quality = manual_quality(streamUrl)
menu = simple_term_menu.TerminalMenu(quality)
menuIndex = menu.show()
qualityName = quality[menuIndex]
downloadStream(username, streamUrl, qualityName, user, password)