Created
October 31, 2015 05:57
-
-
Save andialbrecht/5604cf4968e6d0cd0fe0 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# Usage: python3 tonart2spotify.py DATE | |
# ...where DATE is for example '2015-10-30' | |
import sys | |
import requests | |
from bs4 import BeautifulSoup | |
import spotipy | |
# Your username on Spotify | |
USERNAME = 'INSERT_USERNAME_HERE' | |
# Read here on how to obtain a token: | |
# http://spotipy.readthedocs.org/en/latest/#authorized-requests | |
TOKEN = 'INSERT_TOKEN_HERE' | |
# Create a playlist on Spotify, open it and copy the ID from the URL | |
PLAYLIST_ID = 'INSERT_PLAYLIST_ID_HERE' | |
BASE_URL = 'http://www.deutschlandradiokultur.de/tagesplaylist-dkultur.279.de.html?drpl:date={}' | |
def parse_page(date): | |
response = requests.get(BASE_URL.format(date)) | |
soup = BeautifulSoup(response.content, 'html.parser') | |
ul = soup.find_all('ul', class_='drk-tagesplaylist')[0] | |
in_tonart = False | |
for li in ul.find_all('li'): | |
header = li.find('h3', class_='contentList') | |
if header and in_tonart: | |
break | |
elif header and header.text == 'Tonart': | |
in_tonart = True | |
if in_tonart: | |
entry = li.find('div', class_='drk-tagesplaylistright') | |
title = entry.find('td', text='Titel:').parent.find('strong').text | |
artist = entry.find( | |
'td', text='Interpret:').parent.find('data').text | |
yield(artist, title) | |
def main(date): | |
spoti = spotipy.Spotify(auth=TOKEN) | |
for artist, title in parse_page(date): | |
res = spoti.search(q='{} {}'.format(artist, title), type='track') | |
if res['tracks']['items']: | |
track = res['tracks']['items'][0]['id'] | |
spoti.user_playlist_add_tracks(USERNAME, PLAYLIST_ID, [track]) | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment