Last active
March 15, 2022 22:35
-
-
Save sergiolucero/c3fa7207c0b5c9da4347d387cbbf4c73 to your computer and use it in GitHub Desktop.
Spotipy La Cuerda!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import spotipy, requests | |
from spotipy.oauth2 import SpotifyClientCredentials | |
def get_cuerda(artista, cancion): | |
martista = artista.lower().replace(' ','_') | |
mcancion = cancion.lower().replace(' ','_') | |
url = f'https://acordes.lacuerda.net/{martista}/{mcancion}' | |
bs = BeautifulSoup(requests.get(url).text, 'lxml') | |
links = [link['href'] for link in bs.find_all('a') if 'html' in link.get('href','')] | |
lurl = url.replace(url.split('/')[-1], links[0]) | |
bs2 = BeautifulSoup(requests.get(lurl).text, 'lxml') | |
song = bs2.find('div', attrs={'id':'t_body'}).find('pre') | |
return song | |
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) | |
latin = spotify.playlist('6LLPYHV2tIyKGHSdF7aBjV?si=f2ca4088e85d436c') # sergiolucero, LATIN_ALT | |
import pandas as pd | |
tget = lambda x: [track['track'][x] for track in latin['tracks']['items']] | |
pldf = pd.DataFrame(dict(name=tget('name'), | |
artist=tget('artists'), | |
duration=tget('duration_ms'))) | |
pldf['artist'] = pldf.artist.apply(lambda a: a[0]['name']) | |
def tegc(artista, track): | |
try: | |
return get_cuerda(artista, track) | |
except: | |
return '' | |
pldf['html'] = pldf.apply(lambda row: tegc(row['artist'], row['name']), axis=1) | |
pldf.sample(3 |
Author
sergiolucero
commented
Mar 15, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment