-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot5.py
71 lines (55 loc) · 2.65 KB
/
bot5.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
#This bot collects data on top 100 trending tracks on Tiktok
#for the current week. Then it finds the artist with the
#biggest weekly gain in Spotify daily listeners.
import tweepy
from cm_config import config
from cm_api import *
from helper_funct import *
from helper_funct1 import *
from twitter_bot import *
import random
import decimal
# ################################################
# generate api token for chartmetric api access
rt = config['refresh_token']
api_token = get_api_token(rt)
################################################
current_date = generate_today_date()
before_date = generate_one_week_prior_date()
data = get_tiktok_chart_data(api_token, 'tracks', current_date, 'weekly', limit=100)
#parse data into dataframe (columns='title', 'artist', 'isrc', 'velocity', 'cm_id')
parsed_data = parse_tiktok_data(data)
#get artist id for each artist
id_bucket = []
for artist in parsed_data['artist']:
artist_id = get_artist_id(api_token, artist, 'artists')
id_bucket.append(artist_id)
time.sleep(1.5)
# #create artist ID feature to dataframe
parsed_data['cm_artist_id'] = id_bucket
# #drop rows with no ID
parsed_data2 = parsed_data.dropna(subset=['cm_artist_id'])
parsed_data2.reset_index(inplace=True)
parsed_data2 = parsed_data2[~parsed_data2['cm_artist_id'].isin( ['None'])].reset_index()
# # #collect before and after listener values for each artist
listener_bucket = []
for artist in parsed_data2['cm_artist_id']:
listeners = get_fan_metrics(api_token, artist, 'spotify', before_date, current_date, field='listeners')['listeners']
if len(listeners) > 0:
follow_tuple = (listeners[0]['value'], listeners[-1]['value'])
listener_bucket.append(follow_tuple)
else:
follow_tuple = (None, None)
listener_bucket.append(follow_tuple)
complete_data = parsed_data2.join(pd.DataFrame(listener_bucket, columns=['before', 'after']))
complete_data['listener_diff'] = complete_data['after'] - complete_data['before']
complete_data.drop(axis=1, columns=['level_0', 'index'], inplace=True)
title, artist, artist_id, before, listener_diff = get_most_listener_gain(complete_data)
hashartist = artist.replace(" ", "",)
hashtitle = title.replace(" ", "",)
#get spotify url for artist
spot_url = get_spotify_url(api_token, artist_id)
#instantiatiate twitter bot object
bot = instantiate_twitter_bot()
message = "Out of all the artists trending on this week's top 100 Tiktok tracks,\n{} had the biggest gain in Spotify listeners\nUp {}% since last week\n#{} #{} #DataAnalytics #MusicDiscovery\nPower by @Chartmetric\n{}".format(artist, round(listener_diff/before *100, 2),hashtitle, hashartist,spot_url)
bot.update_status(message)