-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
247 lines (180 loc) · 7.66 KB
/
setup.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import sqlite3
import os
import json
import requests
import youtube
#File for Grading!
"""
Takes in the database cursor and connection as inputs.
Returns: None.
Creates the Genres Table we will utilize to obtain more data.
Uses Napster API to get 23 total genres.
"""
def obtain_genres(cur, conn):
response = requests.get('https://api.napster.com/v2.0/genres?apikey=OGU2ZWQxNjEtZTI5Yi00MzM1LWE0YTgtNDg5ODZhMjhhZDJm')
r = response.text
data = json.loads(r)
cur.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='Genres'")
if cur.fetchone()[0]==1:
return
cur.execute("CREATE TABLE Genres(table_id INTEGER PRIMARY KEY, genre TEXT, genre_id INTEGER)")
count = 1
for item in data['genres']:
cur.execute("INSERT INTO Genres (table_id, genre, genre_id) VALUES (?, ?, ?)", (count, item['name'], item['id']))
count += 1
conn.commit()
"""
Takes in the database cursor and connection as inputs.
Returns: None
Creates & adds to the NapsterTopArtists Table.
Uses Napster API to get reoccuring top artists for genres
previously found.
Uses Youtube API to get # of subscribers for each artist name found.
"""
def obtain_artists(cur, conn, round):
cur.execute("SELECT COUNT(*) FROM Genres")
total_genres = cur.fetchone()[0]
cur.execute("CREATE TABLE IF NOT EXISTS NapsterTopArtists(artist_id INTEGER PRIMARY KEY, name TEXT, genre_id INTEGER, subscribers INTEGER)")
cur.execute("SELECT COUNT (*) FROM NapsterTopArtists")
total_artists = cur.fetchone()[0] #Get current number of artists
base_url = 'https://api.napster.com/v2.2/genres/{}/artists/top?apikey=OGU2ZWQxNjEtZTI5Yi00MzM1LWE0YTgtNDg5ODZhMjhhZDJm'
for g in range(0, 25):
genre = total_artists % total_genres #this should be the current genre ID
genre_id = genre + 1
request_str = "SELECT genre_id from Genres where table_id={}"
format_str = request_str.format(str(genre_id))
cur.execute(format_str)
genre_id = cur.fetchone()[0]
# get desired data from API using genre
request_url = base_url.format(genre_id)
response = requests.get(request_url)
r = response.text
data = json.loads(r)
# select artist
temp = total_artists
count = 0
while temp > 22:
temp = temp - 23
count = count + 1
artist = data['artists'][int(count)]
table_genre_id = genre + 1
subscribers = youtube.subscribers_for_artist(artist['name'])
if subscribers is None:
print("No subscribers")
total_genres = total_genres - 1
#since this artist should be skipped, increase genre count +1 in order to fit the correct genre number for the next artist. Prevent an infinite loop or program being stopped.
continue
cur.execute("INSERT OR IGNORE INTO NapsterTopArtists(artist_id, name, genre_id, subscribers) VALUES (?, ?, ?, ?)", (total_artists, artist['name'], table_genre_id, subscribers))
total_artists = total_artists + 1
cur.execute("SELECT COUNT(*) FROM Genres")
total_genres = cur.fetchone()[0] #reset this parameter since the artist is back on track
conn.commit()
"""
Takes in the database cursor and connection as inputs.
Creates & adds to the TopTracks Table in .db.
Uses ITunes API to search for top artist
names from NapsterTopArtists and retrieves their top song,
music video view count, song price, and length of song.
"""
def topTrackForArtist(cur, conn):
#uses names from table for searching purposes
cur.execute("SELECT name, artist_id FROM NapsterTopArtists")
all_artists = cur.fetchall()
cur.execute("CREATE TABLE IF NOT EXISTS TopTracks(artist_id INTEGER, top_track TEXT, view_count INTEGER, track_price INTEGER, track_length INTEGER)")
cur.execute("SELECT COUNT (*) FROM TopTracks")
total_tracks = cur.fetchone()[0]
check_artists = all_artists[total_tracks:]
for name in check_artists:
check_url = "SELECT COUNT(*) from TopTracks WHERE artist_id={}"
format_check = check_url.format(name[1])
cur.execute(format_check)
if cur.fetchone()[0] != 0:
continue
#replaces the spaces in names with '+' for Itunes API term
person = name[0].replace(" ", "+")
#search for the content with full URL w/ correct parameter keys (escapes '&' character in artist name)
request_url = 'https://itunes.apple.com/search?term={}&entity=musicArtist&limit=10'.format(person.replace("&", "%26"))
#get data from API
response = requests.get(request_url)
if not response.ok:
return None
r = response.text
data = json.loads(r)
artistid = None
unavailable = False
for i in data["results"]:
if i["artistName"].lower() == name[0].lower():
if ("amgArtistId" not in i):
unavailable = True
else:
artistid = i["amgArtistId"]
break
if unavailable:
continue
request_url = 'https://itunes.apple.com/lookup?amgArtistId={}&entity=song&limit=5'.format(artistid)
response = requests.get(request_url)
if not response.ok:
return None
r = response.text
data = json.loads(r)
found = False
for i in data["results"]:
if i["wrapperType"] == "track":
track = i['trackName']
found = True
break
if not found:
continue
request_url = 'https://itunes.apple.com/lookup?amgArtistId={}&entity=song&limit=5'.format(artistid)
response = requests.get(request_url)
if not response.ok:
return None
r = response.text
data = json.loads(r)
found = False
for i in data["results"]:
if i["wrapperType"] == "track":
price = i['trackPrice']
found = True
break
if not found:
price = 0 # CHANGED THIS
request_url = 'https://itunes.apple.com/lookup?amgArtistId={}&entity=song&limit=5'.format(artistid)
response = requests.get(request_url)
if not response.ok:
return None
r = response.text
data = json.loads(r)
found = False
for i in data["results"]:
if i["wrapperType"] == "track":
length = i['trackTimeMillis']
found = True
if not found:
length = 0 # CHANGED THIS
viewCount = youtube.viewcount_for_track(track)
if viewCount == None:
print("No viewcount")
continue
#print(viewCount)
cur.execute("INSERT OR IGNORE INTO TopTracks(artist_id, top_track, view_count, track_price, track_length) VALUES (?, ?, ?, ?, ?)", (name[1], track, viewCount, price, length))
conn.commit()
"""
Main function for this file, calls all functions to collect data and store into the music database.
"""
def setUp():
path = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(path, 'round.txt')
infile = open(full_path,'r', encoding='utf-8')
round = infile.readline()
infile.close()
conn = sqlite3.connect(path+'/'+'music.db')
cur = conn.cursor()
obtain_genres(cur, conn)
obtain_artists(cur, conn, round)
topTrackForArtist(cur, conn)
outfile = open(full_path,'w', encoding='utf-8')
num = str(round)
outfile.write(num)
outfile.close()
setUp()