-
Notifications
You must be signed in to change notification settings - Fork 11
/
m3uparser.py
67 lines (55 loc) · 1.87 KB
/
m3uparser.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
# more info on the M3U file format available here:
# http://n4k3d.com/the-m3u-file-format/
import sys
class track():
def __init__(self, length, title, path):
self.length = length
self.title = title
self.path = path
"""
song info lines are formatted like:
EXTINF:419,Alice In Chains - Rotten Apple
length (seconds)
Song title
file name - relative or absolute path of file
..\Minus The Bear - Planet of Ice\Minus The Bear_Planet of Ice_01_Burying Luck.mp3
"""
def parsem3u(infile):
try:
assert(type(infile) == '_io.TextIOWrapper')
except AssertionError:
infile = open(infile,'r')
"""
All M3U files start with #EXTM3U.
If the first line doesn't start with this, we're either
not working with an M3U or the file we got is corrupted.
"""
line = infile.readline()
if not line.startswith('#EXTM3U'):
return
# initialize playlist variables before reading file
playlist=[]
song=track(None,None,None)
for line in infile:
line=line.strip()
if line.startswith('#EXTINF:'):
# pull length and title from #EXTINF line
length,title=line.split('#EXTINF:')[1].split(',',1)
song=track(length,title,None)
elif (len(line) != 0):
# pull song path from all other, non-blank lines
song.path=line
playlist.append(song)
# reset the song variable so it doesn't use the same EXTINF more than once
song=track(None,None,None)
infile.close()
return playlist
# for now, just pull the track info and print it onscreen
# get the M3U file path from the first command line argument
def main():
m3ufile=sys.argv[1]
playlist = parseM3U(m3ufile)
for track in playlist:
print (track.title, track.length, track.path)
if __name__ == '__main__':
main()