-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_top_songs.py
61 lines (43 loc) · 1.92 KB
/
get_top_songs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#spotify automation
# saves top songs of the month to a text file
import time
import numpy as np
import selenium
import json
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from secrets import spotify_user_id, spotify_password
userid = str(spotify_user_id)
password = str(spotify_password)
time.sleep(1)
driver = webdriver.Chrome(ChromeDriverManager().install())
PATH = "/users/rebridge/Spotify Automation/chromedriver"
driver.get("https://accounts.spotify.com/en/authorize?client_id=09793e3afafa4797bf853f2ad5db72f3&redirect_uri=http://107.170.81.187:8080/SpotifyLoginCallback/&response_type=code&scope=user-read-recently-played+user-library-read+user-top-read&show_dialog=false&state=34fFs29kd09")
username = driver.find_element_by_id('login-username')
username.send_keys(str(userid))
time.sleep(1)
passwordlogin = driver.find_element_by_id('login-password')
passwordlogin.send_keys(str(password))
time.sleep(1)
login = driver.find_element_by_id('login-button')
login.click()
#if statement for first time (shoould only fire if certain url is active)
#this section is onlt needed for a new log in
if driver.current_url == 'https://accounts.spotify.com/en/authorize?client_id=09793e3afafa4797bf853f2ad5db72f3&redirect_uri=http:%2F%2F107.170.81.187:8080%2FSpotifyLoginCallback%2F&response_type=code&scope=user-read-recently-played%20user-library-read%20user-top-read&show_dialog=false&state=34fFs29kd09' :
time.sleep(3)
agree = driver.find_element_by_id('auth-accept')
agree.click()
else :
time.sleep(3)
#refreshes webpage
driver.refresh()
time.sleep(2)
#saves songs
tracks_element = driver.find_element_by_xpath('/html/body/ol[4]')
tracks = tracks_element.text
songs = [tracks]
songs_json = json.dumps(songs)
# saves songs to .txt file
np.savetxt("top_songs_of_the_month.txt", np.array(songs), fmt="%s")
driver.close()
# will save the songs to a text doc in the same directory as this file