def _save_playlist(self): self.saved = True sp = get_spotify() user = get_user() if not sp: raise pbl.PBLException(self, "not authenticated for save") if not user: raise pbl.PBLException(self, "no user") # TODO - this can be slow if the user has lots of playlists # should cache? uri = find_playlist_by_name(sp, user, self.playlist_name) if uri: print 'found', self.playlist_name, uri else: print 'creating new', self.playlist_name, 'playlist' response = sp.user_playlist_create(user, self.playlist_name) uri = response['uri'] pid = get_pid_from_playlist_uri(uri) if pid: batch_size = 100 uris = ['spotify:track:' + id for id in self.buffer] for start in xrange(0, len(uris), batch_size): turis = uris[start:start + batch_size] if start == 0 and not self.append: print 'replace', start sp.user_playlist_replace_tracks(user, pid, turis) else: print 'add', start sp.user_playlist_add_tracks(user, pid, turis) else: print "Can't get authenticated access to spotify"
def compile_object(objname, program): components = program['components'] symbols = program['symbols'] hsymbols = program['hsymbols'] if objname in symbols: return OK, symbols[objname] else: if objname in components: comp = components[objname] spec = get_spec_by_type(comp['type']) if spec: params = {} for param, val in comp['sources'].items(): status, cval = get_param_val(param, val, spec, program) if status == OK: params[param] = cval else: return status + " in " + objname, None for param, val in comp['params'].items(): status, cval = get_param_val(param, val, spec, program) if status == OK: params[param] = cval else: return status + " in " + objname, None try: obj = spec['class'](**params) symbols[objname] = obj hsymbols[obj] = objname return OK, obj except pbl.PBLException as e: #traceback.print_exc() print 'e reason', e.reason raise pbl.PBLException(None, e.reason, objname) except: traceback.print_exc() if debug_exceptions: raise raise pbl.PBLException(None, "creation failure", objname) else: return 'unknown type ' + comp['type'] + ' for ' + objname, None else: return 'missing object ' + str(objname), None
def next_track(self): if not is_authenticated(): raise pbl.PBLException(self, "not authenticated for save") track = self.source.next_track() if track and len(self.buffer) < self.max_size: self.buffer.append(track) elif not self.saved: self._save_playlist() return track
def __init__(self, name, uri=None, user=None, order_by_date_added=False, tracks_added_since=None, tracks_added_before=None): self.name = name self.uri = spotify_plugs.normalize_uri(uri) self.user = user self.order_by_date_added = order_by_date_added if tracks_added_before != None and len(tracks_added_before) > 0: try: delta = reltime.parse_to_rel_time(tracks_added_before) self.tracks_added_before = date_to_epoch(now()) - delta except ValueError as e: raise pbl.PBLException('bad relative time format', str(e)) else: self.tracks_added_before = -1 if tracks_added_since != None and len(tracks_added_since) > 0: try: delta = reltime.parse_to_rel_time(tracks_added_since) self.tracks_added_since = date_to_epoch(now()) - delta except ValueError as e: raise pbl.PBLException(self, 'bad relative time format ' + str(e)) else: self.tracks_added_since = -1 #print "since", tracks_added_since, self.tracks_added_since, date_to_epoch(now()) #print "before", tracks_added_before, self.tracks_added_before, date_to_epoch(now()) self.next_offset = 0 self.limit = 100 self.tracks = [] self.total = 1 self.cur_index = 0 self.track_count = 0
def _save_playlist(self): self.saved = True sp = get_spotify() user = get_user() if not sp: raise pbl.PBLException(self, "not authenticated for save") if not user: raise pbl.PBLException(self, "no user") # print 'creating', self.playlist_name response = sp.user_playlist_create(user, self.playlist_name) uri = response['uri'] pid = get_pid_from_playlist_uri(uri) if pid: batch_size = 100 uris = [ 'spotify:track:' + id for id in self.buffer] for start in xrange(0, len(uris), batch_size): turis = uris[start:start+batch_size] sp.user_playlist_add_tracks(user, pid, turis) else: print "Can't get authenticated access to spotify"
def __init__(self, source, playlist_name, suffix_type = "none"): self.source = source self.name = source.name + ' saved to ' + playlist_name self.buffer = [] self.saved = False self.max_size = 1000 if suffix_type == None: suffix_type = 'none' if suffix_type not in self.formatters: raise pbl.PBLException(self, "bad suffix type" + suffix_type) suffix = self.formatters[suffix_type]() self.playlist_name = playlist_name + suffix
def next_track(self): raise pbl.PBLException(self, "a comment is not runnable")