-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
71 lines (62 loc) · 2.49 KB
/
utils.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
import shutil
import vlc
import time
def play_stream(selected_stream):
Instance = vlc.Instance('--no-video --verbose -1')
player = Instance.media_player_new()
Media = Instance.media_new(selected_stream.url)
Media.get_mrl()
player.set_media(Media)
player.play()
while True:
player_state = player.get_state()
# print(player_state)
if player_state == vlc.State.Playing:
time.sleep(1)
if player.get_state() == vlc.State.Playing:
break
else:
return -1
elif player_state == vlc.State.Ended:
return -1
while True:
time.sleep(2)
if not player.get_time():
return -1
else:
break
print("\nPlaying ", selected_stream.title, "...")
while player.is_playing():
printProgressBar(player.get_position()*100, 100, prefix = 'Progress:', suffix = 'Complete')
time.sleep(0.5)
printProgressBar(100, 100, prefix = 'Progress:', suffix = 'Complete')
return 0
def printProgressBar (iteration, total, prefix = '', suffix = '', usepercent = True, decimals = 1, fill = '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
usepercent - Optoinal : display percentage (Bool)
decimals - Optional : positive number of decimals in percent complete (Int), ignored if usepercent = False
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
# length is calculated by terminal width
twx, twy = shutil.get_terminal_size()
length = twx - 1 - len(prefix) - len(suffix) -4
if usepercent:
length = length - 6
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
# process percent
if usepercent:
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='', flush=True)
else:
print('\r%s |%s| %s' % (prefix, bar, suffix), end='', flush=True)
# Print New Line on Complete
if iteration == total:
print(flush=True)