-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_spotipy.py
196 lines (131 loc) · 5.73 KB
/
spotify_spotipy.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
import spotipy, json
from string import ascii_uppercase
''' To fetch distinct artists name, ID, and image if exist, with alpha order.'''
auth_manager = spotipy.oauth2.SpotifyClientCredentials(requests_timeout=20)
sp = spotipy.Spotify(auth_manager=auth_manager)
''' To fetch distinct artists name, ID, and image if exist, with alpha order.'''
# data = {}
# data['Artists'] = []
# for letter in ascii_uppercase:
# for i in range(20):
# try:
# s = sp.search(q=letter, limit=50, offset=(50*i), type= "artist")
# for i in range(len(s['artists']['items'])):
# try:
# s['artists']['items'][i]['images'][0]['url']
# image = s['artists']['items'][i]['images'][0]['url']
# except:
# image = 'no_image'
# artist = {
# 'name' : s['artists']['items'][i]['name'],
# 'id' : s['artists']['items'][i]['id'],
# 'image_url' : image
# }
# if artist not in data['Artists']:
# data['Artists'].append(artist)
# except:
# continue
# with open('artists_backup.txt', 'w') as f:
# json.dump(data, f, indent= 4)
""" To fetch top artists songs """
artists_info = {}
with open('artists.txt','r') as f:
r = json.loads(f.read())
for i in r['Artists']:
artists_info[i['name']] = i['id']
# num = 0
songs = {}
f = open('songs.txt', 'w')
f.write('{')
for name in artists_info:
artist_name = name
songs[artist_name] = [] # add a key to the album name ? if top song does not exists ?
f.write(f'\n\t"{name}": [,')
s = sp.artist_top_tracks(artist_id= artists_info[artist_name])
for track in s['tracks'][:10]:
# f.write(track['name']+"\n")
f.write(",\n\t\t\t\"{}\"".format( track['name'] ))
f.write("\n\t\t],")
# break
# try :
# s = sp.artist_top_tracks(artist_id= artists_info[artist_name])
# for i in range(len(s['tracks'])):
# album_id = s["tracks"][i]['album']['id']
# album_track = sp.album_tracks(album_id, limit=50, offset=0, market=None)
# for i in range(len(album_track['items'])):
# # print(album_track['items'][i]['name'])
# songs[artist_name].append(album_track['items'][i]['name'])
# # print(songs[artist_name])
# f.write(",\n\t\t\t\"{}\"".format( album_track['items'][i]['name'] ))
# f.write("\n\t\t],")
# except: # to catch the name
# last_artist = name
# with open('arch.txt', 'w') as r:
# r.write('the last name is ' + name)
# f.write("\n}")
# f.close()
# with open('songs.txt', 'w') as f:
# json.dump(songs, f, indent= 4)
# class Spotifyy:
# def __init__(self):
# auth_manager = spotipy.oauth2.SpotifyClientCredentials(requests_timeout=20)
# sp = spotipy.Spotify(auth_manager=auth_manager)
# def fetch_distinct_artist(self, filename):
# # data = {}
# # data['Artists'] = []
# # for letter in ascii_uppercase:
# # for i in range(20):
# # try:
# # s = self.sp.search(q=letter, limit=50, offset=(50*i), type= "artist")
# # for i in range(len(s['artists']['items'])):
# # try:
# # s['artists']['items'][i]['images'][0]['url']
# # image = s['artists']['items'][i]['images'][0]['url']
# # except:
# # image = 'no_image'
# # artist = {
# # 'name' : s['artists']['items'][i]['name'],
# # 'id' : s['artists']['items'][i]['id'],
# # 'image_url' : image
# # }
# # if artist not in data['Artists']:
# # data['Artists'].append(artist)
# # except:
# # continue
# # with open(filename, 'w') as f:
# # json.dump(data, f, indent= 4)
# def fetch_artists_songs(self, filename2):
# artists_info = {}
# with open('artists.txt','r') as f:
# r = json.loads(f.read())
# for i in r['Artists']:
# artists_info[i['name']] = i['id']
# # num = 0
# songs = {}
# for name in artists_info:
# artist_name = name
# songs[artist_name] = [] # add a key to the album name ? if top song does not exists ?
# try :
# s = self.sp.artist_top_tracks(artist_id= artists_info[artist_name])
# ''' I have to organize the data so every artists appear
# with the top songs or list of all songs in a given album '''
# # with open('songs.txt', 'w') as f:
# # print(artist_name, end= ' ')
# # print(len(s['tracks']))
# # print()
# for i in range(len(s['tracks'])):
# album_id = s["tracks"][i]['album']['id']
# album_track = self.sp.album_tracks(album_id, limit=50, offset=0, market=None)
# for i in range(len(album_track['items'])):
# # print(album_track['items'][i]['name'])
# songs[artist_name].append(album_track['items'][i]['name'])
# except: # to catch the name
# last_artist = name
# with open('arch.txt', 'w') as f:
# f.write('the last name is ' + name)
# # if num == 5:
# # break
# # else:
# # num +=1
# with open(filename2, 'w') as f:
# json.dump(songs, f, indent= 4)