-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradio
executable file
·140 lines (104 loc) · 3.08 KB
/
radio
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
#!/usr/bin/env python
import os
import sys
import math
from collections import namedtuple
# configparser >
PLAYLISTS_PATH = os.path.abspath('by-name/')
SHOW_RECENT = 5
STORE_RECENT = 50
RECENT_PATH = os.path.abspath(os.path.expanduser('~/.radiolist_recent'))
PLAYER = 'mpv -playlist {path}'
Radio = namedtuple('Radio', ['num', 'name', 'basename', 'path'])
def get_playlists():
result = []
for root, dirs, files in os.walk(PLAYLISTS_PATH):
for file in files:
result.append(os.path.join(root, file))
return map(
lambda x: Radio(num=x[0],
name=x[1][len(PLAYLISTS_PATH) + 1:],
basename=os.path.basename(x[1]),
path=x[1]),
enumerate(sorted(result)))
def print_playlists(lst):
template = '[{{num:>{length}}}] {{name}}'
fmt = template.format(
length=int(
math.floor(
math.log10(len(lst)))
) + 1)
for radio in lst:
print(fmt.format(**radio._asdict()))
def print_recent(lst):
if not os.access(RECENT_PATH, os.R_OK):
return
with open(RECENT_PATH) as file:
names = list(map(lambda x: x.strip(), file.readlines()))
count = 0
res = []
while names and count < SHOW_RECENT:
radio = lookup(lst, names.pop())
if isinstance(radio, Radio):
count += 1
res.append(radio)
if res:
print('\nRecently played:')
print_playlists(res)
def store_recent(name):
all_recent = []
if os.path.isfile(RECENT_PATH):
with open(RECENT_PATH) as file:
all_recent = list(map(lambda x: x.strip(), file.readlines()))
if name in all_recent:
all_recent.remove(name)
all_recent.append(name)
with open(RECENT_PATH, 'w') as file:
file.writelines(map(lambda x: x + '\n', all_recent[-STORE_RECENT:]))
def lookup(lst, what):
if what.isdigit():
index = int(what)
if index < 0 or index >= len(lst):
return None
return lst[index]
else:
res = list(filter(lambda radio: what in radio.name, lst))
if len(res) == 1:
return res[0]
return res
def safe_input(prompt):
try:
inp = input(prompt)
if inp:
return inp
except EOFError:
pass
sys.exit(1)
def play(chosen):
print('Playing {0}'.format(chosen.name))
store_recent(chosen.name)
os.system(PLAYER.format(**chosen._asdict()))
sys.exit(0)
def find(lst, choice):
while True:
pls = lookup(lst, choice)
if pls:
if isinstance(pls, list):
print_playlists(pls)
lst = pls
choice = safe_input("Your choice? ")
continue
play(pls)
else:
print('Invalid choice')
sys.exit(1)
def main():
lst = list(get_playlists())
if len(sys.argv) > 1:
find(lst, sys.argv[1])
else:
print_playlists(lst)
print_recent(lst)
find(lst, safe_input("Your choice? "))
if __name__ == "__main__":
main()