-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
116 lines (90 loc) · 3.7 KB
/
script.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import datetime
import os
import requests
import spotipy
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from spotipy.oauth2 import SpotifyOAuth
now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
def createDataFolder():
if os.path.exists('data/') != True:
os.mkdir('data/')
def readFilesInDataFolder():
filecontents = ''
for files in os.listdir('data/'):
with open('data/' + files) as f:
filecontents += f.read()
scrapeJuno(filecontents, genreSelector(), timeSpanSelector())
def genreSelector():
selection = input('''
Select genre by typing associated number:
1 - Drum and Bass
2 - Hard Techno
3 - Trance
Your selection: ''')
if selection == '1':
return 'drumandbass'
if selection == '2':
return 'hard-techno'
if selection == '3':
return 'trance-music'
def timeSpanSelector():
selection = input('''
Select time lapse by typing associated number:
1 - Last Week
2 - Last 2 Weeks
3 - Last 4 Weeks
4 - Last 8 Weeks
Your selection: ''')
if selection == '1':
return 'this-week'
if selection == '2':
return 'two-weeks'
if selection == 3:
return 'four-weeks'
if selection == '4':
return 'eight-weeks'
def scrapeJuno(filecontents, genre, timespan):
url = 'https://www.junodownload.com/' + genre + '/' + timespan + '/releases/?items_per_page=100'
soup = BeautifulSoup(requests.get(url).text, features='html.parser')
while len(soup.find_all('a',{'title':'Next Page'})) > 0:
soup = BeautifulSoup(requests.get(url).text, features='html.parser')
file = open('data/' + now + '.txt','a',encoding='utf-8')
for data in soup.find_all('div', {'class':'col-12 col-md order-4 order-md-3 mt-3 mt-md-0 pl-0 pl-md-2'}):
artist = data('div', {'class':'col juno-artist'})[0].get_text()
album = data('a', {'class':'juno-title'})[0].get_text().replace('(Explicit)','')
label = data('a', {'class':'juno-label'})[0].get_text()
if data('div', {'class':'col pl-2 jq_highlight'}):
tracks = data('div', {'class':'col pl-2 jq_highlight'})[0].get_text().rsplit('-', 1)[0]
entry = 'label:"' + label + '" ' + album
with open('data/' + now + '.txt'):
if entry not in filecontents:
file.write(entry)
file.write('\n')
if len(soup.find_all('a',{'title':'Next Page'})) > 0:
url = soup.find('a',{'title':'Next Page'})['href']
def addToSpotifyPlaylist():
load_dotenv()
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
playlist_id = os.getenv('PLAYLIST_ID')
scope = 'playlist-modify-private,playlist-modify-public'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id,client_secret,redirect_uri='http://localhost:8888/callback',scope=scope))
file = open('data/' + now + '.txt',encoding='utf-8')
lines = file.readlines()
for line in lines:
if sp.search(q=line, limit=20,type='track')['tracks']['items']:
searchresults = sp.search(q=line, limit=20,type='track')['tracks']['items']
for item in searchresults:
if item['album']['name'] in line:
albumid = item['album']['id']
albumtracks = sp.album_tracks(albumid)['items']
for tracks in albumtracks:
playlistadd = sp.playlist_add_items(playlist_id,[tracks['uri']],position=None)
break
def main():
createDataFolder()
readFilesInDataFolder()
addToSpotifyPlaylist()
if __name__ == '__main__':
main()