-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumio2m3u.py
executable file
·95 lines (70 loc) · 2.86 KB
/
volumio2m3u.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
#!/usr/bin/python3
import sys, os
from ast import literal_eval
changelog = {}
# ================ Configuration =================
version = 0.1
changelog[0.1] = "Init"
# ================================================
if __name__ == "__main__":
# =============== Argument parser=================
if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']:
print('Usage:')
print('\t{} <filename.volumio>'.format(sys.argv[0]))
print('\t-c, --changelog')
print('\t-h, --help')
print('\t-v, --version')
sys.exit(0)
if len(sys.argv) > 1 and sys.argv[1] in ['-v', '--version']:
print("Version {}".format(version))
sys.exit(0)
elif len(sys.argv) > 1 and sys.argv[1] in ['-c', '--changelog']:
for key in changelog:
print(key,"\t",changelog[key])
sys.exit(0)
if not len(sys.argv) == 2:
raise ValueError('Wront number of arguments!\nUsage: {} <filename.m3u>'.format(sys.argv[0]))
elif not os.path.isfile(sys.argv[1]):
raise ValueError('File {} not found!'.format(sys.argv[1]))
elif not sys.argv[1].endswith('.volumio'):
raise ValueError('Not a .volumio file!')
# ================================================
with open(sys.argv[1], 'r') as f:
data = f.readlines()
data = [l.strip() for l in data]
data = list(filter(None, data))
data = literal_eval(data[0])
print('#EXTM3U')
for entry in data:
trigger_www = 0
if 'http://' in entry['uri'] or 'https://' in entry['uri']:
trigger_www = 1
if trigger_www:
if 'name' in entry:
print('#EXTINF:-1,{}'.format(entry['name']))
print(entry['uri'])
elif 'title' in entry:
print('#EXTINF:-1,{}'.format(entry['title']))
print(entry['uri'])
else:
print('Entry has neither title, nor name tag! Taking end of uri.', file=sys.stderr)
title = entry['uri'].split('/')[-1].strip()
print('#EXTINF:-1,{}'.format(entry['title']))
print(entry['uri'])
else:
if not 'artist' in entry:
artist = ''
else:
artist = entry['artist'].strip()
if 'undefined' in entry['title']:
print('Entry has no title! Taking end of uri.', file=sys.stderr)
title = entry['uri'].split('/')[-1].strip()
# Assuming file extention: Removing it
title = ''.join(title.split('.')[:-1]).strip()
else:
title = entry['title'].strip()
if not artist:
print('#EXTINF:-1,{}'.format(title))
else:
print('#EXTINF:-1,{}'.format(' - '.join( [artist, title] )))
print(entry['uri'])