Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Created September 13, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riebschlager/1e0787e7290dced24ddfdf6041d3dad7 to your computer and use it in GitHub Desktop.
Save riebschlager/1e0787e7290dced24ddfdf6041d3dad7 to your computer and use it in GitHub Desktop.
Create Spotify playlists based on the last 24 hours of music played by The Bridge - https://bridge909.org/
import spotipy
import spotipy.util as util
import time
import urllib
import urllib2
import json
from datetime import datetime
from datetime import timedelta
user = 'dimintech'
scope = 'playlist-modify-public'
token = util.prompt_for_user_token(user, scope)
spotify = spotipy.Spotify(auth=token)
playlist = spotify.user_playlist_create(
user, 'The Bridge - ' + datetime.now().strftime("%m-%d-%Y"))
def add_to_playlist(tracks):
for track in tracks:
if track['title'] != '' and 'KTBG' not in track['artist']:
query = 'artist:' + track['artist'] + ' track:' + track['title']
results = spotify.search(q=query, type='track')
if len(results['tracks']) > 0:
items = results['tracks']['items']
# time.sleep(1)
if len(items) > 0:
print items[0]['name'] + ' - ' + items[0]['artists'][0]['name']
spotify.user_playlist_add_tracks(
user, playlist['id'], [items[0]['id']])
def fetch_tracks(url):
response = urllib2.urlopen(url)
track_list = json.load(response)
add_to_playlist(track_list['events'])
now = int(time.time()) - 1000
then = now - 86000
fetch_tracks('https://ktbg.streamon.fm/eventrange/' +
str(then) + '-' + str(now) + '.json')
@riebschlager
Copy link
Author

Requirements:

requests==2.10.0
spotipy==2.3.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment