-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalplaylist.py
60 lines (48 loc) · 1.75 KB
/
localplaylist.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
import song
import logging
import os
import codecs
logger = logging.getLogger("localplaylist")
class LocalPlaylist(object):
def __init__(self):
self.items = []
def load(self, filename):
with open(filename) as inf:
line = inf.readline()
while line:
desc = ""
if '#' in line:
(line, desc) = line.split('#', 1)
line = line.strip()
try:
track = song.Song.from_encoded(desc.split(' ')[-1])
if track is None:
track = song.Song.from_id(int(line))
self.items += [track]
except:
logger.exception("line %s error(%s)" % (line, filename))
line = inf.readline()
if len(self.items) == 0:
if os.path.exists(filename + os.extsep + "old"):
self.load(filename + os.extsep + "old")
def save(self, filename):
if os.path.exists(filename):
os.rename(filename, filename + os.extsep + "old")
with codecs.open(filename, 'w', 'utf-8') as outf:
for track in self.items:
outf.write(u"{id} # {title} by {artist} {info}\n"
.format(id=track.song_id, title=track.title,
artist=track.artist, info=track.encode()))
def count(self):
return len(self.items)
def get(self, idx):
track = self.items[idx]
try:
track.load_info()
except:
logger.exception("error loading info")
return track
def insert(self, track, idx=0):
self.items.insert(idx, track)
def append(self, track):
self.items += [track]