В парсер M3U добавлена поддержка дополнительных тегов (группы, страны и т.д.)
[vpproxy.git] / plugins / modules / M3uParser.py
1 # more info on the M3U file format available here:
2 # http://n4k3d.com/the-m3u-file-format/
3
4 import sys
5 import hexdump
6 import codecs
7
8 class track():
9     def __init__(self, length, title, path, attrs=None):
10         self.length = length
11         self.title = title
12         self.path = path
13         self.attrs = attrs
14
15
16 # # # song info lines are formatted like:
17 #EXTINF:419,Alice In Chains - Rotten Apple
18 # length (seconds)
19 # Song title
20 # # # file name - relative or absolute path of file
21 # ..\Minus The Bear - Planet of Ice\Minus The Bear_Planet of Ice_01_Burying Luck.mp3
22 def parseM3U(infile):
23     inf = open(infile,'r')
24
25     # # # all m3u files should start with this line:
26         #EXTM3U
27     # this is not a valid M3U and we should stop..
28     line = inf.readline()
29     line = line.lstrip('\xef\xbb\xbf')
30     line = line.rstrip('\n')
31     line = line.strip()
32     if not line.startswith('#EXTM3U'):
33        return
34
35     # initialize playlist variables before reading file
36     playlist=[]
37     song=track(None,None,None)
38
39     for line in inf:
40         line = line.lstrip('\xef\xbb\xbf')
41         line = line.rstrip('\n')
42         line = line.strip()
43         if line.startswith('#EXTINF:'):
44             # pull length and title from #EXTINF line
45             prefix,title=line.split('#EXTINF:')[1].split(',',1)
46             title=title.strip()
47             length,attrstr=prefix.split(' ',1)
48             attrs={}
49
50             while attrstr:
51
52               attrstr=attrstr.strip()
53               key,tail=attrstr.split('=',1)
54               tail=tail[1:]
55               value,attrstr=tail.split('"',1)
56               attrstr=attrstr.strip()
57               attrs[key]=value 
58             
59             title=title.decode('string_escape')
60             song=track(length,title,None,attrs)
61         elif (len(line) != 0):
62             # pull song path from all other, non-blank lines
63             song.path=line
64             playlist.append(song)
65
66             # reset the song variable so it doesn't use the same EXTINF more than once
67             song=track(None,None,None)
68
69     inf.close()
70
71     return playlist
72
73 # for now, just pull the track info and print it onscreen
74 # get the M3U file path from the first command line argument
75 def main():
76     m3ufile=sys.argv[1]
77     playlist = parseM3U(m3ufile)
78     for track in playlist:
79         print (track.title, track.length, track.path)
80
81 if __name__ == '__main__':
82     main()
83