forked from camathieu/soundcloudsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracks.py
202 lines (168 loc) · 6.24 KB
/
Tracks.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Sysaccess
import re
class Tracks:
def __init__(self,controller):
self.store = {}
self.controller = controller
def __getitem__(self,id):
if id in self.store:
return self.store[id]
obj = Sysaccess.rget(self.controller.redis,'track'+str(id))
if obj:
self.store[id] = obj
return self.store[id]
else:
self.store[id] = Track(id)
return self.store[id]
def __setitem__(self,id,res):
if id in self.store:
self.store[id].load(res)
else:
self[id]
def __delitem__(self,id):
if id in self.store:
del self.store[id]
Sysaccess.rdelete(self.controller.redis,'track'+str(id))
import CustomResource
class Track(CustomResource.CustomResource):
def __init__(self,id):
CustomResource.CustomResource.__init__(self,id)
self.favorites = []
self.playlist_name = None
self.load()
def load(self,res=None):
if not res:
self.load_res(Track.controller.api.get('/tracks/' + str(self.id)))
else :
self.load_res(res)
self.reload()
self.save()
def reload(self):
if Sysaccess.is_older(self.timestamp,Track.controller.settings.misc['refresh']):
if self.download():
self.tag()
self.timestamp = Sysaccess.now()
def save(self):
Sysaccess.rsave(Track.controller.redis,'track'+str(self.id),self)
def filename(self):
file_name = self.ascii('title')
if file_name:
return re.sub("[^\w]+",'_',self.ascii('title')) + ".mp3"
return False
def path(self):
file_name = self.filename()
if file_name:
return Track.controller.users[self['user_id']].path() + "/" + file_name
return False
def old_path(self):
file_name = self.filename()
if file_name:
return Track.controller.users[self['user_id']].old_path() + "/" + file_name
return False
def artwork_path(self):
return Track.controller.settings.path['track_artwork'] + "/" + str(self['id']) + ".png"
def get_download_url(self):
if self['stream_url']:
url = Track.controller.api.get(self['stream_url'], allow_redirects=False)
if url and url.location:
return url.location
print "download url not found"
return False
def download(self):
if Sysaccess.isfile(self.path()):
return True
print "Downloading " + self.ascii('title')
url = self.get_download_url()
if not url:
return False
success = Sysaccess.download(url,self.path())
if not success:
return False
Sysaccess.clean_tags(self.path())
def download_artwork(self):
if Sysaccess.isfile(self.artwork_path()):
return self.artwork_path()
url = self['artwork_url']
if not url:
return False
url = re.sub("large","crop",url)
success = Sysaccess.download(url,self.artwork_path())
if not success:
return False
return self.artwork_path()
def tag(self,force=False):
tags = Sysaccess.get_tags(self.path())
if not tags:
return False
new_tags = {}
if self['title']:
title = unicode(self.utf8('title').decode('utf-8'))
if title and tags['TIT2'] != title:
new_tags['TIT2'] = unicode(self.utf8('title').decode('utf-8'))
user = Track.controller.users[self['user_id']]
if user:
artist = unicode(user.utf8('username').decode('utf-8'))
if tags['TPE1'] != artist:
new_tags['TPE1'] = artist
if self.playlist_name:
album = unicode(self.playlist_name.decode('utf-8'))
if not tags['TALB']:
new_tags['TALB'] = album
if self['label_name']:
label = unicode(self.utf8('label_name').decode('utf-8'))
if label and tags['TIT1'] != label:
new_tags['TIT1'] = label
if self['release_year']:
year = unicode(str(self.utf8('release_year')))
if year and str(tags['TDRC']) !=str( year ):
new_tags['TDRC'] = year
if self['description']:
desc = unicode(self.utf8('description').decode('utf-8'))
if desc and tags['COMM'] != desc:
new_tags['COMM'] = desc
if self['genre']:
genre = unicode(self.utf8('genre').decode('utf-8'))
if genre and tags['TCON'] != genre:
new_tags['TCON'] = genre
if not tags['APIC']:
artwork = self.download_artwork()
if artwork:
new_tags['APIC'] = artwork
fav = unicode('##FAV##')
if self['user_favorite'] and not tags['TCOM'] == fav:
new_tags['TCOM'] = fav
elif not self['user_favorite'] and tags['TCOM'] == fav:
new_tags['TCOM'] = None
if not len(new_tags.keys()):
return True
print "old :" + tags.__repr__()
print "new :" + new_tags.__repr__()
print "taging " + self.utf8('title')
Sysaccess.tag(self.path(),new_tags)
def favoritize(self,user):
if user.id in self.favorites:
return True
file_name = self.filename()
if file_name:
user_fav_path = user.path() + '/favorites/' + file_name
print self.path()
print user_fav_path
Sysaccess.link(self.path(),user_fav_path)
if user.id == self.controller.me:
self.tag()
self.favorites.append(user.id)
self.save()
def set_playlist(self,playlist_name):
if not playlist_name:
return False
if self.playlist_name == playlist_name:
return True
file_name = self.filename()
if file_name:
playlist_path = Track.controller.users[self['user_id']].path() + '/'+ playlist_name + '/' + file_name
Sysaccess.link(self.path(),playlist_path)
self.playlist_name = playlist_name
self.tag()
self.save()
def __repr__(self):
return self.utf8('title')