class Tagger: def __init__(self): if config.TAG: self.djv = Dejavu(config.CONFIG) self.sp = SpotifyManager() while not self.sp.is_authenticated(): self.sp.auth() def tag(self, dir): for filename in os.listdir(dir): if os.path.isdir(dir + filename): self.tag(dir + filename + '/') continue if config.TAG: if config.VERBOSE: print("Tagging: " + dir + filename) song = self.djv.recognize(FileRecognizer, dir + filename) song = self.sp.sp.search(q=song['song_name'], type='track', limit=1) try: song_artist = song['tracks']['items'][0]['artists'][0][ 'name'] song_name = song['tracks']['items'][0]['name'] pprint(song_artist + ' - ' + song_name) self.apply_tags(dir + filename, song_artist, song_name) except IndexError: print(bcolors.FAIL + "unable to get tags for " + filename + bcolors.ENDC) pass except: print(bcolors.FAiL + "Wrong file type: " + filename + bcolors.ENDC) pass if config.RENAME: r = Renamer() r.rename(dir + filename, dir) pass def apply_tags(self, song, artist, name): try: f = mutagen.File(song) f.tags.get('TPE1').text[0] = artist f.tags.get('TIT2').text[0] = name f.save() except AttributeError as e: print(bcolors.FAIL + e.message + bcolors.ENDC)
def startService(self): # interrupts go nasty when we have spotify running in a thread signal.signal(signal.SIGINT, self.sigint) self.mgr = SpotifyManager(self) reactor.callInThread(self.mgr.connect) return service.Service.startService(self)
class Spotify(Item, service.Service): """ The spotify service. Provides an interface to the rest of the system for the spotify session and related machinery. """ implements(ispotify.ISpotifyService, isqueal.IMusicSource, isqueal.ITrackSource, isqueal.IUserConfigurable, isqueal.IRootResourceExtension, ) powerupInterfaces = (ispotify.ISpotifyService, isqueal.ITrackSource, isqueal.IMusicSource, isqueal.IUserConfigurable, isqueal.IRootResourceExtension, ) namespace = 'spotify' username = text() password = text() running = inmemory() name = 'spotify' parent = inmemory() mgr = inmemory() playing = inmemory() setup_form = setup_form label = "Spotify" def __init__(self, store, username, password): Item.__init__(self, store=store, username=username, password=password) def __repr__(self): return "Spotify(username=%r, password=SECRET, storeID=%r)" % (self.username, self.storeID) @property def evreactor(self): return self.store.findFirst(EventReactor) def activate(self): self.playing = None def sigint(self, handler, frame): # filthy hack! os.kill(os.getpid(), signal.SIGQUIT) def startService(self): # interrupts go nasty when we have spotify running in a thread signal.signal(signal.SIGINT, self.sigint) self.mgr = SpotifyManager(self) reactor.callInThread(self.mgr.connect) return service.Service.startService(self) def play(self, tid): log.msg("Play called with %r" % tid, system="squeal.spot.service.Spotify") track = self.get_track(tid) for p in self.store.powerupsFor(ISlimPlayerService): p.play(track) def registerConsumer(self, consumer, tid): log.msg("registering consumer %r on %r" % (consumer, self), system="squeal.spot.service.Spotify") self.playing = tid self.mgr.load(tid) self.mgr.play(consumer) def playlists(self): if self.mgr.ctr: return self.mgr.ctr return [] def get_playlist(self, pid): for playlist in self.mgr.ctr: if unicode(Link.from_playlist(playlist)) == pid: return playlist def image(self, image_id): return self.mgr.image(image_id) def search(self, query): self.mgr.search(query) def getTrackByLink(self, link): l = Link.from_string(link) return l.as_track() def getPlaylistByLink(self, link): """ Pass link as the string representation. This is a bit evil, there's no other way to do this, and if we list other people's playlists we'll need to do even more weird stuff. See http://getsatisfaction.com/spotify/topics/libspotify_does_not_provide_a_sp_link_as_playlist """ for p in self.mgr.ctr: l = str(Link.from_playlist(p)) if l == link: return p log.msg("Cannot find playlist for %s" % link, system="squeal.spot.service.Spotify") def main_widget(self): return web.Main() #isqueal.TrackSource def get_track(self, tid): track = Link.from_string(tid).as_track() return SpotifyTrack(track, self) def wrap_tracks(self, *tracks): for t in tracks: yield SpotifyTrack(t, self) #isqueal.IRootResourceExtension def add_resources(self, root): root.putChild("spotify", web.Root(self))
def __init__(self): if config.TAG: self.djv = Dejavu(config.CONFIG) self.sp = SpotifyManager() while not self.sp.is_authenticated(): self.sp.auth()