# Clean Welcome CLEAN_WELCOME = sb(os.environ.get("CLEAN_WELCOME", "True")) # Last.fm Module BIO_PREFIX = os.environ.get("BIO_PREFIX", None) DEFAULT_BIO = os.environ.get("DEFAULT_BIO", None) LASTFM_API = os.environ.get("LASTFM_API", None) LASTFM_SECRET = os.environ.get("LASTFM_SECRET", None) LASTFM_USERNAME = os.environ.get("LASTFM_USERNAME", None) LASTFM_PASSWORD_PLAIN = os.environ.get("LASTFM_PASSWORD", None) LASTFM_PASS = md5(LASTFM_PASSWORD_PLAIN) if LASTFM_API and LASTFM_SECRET and LASTFM_USERNAME and LASTFM_PASS: lastfm = LastFMNetwork(api_key=LASTFM_API, api_secret=LASTFM_SECRET, username=LASTFM_USERNAME, password_hash=LASTFM_PASS) else: lastfm = None # Google Drive Module G_DRIVE_CLIENT_ID = os.environ.get("G_DRIVE_CLIENT_ID", None) G_DRIVE_CLIENT_SECRET = os.environ.get("G_DRIVE_CLIENT_SECRET", None) G_DRIVE_AUTH_TOKEN_DATA = os.environ.get("G_DRIVE_AUTH_TOKEN_DATA", None) GDRIVE_FOLDER_ID = os.environ.get("GDRIVE_FOLDER_ID", None) TEMP_DOWNLOAD_DIRECTORY = os.environ.get("TMP_DOWNLOAD_DIRECTORY", "./downloads") # Setting Up CloudMail.ru and MEGA.nz extractor binaries, # and giving them correct perms to work properly. if not os.path.exists('bin'):
def Client(): global _network if not _network: _network = LastFMNetwork(api_key=api_k, api_secret=api_s) _network.enable_rate_limit() return _network
def get_art(self, song): """Get artwork from LastFM. Before connecting to network, if there exists a file whose name begins with ``_sanitize(song['artist'] + ' ' + song['album'])``, return that file path. Do not overwrite existing files. Set ``os.path.join(save_dir, 'current')`` as symlink to image file. :param dict song: A dictionary with keys ``'album'`` and ``'artist'`` to correspond with string representations of the album and artist (resp.) of interest. Use ``MPDClient.currentsong()`` to return uch a dictionary . :return: A string representation of the local file path to the image file for ``song`` or ``None`` if no results found """ if not os.path.exists(self.save_dir): os.makedirs(self.save_dir) l = [ n for n in os.listdir(self.save_dir) if n.startswith(song['artist'] + "_" + song['album']) ] if l != []: self._log("Already have this album\n") file_path = os.path.join(self.save_dir, l[0]) # We have album art - check if it's linked if os.path.realpath(self.link_path) != file_path: self._log("Linking...\n") self.remove_current_link() self.set_current_link(file_path) self._log("Exiting.\n") return file_path # Define the search network compatible with LastFM API network = LastFMNetwork(api_key=_last_fm_api_key) album_search = AlbumSearch(song['artist'] + ' ' + song['album'], network) if album_search.get_total_result_count() == 0: # Remove current album link, and return, since no art was found for # given query. self._log("No results from Last.FM\n") self.remove_current_link() return None # Get the first hit, since there is at least one result - # the "I'm feeling lucky" approach. album = album_search.get_next_page()[0] # Get url of album art from ``pylast.AlbumSearch`` object img_url = album.get_cover_image() file_path = os.path.join(self.save_dir, self._get_save_name(song, img_url)) # Check if this file exists in filesystem already and is linked if os.path.isfile(file_path): if os.path.realpath(self.link_path) == file_path: return file_path else: try: # Download the image urlretrieve(img_url, file_path) self.remove_current_link() except HTTPError as e: self._log(e + "\n") self.remove_current_link() return None self.set_current_link(file_path) return file_path
TZ_NUMBER = int(os.environ.get("TZ_NUMBER", 1)) # Clean Welcome CLEAN_WELCOME = sb(os.environ.get("CLEAN_WELCOME", "True")) # Last.fm Module BIO_PREFIX = os.environ.get("BIO_PREFIX", None) DEFAULT_BIO = os.environ.get("DEFAULT_BIO", None) LASTFM_API = os.environ.get("LASTFM_API", None) LASTFM_SECRET = os.environ.get("LASTFM_SECRET", None) LASTFM_USERNAME = os.environ.get("LASTFM_USERNAME", None) LASTFM_PASSWORD_PLAIN = os.environ.get("LASTFM_PASSWORD", None) LASTFM_PASS = md5(LASTFM_PASSWORD_PLAIN) if LASTFM_API and LASTFM_SECRET and LASTFM_USERNAME and LASTFM_PASS: lastfm = LastFMNetwork(api_key=LASTFM_API, api_secret=LASTFM_SECRET, username=LASTFM_USERNAME, password_hash=LASTFM_PASS) else: lastfm = None # Google Drive Module G_DRIVE_CLIENT_ID = os.environ.get("G_DRIVE_CLIENT_ID", None) G_DRIVE_CLIENT_SECRET = os.environ.get("G_DRIVE_CLIENT_SECRET", None) G_DRIVE_AUTH_TOKEN_DATA = os.environ.get("G_DRIVE_AUTH_TOKEN_DATA", None) GDRIVE_FOLDER_ID = os.environ.get("GDRIVE_FOLDER_ID", None) TEMP_DOWNLOAD_DIRECTORY = os.environ.get("TMP_DOWNLOAD_DIRECTORY", "./downloads") # Setting Up CloudMail.ru and MEGA.nz extractor binaries,
def get_last(): with open('instance/config.yaml', 'r') as fp: config = yaml.load(fp) return LastFMNetwork(api_key=config['lastfm_api_key'], api_secret=config['lastfm_api_secret'])
def get_art(self, song): """Get artwork from LastFM. Before connecting to network, if there exists a file whose name begins with ``_sanitize(song['artist'] + ' ' + song['album'])``, return that file path. Do not overwrite existing files. :param dict song: A dictionary with keys ``'album'`` and ``'artist'`` to correspond with string representations of the album and artist (resp.) of interest. Use ``MPDClient.currentsong()`` to return uch a dictionary . :return: A string representation of the local file path to the image file for ``song`` or ``None`` if no results found """ album_tofind = _sanitize(song['artist'], song['album']) l = [ n for n in os.listdir(self.save_dir) if n.startswith(album_tofind) ] if l != []: file_path = os.path.join(self.save_dir, l[0]) sys.stderr.write("Found {}\n".format(file_path)) return file_path # Define the search network compatible with LastFM API network = LastFMNetwork(api_key=_last_fm_api_key) album_search = AlbumSearch(song['album'], network) #if int(album_search.get_total_result_count()) == 0: if album_search.get_total_result_count() == None: # LastFm does not have this album, links to unknown.png sys.stderr.write("Last.FM: no results\n") unknown = os.path.join(self.save_dir, 'unknown.png') album_tofind += ".png" os.symlink(unknown, os.path.join(self.save_dir, album_tofind)) return album_tofind # Get the first hit, since there is at least one result - # the "I'm feeling lucky" approach. album = album_search.get_next_page()[0] # Get url of album art from ``pylast.AlbumSearch`` object img_url = album.get_cover_image() file_path = os.path.join(self.save_dir, self._get_save_name(song, img_url)) # Check if this file exists in filesystem already if os.path.isfile(file_path): sys.stderr.write( "Last.FM: we already had the album {}!\n".format(file_path)) return file_path else: try: # Download the image urlretrieve(img_url, file_path) except e: sys.stderr.write(e + "\n") return None sys.stderr.write("Last.FM: found {}\n".format(file_path)) return file_path
from flask import Flask app = Flask(__name__) app.config.from_pyfile("../config.cfg") from pylast import LastFMNetwork lastfm = LastFMNetwork(api_key=app.config["LASTFM_API_KEY"], api_secret=app.config["LASTFM_API_SECRET"]) from . import views, models
def __init__(self, username=''): self._username = username self._api = LastFMNetwork(api_key=get_last_fm_api_key()) self._user = self._api.get_user(self._username)