Skip to content

Instantly share code, notes, and snippets.

@alexanderjackson
Created April 29, 2019 12:11
Show Gist options
  • Save alexanderjackson/a96d20c71a0618fcafc461ad0eac081d to your computer and use it in GitHub Desktop.
Save alexanderjackson/a96d20c71a0618fcafc461ad0eac081d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf8 -*-
__author__ = "Alexander Jackson"
__copyright__ = "Copyright 2019"
__credits__ = ["Michael J. Sanders"]
__license__ = "AGPL-3.0"
__version__ = "1.0.0"
__maintainer__ = "Alexander Jackson"
__email__ = "alexander.jackson@jackson-it.de"
__status__ = "Production"
"""Requirements
- Spotify on Linux with pulse-audio
- sp - Commandline spotify handler: https://gist.github.com/wandernauta/6800547
- stream_recorder.pl - https://snippets.geertvandeweyer.be/index.php?page=read&id=45, https://bitbucket.org/geertvandeweyer/spotify_recorder/src/e3b77d94e60d890bb1eb395a521f6620032da063/stream_recorder.pl?at=default&fileviewer=file-view-default
Make sure to disable the album repeat feature. Otherwise it will contniously record the first album..
"""
from selenium import webdriver
from collections import OrderedDict
from os.path import expanduser
import os, sys, time
start = time.time()
if len(sys.argv) > 1:
artist = sys.argv[1]
else:
print 'You must provide a search term. Example: spotify-album-recorder.py "Taylor Swift"'
quit()
# Kill all processes
os.system("killall spotify >/dev/null 2>&1")
os.system("pkill -f 'stream_recorder.pl' >/dev/null 2>&1")
os.system("spotify >/dev/null 2>&1&")
workdir = expanduser("~") + "/spotify-album-recorder/"
existcount = 0
newcount = 0
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1200')
options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
driver = webdriver.Chrome(chrome_options=options)
# Selenium script to scroll to the bottom, wait 3 seconds for the next batch of data to load, then continue scrolling.
# It will continue to do this until the page stops loading new data.
# Found at https://michaeljsanders.com/2017/05/12/scrapin-and-scrollin.html
def scroll_to_bottom():
lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
match = False
while(match==False):
lastCount = lenOfPage
time.sleep(3)
lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
if lastCount==lenOfPage:
match = True
# Searching for artist
driver.get('https://open.spotify.com/search/artists/%s' % artist)
# Scrolling to bottom of artist search page. Ensures page is fully loaded..
scroll_to_bottom()
# Searching for artists page
for a in driver.find_elements_by_xpath('.//a'):
if "open.spotify.com/artist/" in a.get_attribute('href'):
albums = a.get_attribute('href')
# Loading artists page
driver.get(albums)
# Scrolling to bottom of artists page. Ensures page is fully loaded..
scroll_to_bottom()
# Checking for albums on artists page
unfiltered = []
for a in driver.find_elements_by_xpath('.//a'):
if "open.spotify.com/album/" in a.get_attribute('href'):
unfiltered.append(a.get_attribute('href'))
# Remove any duplicates
filtered = list(OrderedDict.fromkeys(unfiltered))
# Print how results
print "Found", len(filtered), "albums for", artist
# Start recording found albums
albumIDlist = []
for album in filtered:
albumID = album.replace('https://open.spotify.com/album/', '')
albumIDlist.append(albumID)
for albumID in sorted(albumIDlist):
lockdir = workdir + ".lock/" + albumID
if os.path.exists(lockdir):
print ("Directory for album %s exists. Skipping.." % albumID)
existcount = existcount+1
else:
print ("Recording album %s " % albumID)
os.makedirs(lockdir)
os.system("stream_recorder.pl --silent --format flac --outdir '%s' --uri 'spotify:album:%s'" % (workdir, albumID))
newcount = newcount+1
end = time.time()
# Print stats
print "We found", existcount+newcount, "albums for the search term \"",artist,"\".", existcount, "contained a album ID lock under", workdir + ".lock/.", newcount, "where newly recorded. The whole process took", end-start, "seconds"
# Quit
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment