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
|
def main():
try:
conn = pymysql.connect(host=host, user=username, passwd=password, db=database, port=port, use_unicode=True, charset='utf8')
cursor = conn.cursor()
except:
logging.error('could not connect to RDS')
sys.exit(1)
headers = get_headers(client_id, client_secret)
## Spotify Search api
params = {
'q': 'BTS',
'type': 'artist',
'limit': '1'
}
r = requests.get('https://api.spotify.com/v1/search', params=params, headers=headers)
raw = json.loads(r.text)
artist_raw = raw['artists']['items'][0]
if artist_raw['name'] == params['q']:
artist = {
'id': artist_raw['id'],
'name': artist_raw['name'],
'followers': artist_raw['followers']['total'],
'popularity': artist_raw['popularity'],
'url': artist_raw['external_urls']['spotify'],
'image_url': artist_raw['images'][0]['url']
}
query = """
INSERT INTO artists (id, name, followers, popularity, url, image_url)
VALUES ('{}', '{}', {}, {}, '{}', '{}')
ON DUPLICATE KEY UPDATE id='{}', name='{}', followers={}, popularity={}, url='{}', image_url='{}'
""".format(
artist['id'],
artist['name'],
artist['followers'],
artist['popularity'],
artist['url'],
artist['image_url'],
artist['id'],
artist['name'],
artist['followers'],
artist['popularity'],
artist['url'],
artist['image_url']
)
cursor.execute(query)
conn.commit()
|