-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (52 loc) · 1.72 KB
/
main.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
62
import time
import os
import datetime
from dotenv import load_dotenv
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
def get_dates(start_date="2017-1-1"):
"""
Returns array of dates from Jan 1st 2017 to yesterday
@param start_date Defaults to 2017-1-1
"""
# Spotify data starts Jan 1st 2017
today = datetime.datetime.today()
yesterday = today - datetime.timedelta(days=1)
dates = pd.date_range(start_date, yesterday).tolist()
return dates
def main():
# Load ENV variables
load_dotenv()
username = os.getenv("SPOTIFY_USERNAME")
password = os.getenv("SPOTIFY_PASSWORD")
# Start Chrome driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Login to Spotify Charts
driver.get(
"https://charts.spotify.com")
time.sleep(1)
elem = driver.find_element(By.CLASS_NAME, "jEPoVx")
elem.click()
elem = driver.find_element(By.ID, "login-username")
elem.send_keys(username)
elem = driver.find_element(By.ID, "login-password")
elem.send_keys(password + Keys.RETURN)
time.sleep(1)
# Collect CSVs
dates = get_dates()
for date in dates:
# Format date to match URL scheme
date = date.strftime("%Y-%m-%d")
print("Collecting data for", date)
# Collect CSV
driver.get(
"https://charts.spotify.com/charts/view/regional-us-daily/" + date)
time.sleep(2)
elem = driver.find_element(By.CLASS_NAME, "jvhDJQ")
elem.click()
time.sleep(2)
main()