Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reginaldojunior/005b4702a8b73494432ba070350a2ce9 to your computer and use it in GitHub Desktop.
Save reginaldojunior/005b4702a8b73494432ba070350a2ce9 to your computer and use it in GitHub Desktop.
Using Selenium+Python sign in to Spotify, grab liked tracks, sign in to Google Music, add Spotify tracks if missing.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.implicitly_wait(10)
#open spotify
driver.get('https://open.spotify.com/collection/tracks')
#sign in with Facebook
driver.find_element_by_css_selector('.btn-black.btn--no-margin.btn--full-width').click()
driver.find_element_by_css_selector('.btn.btn-block.btn-facebook.ng-binding').click()
#sign in
#username
driver.find_element_by_id('email').send_keys('')
#password
driver.find_element_by_id('pass').send_keys('')
driver.find_element_by_id('loginbutton').click()
#open all liked tracks
driver.get('https://open.spotify.com/collection/tracks')
#scroll to the bottom of the page
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(0.5)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
#get all tracks titles
songs = []
titles = driver.find_elements_by_xpath('//div[@class="tracklist-name ellipsis-one-line"]')
artists = driver.find_elements_by_xpath('//div[@class="second-line"]')
for title, artist in zip(titles, artists):
t = title.text
a = artist.text.split('\n')[0]
songs.append(t + ' ' + a)
exp = ' EXPLICIT'
for s1 in songs:
if exp in s1:
ind = songs.index(s1)
s2 = s1.split(exp)[0]
songs[ind] = s2
else:
pass
#open Google Music
driver.get('https://play.google.com/music/listen')
#sign in
driver.find_element_by_css_selector('.material-primary.x-scope').click()
#username
driver.find_element_by_id('identifierId').send_keys('')
driver.find_element_by_css_selector('.RveJvd.snByac').click()
time.sleep(0.5)
#password
driver.find_element_by_xpath('//*[@name="password"]').send_keys('')
driver.find_element_by_xpath('//*[@name="password"]').send_keys(Keys.ENTER)
time.sleep(1)
#search for tracks from Spotify's list and add them if they are missing in the Google's Music list
gMusicSearch = driver.find_element_by_id('input')
notAddedSongs = []
for s in songs:
gMusicSearch.send_keys((Keys.CONTROL)+(Keys.SHIFT)+(Keys.HOME))
gMusicSearch.send_keys(s)
gMusicSearch.send_keys(Keys.ENTER)
time.sleep(1)
try:
driver.find_element_by_xpath('//*[@class="column-content tooltip"]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//*[@data-id="menu"]').click()
add = driver.find_element_by_id(':a')
if add.is_displayed():
add.click()
else:
pass
except:
notAddedSongs.append(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment