def main(): if __name__ == '__main__': names = open("lastfm.txt", "r") #A text file where each line is a username duplicates = open(setting + "_duplicates.txt", "r") get_duplicates(duplicates, duplicate_array) duplicates.close() API_KEY = lastfmsettings.API_KEY API_SECRET = lastfmsettings.API_SECRET username = lastfmsettings.username password_hash = pylast.md5(lastfmsettings.password) from_date = "1501761600" #Edit these to run at different times - time is in unix to_date = "1502366399" #from_date = "1498867200" #to_date = "1501545599" timerange = datetime.datetime.fromtimestamp(int(from_date)).strftime('%Y-%m-%d %H:%M:%S') + " to " + datetime.datetime.fromtimestamp(int(to_date)).strftime('%Y-%m-%d %H:%M:%S') print("current time is: " + datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S')) print(timerange) usernames = [] for line in names: name = line.strip("\n").strip() if name not in usernames: usernames.append(name) else: print(name + " appeared twice.") names.close() network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash) for username in usernames: user = network.get_user(username) if setting == "album": try: chart = user.get_weekly_album_charts(from_date=from_date, to_date=to_date) except pylast.WSError: print(str(user) + " was not found.") continue else: parse_chart(chart, user) elif setting == "song": try: chart = user.get_weekly_track_charts(from_date=from_date, to_date=to_date) except pylast.WSError: print(str(user) + " was not found.") continue else: parse_chart(chart, user) chart_full_temp = reversed(sorted(chart_full.items(), key=operator.itemgetter(1))) rank = {} prev = None outputfile = open(setting + "_chart.txt", "w") outputfile.write("Rank|" + setting.title() + "|Total Points|Number Ones|Total Listeners\n---|---|---|---|---|---\n") for i,(k,v) in enumerate(chart_full_temp): if v!=prev: place,prev = i+1,v rank[k] = place for i in range(len(rank)): song = min(rank.items(), key=operator.itemgetter(1)) name = str(song[0]) outputfile.write(str(song[1]) + "|" + name + "|" + str(float(chart_full.get(name))) + "|") outputfile.write(str(number_ones[name]) + "|" + str(total_listeners[name]) + "\n") del rank[name] outputfile.close() print("current time is: " + datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S'))
def __init__(self, username, period, output_dir): self.metadata = [] self.network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET) self.user = self.network.get_user(username) self.albums = [item.item for item in self.user.get_top_albums(period)] self.output_dir = output_dir self.cover_art_path = os.path.join(output_dir, 'cover_art') if not os.path.exists(self.cover_art_path): os.mkdir(self.cover_art_path) for i, album in enumerate(self.albums): print("fetching artwork for album {}/{}".format( i, len(self.albums))) try: cover_art_url = album.get_cover_image() except: cover_art_url = None # force fallback case (check static dir) cover_art_exists = False matching_files = glob.glob( os.path.join( self.cover_art_path, "".join(x for x in album.title if x.isalnum()) + "*")) if len(matching_files) > 0: cover_art_output = matching_files[0] cover_art_exists = True elif cover_art_url: cover_art = requests.get(album.get_cover_image(), stream=True) extension = cover_art_url.split(".") if len(extension) < 2: extension = "" else: extension = extension[-1] cover_art_output = os.path.join( self.cover_art_path, "".join(x for x in album.title if x.isalnum()) + extension) with open(cover_art_output, 'wb') as f: cover_art.raw.decode_content = True copyfileobj(cover_art.raw, f) cover_art_exists = True else: for key, path in COVER_ART_SUBS.items(): if album.title.lower().find(key) != -1: extension = '.' + path.split('.')[-1] cover_art_output = os.path.join( self.cover_art_path, "".join(x for x in album.title if x.isalnum()) + extension) with open(path, 'rb') as src: with open(cover_art_output, 'wb') as dest: copyfileobj(src, dest) cover_art_exists = True break self.metadata.append({ "index": i, "album": album.title, "artist": album.artist.name, "url": urllib.parse.unquote(album.get_url()), "coverArt": os.path.relpath(cover_art_output, self.output_dir) if \ cover_art_exists else None })
def __init__(self): self.network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
https://gist.github.com/1241307 """ import logging import pylast import os import yaml from beets import plugins from beets import ui from beets.util import normpath, plurality from beets import config from beets import library log = logging.getLogger('beets') LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY) C14N_TREE = os.path.join(os.path.dirname(__file__), 'genres-tree.yaml') PYLAST_EXCEPTIONS = ( pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError, ) # Core genre identification routine. def _tags_for(obj): """Given a pylast entity (album or track), returns a list of tag names for that entity. Returns an empty list if the entity is not found or another error occurs.
twitter_api_key = config.twitter_api_key twitter_api_secret = config.twitter_api_secret twitter_access_token = config.twitter_access_token twitter_access_token_secret = config.twitter_access_token_secret # Spotify Token token = util.prompt_for_user_token(username=spotify_username, client_id=client_id, client_secret=client_secret, redirect_uri="http://localhost:8090", scope=scope) # LastFM API last_fm_api_key = config.lastfm_api_key last_fm_api_secret = config.lastfm_api_secret last_fm_username = config.lastfm_username # LastFM network objects network = pylast.LastFMNetwork(api_key=last_fm_api_key, api_secret=last_fm_api_secret, username=last_fm_username) user = network.get_user(last_fm_username) def dynamic_playlist_updater(playlist_id): top_tracks = [user.get_top_tracks(period="7day"), user.get_top_tracks(period="1month"), user.get_top_tracks(period="3month"), user.get_top_tracks(period="6month"), user.get_top_tracks(period="12month"), user.get_top_tracks(period="overall")] # TODO: user.getRecentTracks with time_ranges. Use same time range song weights. e.g. 1 week all 100 for each hit. 1 month (minus that week thats already been counted)
import pylast from key import * API_KEY = apiKey API_SECRET = secret username = uname password_hash = pylast.md5(pword) network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash) user = network.get_user(uname) playCount = network.get_user(uname).get_playcount() #Let's user see if i've listened to a artist. #If i have, it will return the latest song i listened to with that artist. #And the date i listened to the song. #Else return "I haven't listened to that artist". def listenedArtists(name): getArtistTracks = network.get_user(uname).get_artist_tracks(name) if len(getArtistTracks) > 0: return getArtistTracks[0][0], getArtistTracks[0][2] else: return "Nej jag har inte lyssnat på den här artisten" #Checks if im listening at music
def __init__(self): self.last_fm_api = pylast.LastFMNetwork( api_key=Config.LAST_FM_KEY, api_secret=Config.LAST_FM_SECRET)
formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument("user", help="User to follow along") parser.add_argument("sp_username", help="Your Spotify username") args = parser.parse_args() sp = spotipy.Spotify() token = util.prompt_for_user_token(args.sp_username, 'streaming user-library-read') if token: sp = spotipy.Spotify(auth=token) else: print("Can't get token for", args.sp_username) exit() network = pylast.LastFMNetwork(API_KEY, API_SECRET) if not os.path.exists(SESSION_KEY_FILE): skg = pylast.SessionKeyGenerator(network) url = skg.get_web_auth_url() print("Please authorize the scrobbler to scrobble to your account: {url}\n") import webbrowser webbrowser.open(url) while True: try: session_key = skg.get_web_auth_session_key(url) fp = open(SESSION_KEY_FILE, "w") fp.write(session_key)
def spotify(): # Start de Spotify client sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope)) playlists = { 'bruut': 'StuBru | Bruut', 'tijdloze': 'StuBru | De Tijdloze', 'hooray': 'StuBru | Hooray', 'belgisch': 'StuBru | #ikluisterbelgisch' } # Haal de playlijsten op van de huidige gebruiker user_playlists = sp.current_user_playlists() user_playlists_titles = [p['name'] for p in user_playlists['items']] # Maak een nieuwe playlist als ze niet bestaat for _, playlist in playlists.items(): if playlist not in user_playlists_titles: user_playlists['items'].append( sp.user_playlist_create(sp.me()['id'], playlist)) # Hervorm de playlijsten user_playlists = { p['name']: { 'id': p['id'], 'uri': p['uri'], 'href': p['href'] } for p in user_playlists['items'] } # Definieer een cleanup regex om songtitels op te kuisen cleanup = re.compile(r'(radio edit|\(|\)|live)') network = {} for stream in streams: network[stream] = pylast.LastFMNetwork( api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, username=LASTFM_USERNAME[stream], password_hash=pylast.md5(LASTFM_PASSWORD[stream])) # Haal de top 10 op van de laatste week tracklist = network[stream].get_authenticated_user().get_top_tracks( period=PERIOD_1MONTH, limit=15) # Zoek de nummer op spotify en sla hun id op nummers = [] for nummer, plays in tracklist: # Stop bij 10 nummers if len(nummers) >= 10: break # Haal de titel en artiest op titel, _ = cleanup.subn('', nummer.title) artiest = nummer.artist.name # Zoek het liedje op spotify try: resultaat = sp.search(artiest + ' ' + titel, type='track', limit=1) spotify_id = resultaat['tracks']['items'][0]['id'] except: # Sla het liedje over als het niet gevonden is continue else: # Voeg liedje toe aan de playlijst nummers.append(spotify_id) # Overschrijf alle nummers op de afspeellijst sp.playlist_replace_items(user_playlists[playlists[stream]]['id'], nummers)
def scrobble(): network = {} for stream in streams: network[stream] = pylast.LastFMNetwork( api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, username=LASTFM_USERNAME[stream], password_hash=pylast.md5(LASTFM_PASSWORD[stream])) logging.debug(network[stream].get_authenticated_user()) with requests.Session() as s: # Haal de stream op uit de status pagina r = s.get(status) # Controleer of je een geldige response kreeg if r.status_code == 200: json_data = r.json() # Loop over de verschillende streams for stream in streams: jsonpath_expr = parse( f'$.icestats.source[?server_name =~ "{stream.capitalize()}"].title' ) matches = jsonpath_expr.find(json_data) # Definieer artiest en nummer artiest, nummer = None, None # Haal de artiest en het nummer op for match in matches: m = re.search(r'(.*?) - (.*)', match.value) if m: artiest = m.group(1).lower() nummer = m.group(2).lower() logging.debug( f'StuBru {stream.capitalize()} - Artiest: {artiest} - Nummer: {nummer}' ) break # Controleer of een songtitel gevonden is if artiest is None and nummer is None: logging.debug( f'StuBru {stream.capitalize()} - Geen songtitel gevonden' ) continue # Scrobble de nummer naar last.fm user = network[stream].get_authenticated_user() current = network[stream].get_track(artiest, nummer) try: previous = user.get_recent_tracks(1, cacheable=False)[0].track except IndexError: network[stream].scrobble(artiest, nummer, timestamp=time.time()) logging.info( f'Nummer gescrobbled naar StuBru-{stream.capitalize()}: {artiest.capitalize()} - {nummer.capitalize()}' ) else: # Scrobble het nummer als het niet het laatste nummer is if current.get_correction() != previous.title: network[stream].scrobble(artiest, nummer, timestamp=time.time()) logging.info( f'Nummer gescrobbled naar StuBru-{stream.capitalize()}: {artiest.capitalize()} - {nummer.capitalize()}' ) else: logging.debug(f'Geen nieuw nummer...') # Update now-playing network[stream].update_now_playing(artiest, nummer)
def sync_to_trello(trello, secrets, song_objects, force_add=None): pw_hash = secrets['lastfm'].get('password_hash') if pw_hash is None: pw_hash = pylast.md5(secrets['lastfm']['password']) lfm = pylast.LastFMNetwork( api_key=secrets['lastfm']['api_key'], api_secret=secrets['lastfm']['secret'], username=secrets['lastfm']['username'], password_hash=pw_hash, ) concerts_board = [x for x in trello.me.boards if x.name == 'Concerts'][0] landing_list = [ x for x in concerts_board.lists if x.name == 'Coming to Town' ][0] archive_cards = [ x for x in landing_list.cards if arrow.get(x.badges['due']) < arrow.utcnow() ] for card in archive_cards: card.close() added_cards = [x for x in concerts_board.cards if '*UUID: ' in x.desc] existing_uuids = [] for card in added_cards: uuid_line = [x for x in card.desc.split('\n') if '*UUID: ' in x][0] existing_uuids.append(uuid_line.split('*UUID: ')[-1].split('*')[0]) for obj in song_objects: uuid_string = u"{} playing at {} on {}".format( obj['headliners'], obj['venue'], obj['date'].strftime('%X'), ) obj['uuid'] = uuid.uuid5(uuid_namespace, uuid_string).hex contents = get_synced_items() existing_uuids += contents['events'].keys() new_shows = [x for x in song_objects if x['uuid'] not in existing_uuids] if new_shows: print("Adding {} shows to trello".format(len(new_shows))) for show in new_shows: show['_internal'] = {} show['play_counts'] = {} show['_internal']['title'] = u"{} at {}".format( human_format_list(show['headliners']), show['venue'], ) description = [] if show['ticket_link']: description = [ u"[Click here to purchase tickets]({})".format( show['ticket_link']), "*Ticket Delivery Method*:", "", ] if show['age_restriction']: description += [ "**{}+ Event**".format(show['age_restriction']), "", ] if show['headliners']: description += [ u"Headliners:", u"-----------", trello_format_list(show['headliners']), "", ] if show['openers']: description += [ u"Opening Acts:", u"-------------", trello_format_list(show['openers']), "", ] if show['description']: description += [ show['description'], "", ] description += [ u"Venue: {}".format(show['venue']), u"", u"Date: {}".format(show['date'].isoformat()), ] description += [u"*UUID: {}*".format(show['uuid'])] show['_internal']['description'] = u"\n".join(description) all_artists = show['headliners'] + show['openers'] show_play_count = 0 for artist in all_artists: count = get_play_count(lfm, artist) show['play_counts'][artist] = count show_play_count = max(count, show_play_count) add_card = False if force_add: add_card = True labels = [] if show_play_count < 10: labels.append('green') elif show_play_count < 100: labels.append('yellow') add_card = True elif show_play_count < 500: labels.append('orange') add_card = True else: labels.append('red') add_card = True comments = [] if show_play_count < 100: for artist in all_artists: similar_artists = get_similar_artists(lfm, artist) max_count = 0 matches = [] for art in [ k for k, v in similar_artists.items() if v['match'] >= .75 ]: count = get_play_count(lfm, art) if count >= 10: matches.append(u"{} ({} plays)".format(art, count)) max_count = max(count, max_count) if matches: add_card = True comment = [ u"{} is similar to other bands you listen to:".format( artist), u"-------------------------------------", trello_format_list(matches), ] comments.append(u'\n'.join(comment)) if max_count >= 50: labels.append('sky') elif show_play_count < 500: labels.append('sky') else: labels.append('pink') if add_card: print(u"{} on {}".format( show['_internal']['title'], show['date'], )) card = landing_list.add_card(show['_internal']['title'], show['_internal']['description']) set_due_date(card, show['date']) for label in list(set(labels)): if label not in card._valid_label_colors: card._valid_label_colors.append(label) card.set_label(label) for comment in comments: card.add_comment(comment) show.pop('_internal') contents['events'][show['uuid']] = show contents['events'][show['uuid']]['date'] = show['date'].isoformat() if add_card: time.sleep(0.3) save_synced_items(contents)
class MyAPI (CrawlerAbstractAPI): """ Encapsulates the interactions for the API used in lab. There are people and tags. artist is bipartite 0. Tags bipartite 1. Variables """ # API_KEY="05e06f8849fea2cee2c0c55f88f4479d" # API_SECRET="d53664975702a7348c95f3e588f46ed7" # username = "******" # password_hash = pylast.md5("Bellsprout299!") API_KEY="f6b5317d9f966c37f7c21bdd554094a1" API_SECRET="bbcced560aede1de9fafe65fd10a340b" username = "******" password_hash = pylast.md5("CS299!") network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash) def initial_nodes(self): # initial_tag1 = '60s' # initial_artist1 = 'The Velvet Underground' # initial_artist2 = 'King Crimson' # initial_artist3 = 'The Beach Boys' # initial_artist4 = 'The Beatles' # initial_artist5 = 'John Coltrane' # initial_artist6 = 'The Doors' # initial_artist7 = 'Bob Dylan' # initial_artist8 = 'Mingus' # initial_artist9 = 'The Jimi Hendrix Experience' # initial_artist10 = 'Miles Davis' # initial_artist11 = 'Led Zeppelin' # initial_artist12 = 'Leonard Cohen' # initial_artist13 = 'Frank Zappa' # initial_artist14 = 'Love' # initial_artist15 = 'Pink Floyd' # initial_artist16 = 'The Zombies' # initial_artist17 = 'Nick Drake' # initial_artist18 = 'The Rolling Stones' # initial_artist19 = 'Neil Young with Crazy Horse' # initial_artist20 = 'Van Morrison' # initial_artist21 = 'The Kinks' # initial_artist22 = 'The Ronettes' # initial_artist23 = 'Pharoah Sanders' # initial_artist24 = 'Otis Redding' # initial_artist25 = 'Eric Dolphy' # init_60s = [] # init_60s.append((initial_tag1, self.make_node_tag(initial_tag1, 0))) # init_60s_artists = self.returnTopArtistsByTag(initial_tag1, 40) # for artist in init_60s_artists: # init_60s.append((artist, self.make_node_artist(artist, 0))) initial_tag2 = '70s' init_70s = [] init_70s.append((initial_tag2, self.make_node_tag(initial_tag2, 0))) init_70s_artists = self.returnTopArtistsByTag(initial_tag2, 40) for artist in init_70s_artists: init_70s.append((artist, self.make_node_artist(artist, 0))) # init_60s.append((initial_artist1, self.make_node_artist(initial_artist1, 0))) # init_60s.append((initial_artist2, self.make_node_artist(initial_artist2, 0))) # init_60s.append((initial_artist3, self.make_node_artist(initial_artist3, 0))) # init_60s.append((initial_artist4, self.make_node_artist(initial_artist4, 0))) # init_60s.append((initial_artist5, self.make_node_artist(initial_artist5, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist6, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist7, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist8, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist9, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist10, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist11, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist12, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist13, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist14, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist15, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist16, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist17, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist18, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist19, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist20, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist21, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist22, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist23, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist24, 0))) # init_60s.append((initial_artist6, self.make_node_artist(initial_artist25, 0))) # initial_tag2 = '70s' # initial_artist26 = 'Pink Floyd' # initial_artist27 = 'Led Zeppelin' # initial_artist28 = 'David Bowie' # initial_artist29 = 'Black Sabbath' # initial_artist30 = 'Joy Division' # initial_artist31 = 'Nick Drake' # initial_artist32 = 'Television' # initial_artist33 = 'King Crimson' # initial_artist34 = 'The Clash' # initial_artist35 = 'Bob Dylan' # initial_artist36 = 'Miles Davis' # initial_artist37 = 'Can' # initial_artist38 = 'Yes' # initial_artist39 = 'Neil Young' # initial_artist40 = 'The Beatles' # initial_artist41 = 'The Stooges' # initial_artist42 = 'The Rolling Stones' # initial_artist43 = 'The Who' # initial_artist44 = 'Eno' # initial_artist45 = 'Stevie Wonder' # initial_artist46 = 'Creedence Clearwater Revival' # initial_artist47 = 'Genesis' # initial_artist48 = 'Television' # initial_artist49 = 'Iggy and The Stooges' # initial_artist50 = 'Talking Heads' # init_70s = [] # init_70s.append((initial_tag2, self.make_node_tag(initial_tag2, 0))) # init_70s.append((initial_artist7, self.make_node_artist(initial_artist21, 0))) # init_70s.append((initial_artist8, self.make_node_artist(initial_artist22, 0))) # init_70s.append((initial_artist9, self.make_node_artist(initial_artist23, 0))) # init_70s.append((initial_artist10, self.make_node_artist(initial_artist24, 0))) # init_70s.append((initial_artist11, self.make_node_artist(initial_artist25, 0))) # init_70s.append((initial_artist12, self.make_node_artist(initial_artist26, 0))) # return either init_60s or init_70s depending on which graph print("init_70s") print(init_70s) print("top artists") print(init_70s_artists) # return init_60s return init_70s def returnTopArtistsByTag(self, tagName, lim): tag = self.network.get_tag(tagName) artists = [] artistDump = tag.get_top_artists(limit=lim) iterVar = 0 while (iterVar < len(artistDump)): artists.append(str(artistDump[iterVar][0])) iterVar = iterVar + 1 return artists def returnTopTagsByArtist(self, artistName, lim): artist = self.network.get_artist(artistName) tags = [] tagDump = artist.get_top_tags(limit=lim) iterVar = 0 while (iterVar < len(tagDump)): tags.append(str(tagDump[iterVar][0])) iterVar = iterVar + 1 return tags def retTopTagsWeight_Artist(self, artistName, lim): artist = self.network.get_artist(artistName) data = [] tagDump = artist.get_top_tags(limit=lim) iterVar = 0 while (iterVar < len(tagDump)): data.append((str(tagDump[iterVar][0]), str(tagDump[iterVar][1]))) iterVar = iterVar + 1 return data # def returnTopTracksByArtist(network, artistName, lim): # artist = network.get_artist(artistName) # tracks = [] # trackDump = artist.get_top_tracks(limit = lim) # iterVar = 0 # while (iterVar < lim): # tracks.append(str(trackDump[iterVar][0])) # iterVar = iterVar + 1 # return tracks def make_node_artist(self, artist, depth): """ Makes a node representing an artist Arguments id -- the node id (converted to a string) artist -- artist's name depth -- depth of the search to this point graph -- Graph object to add the node to """ nid = self.make_node(0, artist, depth) #Get playcount for artist art = self.network.get_artist(artist) playcount = art.get_playcount() #Add playcount to graph self._graph.nodes[nid]['playcount'] = playcount return nid def make_node_tag(self, tag, depth): """ Makes a node representing a tag Arguments id -- the node id (converted to a string) tag -- the tag string depth -- depth of the search to this point graph -- Graph object to add the node to """ nid = super().make_node(1, tag, depth) return nid def execute_artist_query(self, tag): """ Executes the names query and parses the results. Arguments tag -- a tag Returns (success flag, data) -- tuple success flag -- true if the values were successfully parsed (no errors) data -- a list of names that resulted from the query """ try: data = self.returnTopArtistsByTag(tag, 10) return (True, data) except ValueError as e: # Usually this means that the API call has failed print(e) return self._ERROR_RESULT except TypeError as e: print(e) return self._ERROR_RESULT def execute_tags_query(self, artist): """ Executes the tags query and parses the results. Arguments name -- an artist's name Returns (success flag, data) -- tuple success flag -- true if the values were successfully parsed (no errors) data -- a list of tags that resulted from the query """ try: data = self.returnTopTagsByArtist(artist, 10) return (True, data) except ValueError as e: # Usually this means that the API call has failed print(e) return self._ERROR_RESULT except TypeError as e: print(e) return self._ERROR_RESULT # These are ARTISTS bipartite 0 def get_child0(self, node, graph, state): artist = graph.node[node]['label'] success, data = self.execute_tags_query(artist) if success: # Distinguish nodes previously seen from new nodes old_tags = [tag for tag in data if state.is_visited(1, tag)] new_tags = [tag for tag in data if not (state.is_visited(1, tag))] # Get the existing nodes old_nodes = [state.visited_node(1, tag) for tag in old_tags] # Create the new nodes new_depth = graph.node[node]['_depth'] + 1 new_nodes = [self.make_node_tag(tag, new_depth) for tag in new_tags] # Return the dict with the info return {'success': True, 'new': new_nodes, 'old': old_nodes} else: return {'success': False} # These are TAGS bipartite 1 def get_child1(self, node, graph, state): tag = graph.nodes[node]['label'] success, data = self.execute_artist_query(tag) if success: # Distinguish nodes previously seen from new nodes old_artists = [artist for artist in data if state.is_visited(0, artist)] new_artists = [artist for artist in data if not (state.is_visited(0, artist))] old_nodes = [state.visited_node(0, artist) for artist in old_artists] new_depth = graph.node[node]['_depth'] + 1 new_nodes = [self.make_node_artist(artist, new_depth) for artist in new_artists] return {'success': True, 'new': new_nodes, 'old': old_nodes} else: return {'success': False}
import pandas as pd import pylast username = "******" password_hash = pylast.md5("dummypassword1!") network = pylast.LastFMNetwork( api_key="52557fe79157ddf53597b835faa25f92", api_secret="3a018cc75468cadeaa9fbfa4604dba5e", username=username, password_hash=password_hash, ) # returns dictionary of top 10 tags of artist and their weight def get_tags(artist, song): track = network.get_track(artist, song) topItems = track.get_top_tags(limit=8) d = {} for topItem in topItems: if "".join((str(topItem.item.get_name()).lower()).split()) != "".join( (str(artist).lower()).split()): item_clean = "".join(topItem.item.get_name().lower().replace( "-", "").split()) d.update({item_clean: topItem.weight}) return d # adds the top tags to dataframe def add_features_to_df(df): for i, row in df.iterrows(): try:
def __init__(self, config=None): self.network = pylast.LastFMNetwork(api_key=config['key'])
# Use spotipy package for maanaging access tokens. if config["spotifyClientId"] is None: raise Exception("Environment variable SPOTIFY_CLIENT_ID is required.") if config["spotifyClientSecret"] is None: raise Exception("Environment variable SPOTIFY_CLIENT_SECRET is required.") if config["lastFmKey"] is None: raise Exception("Environment varialbe LASTFM_API_KEY is required.") if config["lastFmSecret"] is None: raise Exception("Environment varialbe LASTFM_API_SECRET is required.") spotifyCredentials = SpotifyClientCredentials( client_id=config["spotifyClientId"], client_secret=config["spotifyClientSecret"], ) spotify = spotipy.Spotify(client_credentials_manager=spotifyCredentials) lastfm = pylast.LastFMNetwork(api_key=config["lastFmKey"], api_secret=config["lastFmSecret"]) class SpotifyTrack: def __init__(self, id, title, artist): self.id = id self.title = title self.artist = artist def __str__(self): return f"<[{self.id}] {self.title} ; {self.artist}>" class SpotifyFeatures: def __init__( self,
#!/usr/bin/env python3 import pylast import os last_fm = pylast.LastFMNetwork( api_key=os.environ["LAST_API_KEY"], api_secret=os.environ["LAST_API_SECRET"], username=os.environ["LAST_USERNAME"], password_hash=pylast.md5(os.environ["LAST_PASSWORD"]), ) print("Getting now playing...") track = last_fm.get_authenticated_user().get_now_playing() if track: print("Loving: ", track) track.love() else: print("Man, play something")
# Based on https://github.com/hugovk/lastfm-tools/blob/master/nowplaying.py import os import time import pylast from dotenv import load_dotenv from mastodon import Mastodon load_dotenv() SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".lastfm_session_key") mastodon = Mastodon(access_token=os.getenv('MASTODON_ACCESS_TOKEN'), api_base_url=os.getenv('MASTODON_HOST')) network = pylast.LastFMNetwork(os.getenv('LASTFM_KEY'), os.getenv('LASTFM_SECRET')) if not os.path.exists(SESSION_KEY_FILE): skg = pylast.SessionKeyGenerator(network) url = skg.get_web_auth_url() print( f"Please authorize the scrobbler to scrobble to your account: {url}\n") import webbrowser webbrowser.open(url) while True: try: session_key = skg.get_web_auth_session_key(url) fp = open(SESSION_KEY_FILE, "w")
def crop_center(pil_img, crop_width, crop_height): img_width, img_height = pil_img.size return pil_img.crop( ((img_width - crop_width) // 2, (img_height - crop_height) // 2, (img_width + crop_width) // 2, (img_height + crop_height) // 2)) def crop_max_square(pil_img): return crop_center(pil_img, min(pil_img.size), min(pil_img.size)) with open(path_to_git + "config.json", "r+") as f: config = json.load(f) network = pylast.LastFMNetwork(api_key=config['apikey'], api_secret=config['secret'], username=config['username'], password_hash=pylast.md5(config['password'])) cache = LastfmCache(config['apikey'], config['secret']) cache.enable_file_cache() try: artists = network.get_authenticated_user().get_top_artists( limit=6, period=pylast.PERIOD_7DAYS) top_artist = network.get_authenticated_user().get_top_artists( limit=6, period="12month") except Exception as e: print(e) artist_dict = {} top_artist_dict = {}
def lastfm_user(api_key, api_secret, user): network = pylast.LastFMNetwork(api_key=api_key, api_secret=api_secret, username=user) return network.get_user(user)
def networkdeclaration(self, username): network = pylast.LastFMNetwork(api_key=self.API_KEY, api_secret=self.API_SECRET, username=username, password_hash=None) return network
def _init_network(self): self.network = pylast.LastFMNetwork(api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, token=self.token)
def __init__(self, logger, lastfm_api_key, lastfm_api_secret): self._logger = logger self._lastfm = pylast.LastFMNetwork(api_key=lastfm_api_key, api_secret=lastfm_api_secret)
# if len(sys.argv) < 2: print("Usage: %s lastfm-crawler.config" % os.path.basename(sys.argv[0])) sys.exit(0) config = {} for line in open(sys.argv[1], 'r'): line = line.strip() (key, value) = line.split('=') config[key] = value logging.info("Connecting to Last.fm network.") network = pylast.LastFMNetwork(api_key=config["API_KEY"], api_secret=config["API_SECRET"], username=config["USERNAME"], password_hash=config["PASSWORD_MD5"]) logging.info("Creating SQLite database schema.") db = sqlite3.connect(config["SQLITE_DB"]) create_schema(db) seed_users.add(config["SEED_USERNAME"]) while len(users_expanded) < LIMIT and len(seed_users) > 0: start_user = seed_users.pop() if start_user in users_expanded: continue seed_users = seed_users.union(crawl_user_neighborhood(db, start_user)) users_expanded.add(start_user) db.close()
pool = mp.Pool(processes=n_cores) #start = time.time() work_res = pool.map_async(worker, jobs) if arrs==1: dists = np.array(map(itemgetter(1), sorted(itertools.chain(*work_res.get())))) elif arrs==2: dists = np.array(map(itemgetter(1,2), sorted(itertools.chain(*work_res.get())))) #print "distances calculated in %s" % str(datetime.timedelta(seconds=(time.time()-start))) pool.terminate() return dists ################################################# if scrape_data: network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET) artists = pd.read_table("data/vocab_idx",header=None,names=['artist','idx']) all_artist_names = set(artists['artist']) done = set() try: with open(outfile) as fin: for line in fin: done.add(line[:line.find('\t')]) except IOError: pass with open(outfile,'a') as out: for i,a in enumerate(artists['artist']):
def __init__(self): self.network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=SECRET_KEY) self.font = ImageFont.truetype(FONT_PATH, FONT_SIZE) self.user_cache = {}
def initLastFM(self): config = configparser.ConfigParser() config.read(CONFIG_PATH) API_KEY = config['DEFAULT']['API_KEY'] network = pylast.LastFMNetwork(api_key=API_KEY) return network
def get_events(self): """ Queries Eventful, LastFM, and Eventbrite APIs and returns events within an area based on geo parameters """ # Datetime format for event start date/time datetime_format = '%b %d, %Y -- %I:%M %p' lst_events = [] ### ----- Eventful ----- ### # Queries Eventful API and returns events within an area based on geo # parameters. Exception handling to prevent error in case no events # are found. api = eventful.API(Eventful_Key) try: eventful_events = api.call('/events/search', location=(str(self.latitude) + ',' + str(self.longitude)), within=self.miles, sort_order="date") except: eventful_events = dict({'events': None}) # Appends Eventful event details to 'lst_events' list. Two different # 'For Loops' are used because if only one event is found, it is returned # as a dict, otherwise it is returned as a list if eventful_events['events'] != None: if isinstance(eventful_events['events']['event'], list): for event in eventful_events['events']['event']: tmp_event = [] tmp_time = [] tmp_event.append(event['title'][:35]) tmp_event.append(event['venue_name'][:20]) # converts datetime format tmp_time = event['start_time'] tmp_time_adj = datetime.strptime( tmp_time, '%Y-%m-%d %H:%M:%S').strftime(datetime_format) tmp_event.append(tmp_time_adj) tmp_event.append(event['url']) lst_events.append(tmp_event) elif isinstance(eventful_events['events']['event'], dict): tmp_event = [] tmp_time = [] tmp_event.append( eventful_events['events']['event']['title'][:35]) tmp_event.append( eventful_events['events']['event']['venue_name'][:20]) tmp_time = eventful_events['events']['event']['start_time'] # converts datetime format tmp_time_adj = datetime.strptime( tmp_time, '%Y-%m-%d %H:%M:%S').strftime(datetime_format) tmp_event.append(tmp_time_adj) tmp_event.append(eventful_events['events']['event']['url']) lst_events.append(tmp_event) ### ----- LastFM ----- ### # Queries LastFM API and returns events within an area based on geo # parameters. Exception handling to prevent error in case no events # are found. network = pylast.LastFMNetwork(api_key=Last_fm_Key, api_secret=Last_fm_Secret) try: lastfm_events = network.get_geo_events(longitude=self.longitude, latitude=self.latitude, distance=self.miles) except: lastfm_events = [] # Appends LastFM event details to 'lst_events' list. Exception handling # in case no events are found or etracting certain event details creates # an error for i in range(len(lastfm_events)): try: tmp_event = [] tmp_time = [] tmp_event.append(lastfm_events[i].get_title()) tmp_event.append(lastfm_events[i].get_venue().get_name()) # converts datetime format tmp_time = lastfm_events[i].get_start_date() tmp_time_adj = datetime.strptime( tmp_time, '%a, %d %b %Y %H:%M:%S').strftime(datetime_format) tmp_event.append(tmp_time_adj) tmp_event.append(lastfm_events[i].get_url()) lst_events.append(tmp_event) except: continue ### ----- Eventbrite ----- ### # Queries Eventbrite API and returns events within an area based on geo # parameters. eventbrite = Eventbrite(Eventbrite_API) # Eventbrite API requires radius ('within' param) to be integer # not less than 1. eventbrite_within = max('1', str(int(float(self.miles)))) eventbrite_events = eventbrite.event_search( **{ 'location.within': eventbrite_within + "mi", 'location.latitude': str(self.latitude), 'location.longitude': str(self.longitude), 'popular': 'true', 'sort_by': 'date' }) # Appends Eventbrite event details to 'lst_events' list. Exception handling # in case no events are found or extracting certain event details creates # an error for i in range(len(eventbrite_events)): try: tmp_event = [] tmp_time = [] tmp_event.append( eventbrite_events['events'][i]['name']['text'][:35]) tmp_event.append( eventbrite_events['events'][i]['venue']['name'][:20]) # converts datetime format tmp_time = eventbrite_events['events'][i]['start']['local'] tmp_time_adj = datetime.strptime( tmp_time, '%Y-%m-%dT%H:%M:%S').strftime(datetime_format) tmp_event.append(tmp_time_adj) tmp_event.append(eventbrite_events['events'][i]['url']) lst_events.append(tmp_event) except: continue # Events in lst_events are sorted by chronological order return sorted(lst_events, key=lambda x: datetime.strptime(x[-2], datetime_format)) # x = GlocalAPI("1500 Massachusetts Ave NW", "washington","dc","1" ) # # # # x.get_instagram() # x.get_events() # # # # # y = GlocalAPI("","Sanaa","Yemen","10") # # # y.get_events() # # z = GlocalAPI("42 mar elias street","al-mina, tripoli", "lebanon","5") # z.get_events()
UPSTREAM_REPO_URL = os.environ.get( "UPSTREAM_REPO_URL", "https://github.com/ProThinkerGang/PhantomUserbot.git") # 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 = pylast.md5(LASTFM_PASSWORD_PLAIN) if not LASTFM_USERNAME == "None": lastfm = pylast.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("TEMP_DOWNLOAD_DIRECTORY", "./downloads") else: # Put your ppe vars here if you are using local hosting PLACEHOLDER = None
def init(self): self._client = pylast.LastFMNetwork(api_key=self.api_key, api_secret=self.api_secret, username=self.username) self._client.enable_caching() return self._client