-
Notifications
You must be signed in to change notification settings - Fork 1
/
cm_api.py
95 lines (83 loc) · 3.9 KB
/
cm_api.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
import requests
import re
def get_api_token(REFRESH_TOKEN):
api_url = 'https://api.chartmetric.com/api/token'
response = requests.post(url=api_url, data={'refreshtoken' : REFRESH_TOKEN},
json={'Content-Type' : 'application/json'})
results = response.json()
api_token = results['token']
return api_token
def get_track_metadata(api_token, cm_track_id):
#cm_track_id refers to the chartmetric track id associated with the song
#returns a dictionary('id', 'name', 'isrc', 'image_url', 'duration_ms', 'composer_name',
#'artists', 'albums', 'tags', 'cm_audio_features', 'release_date', 'lastfm', 'cm_statistics')
response = requests.get(url='https://api.chartmetric.com/api/track/{}'.format(cm_track_id),
headers={'Authorization' : 'Bearer {}'.format(api_token)}
)
if response.status_code == 200:
data = response.json()
track = data['obj']
else:
pass
return track
def get_chart_data(api_token, cm_track_id, chart_type, date):
#refer to https://api.chartmetric.com/apidoc/#api-Track-getTrackCharts for allowed values for chart_type
#date == YYYY-MM-DD
#returns a list of dictionaries with chart data for each date
response = requests.get(url='https://api.chartmetric.com/api/track/{}/{}/charts'.format(cm_track_id, chart_type),
headers={'Authorization' : 'Bearer {}'.format(api_token)}, params={'since': date}
)
if response.status_code == 200:
data = response.json()
chart = data['obj']
return chart['data']
def get_tiktok_chart_data(api_token, chart_type, date, interval):
#for apitoken import get_import_token
#for chart_type, accepted values include 'tracks', 'videos', 'users'
#date == YYYY-MM-DD
#for interval, accepted values include 'daily', 'weekly'
#returns a list of song metadata
response = requests.get(url='https://api.chartmetric.com/api/charts/tiktok/{}'.format(chart_type),
headers={'Authorization' : 'Bearer {}'.format(api_token)}, params={'date': date, 'interval': interval}
)
if response.status_code == 200:
data = response.json()
chart = data['obj']
return chart['data']
def get_artist_id(api_token, q, search_type):
#return tuple (artist name, artist cm id)
response = requests.get(url='https://api.chartmetric.com/api/search',
headers={'Authorization' : 'Bearer {}'.format(api_token)}, params={'q': q, 'type': search_type}
)
if response.status_code == 200:
data = response.json()
try:
chart = data['obj']
for artist in chart['artists']:
if re.fullmatch(q.lower(), artist['name'].lower()):
# print(artist['name'])
# print('Chartmetric ID: ',artist['id'])
return artist['id']
else:
continue
except TypeError:
return "None"
else:
print(response.status_code)
print(response.text)
def get_fan_metrics(api_token, cm_artist_id, source, since_date, field):
#returns a list of dictionaries, each item being a different timestamp
response = requests.get(url='https://api.chartmetric.com/api/artist/{}/stat/{}'.format(cm_artist_id, source),
headers={'Authorization' : 'Bearer {}'.format(api_token)}, params={'since': since_date, 'field': field}
)
if response.status_code == 200:
data = response.json()
try:
chart = data['obj']
return chart
except TypeError:
return "None"
else:
print(cm_artist_id)
print(response.status_code)
print(response.text)