def playlistinfo(self): with mpd.connect(self.host, self.port) as client: info = client.playlistinfo() info = [item for item in info if item] for item in info: normalize(item, {'title' : ('file',), 'artist' : tuple()}) item['duration'] = fmt_time(item.get('time', 0)) return info
def search(self, type, what): self.last_search = (type, what) with mpd.connect(self.host, self.port) as client: results = client.search(type, what) results = [x for x in results if 'file' in x and x['file'].strip() != ''] for result in results: normalize(result, {'title' : ('file',)}) self.last_search_results = results return results
def list(self, uri): with mpd.connect(self.host, self.port) as client: listing = client.lsinfo(uri) for d in listing: if 'directory' in d: d['dirname'] = d['directory'].split('/')[-1] if 'file' in d: normalize(d, {'title' : ('file',)}) return listing
def connect_mpd(mpd): # Connect with MPD #------------------------------------------------ global debug connected = False retry_count = 3 i = 1 while connected == False: connected = True try: mpd.connect(TEST_MPD_HOST, TEST_MPD_PORT) except SocketError as e: connected = False if connected == False: if debug: print "Couldn't connect. Retrying" i = i + 1 if i > retry_count: return (9) time.sleep(3) if debug: print("Connected to MPD-Server") if debug: print "MPD-Version: %s" % mpd.mpd_version return (0)
def status(self): with mpd.connect(self.host, self.port) as client: status = client.status() current_song = client.currentsong() try: a,b = status.pop('time') status['time'] = a.seconds status['duration'] = b.seconds status['title'] = current_song['title'] status['artist'] = current_song['artist'] except: pass return status
def info(self): with mpd.connect(self.host, self.port) as client: info = client.lsinfo() return info
def seek(self, song, time): with mpd.connect(self.host, self.port) as client: client.seek(song, time)
def next(self): with mpd.connect(self.host, self.port) as client: client.next()
def load(self, song): with mpd.connect(self.host, self.port) as client: client.load(song)
def delete(self, song): with mpd.connect(self.host, self.port) as client: client.delete(song)
def clear(self): with mpd.connect(self.host, self.port) as client: client.clear()
def stop(self): with mpd.connect(self.host, self.port) as client: client.stop()
def listplaylists(self): with mpd.connect(self.host, self.port) as client: info = client.listplaylists() return info
for name, scrobbles in album_stats.items(): artists[artist_name].albums[name].scrobbles = scrobbles except KeyError: print artist_name + " no longer in library" def normalize_stats(artists): for artist in artists.values(): for album in artist.albums.values(): num_tracks = len(album.songs) album.times_listened = album.scrobbles / float(num_tracks) network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET) mpd = mpd.MPDClient() mpd.connect("127.0.0.1", 6600) songs = mpd.listallinfo() artists = create_artist_list(songs, DIRECTORY, SUB_DIRECTORY, True) genres = create_genre_list(artists) load_stats(artists, "stats.bin") #fetch_album_stats(artists, network) try: #fetch_listener_stats(artists, network) pass except httplib.BadStatusLine: print "retrying"
def play(self): with mpd.connect(self.host, self.port) as client: client.play()
def pause(self): with mpd.connect(self.host, self.port) as client: client.pause(1)
def setvol(self,x): with mpd.connect(self.host, self.port) as client: client.setvol(x)
def currentsong(self): with mpd.connect(self.host, self.port) as client: current_song = client.currentsong() normalize(current_song, {'title' : ('file',), 'artist' : tuple()}) return current_song
def add(self, *songs): with mpd.connect(self.host, self.port) as client: for song in songs: client.add(song)
self.redirect(url) except: self.redirect("http://img.schredder.me/200x200") class PlaylistHandler(tornado.web.RequestHandler): def get(self): pl = self.get_arguments("pl") if len(pl) > 0: self.write(json.dumps(mpd.listplaylist(pl[0]))) self.write(json.dumps(mpd.listplaylists())) ArtistArtCache = {} AlbumArtCache = {} mpd = mpd.MPDClient() application = tornado.web.Application([ (r"/artist", ArtistHandler), (r"/artistArt", ArtistArtHandler), (r"/albums", AlbumHandler), (r"/albumArt", AlbumArtHandler), (r"/albums/title", TitleHandler), (r"/playlist", PlaylistHandler), (r"/", tornado.web.RedirectHandler, {"url": "/index.html"}), (r"/music/(.*)", tornado.web.StaticFileHandler, {"path": MUSIC_PATH}), (r"/(.*)", tornado.web.StaticFileHandler, {"path": "static/"}) ]) if __name__ == "__main__": mpd.connect("localhost", 6600) application.listen(8888) tornado.ioloop.IOLoop.instance().start()
def previous(self): with mpd.connect(self.host, self.port) as client: client.previous()