-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
294 lines (238 loc) · 9.97 KB
/
bot.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from config import *
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(tw_consumer_key, tw_consumer_secret)
auth.set_access_token(tw_access_token, tw_access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# our timezone
eastern = timezone('US/Eastern')
# this sends a tweet :)
def sendTweet(content):
api.update_status(content)
def formatName(name):
name = name.lower()
name = name.replace(" ", "")
name = name.replace("_", "")
name = name.replace("'", "")
name = name.replace("\"", "")
name = name.replace("(US)", "")
name = name.replace(",", "")
name = name.replace("(", "")
name = name.replace(")", "")
name = re.sub('\W', '', name)
return name
def findHandle(artistName):
searchAccounts = api.search_users(artistName,20,1)
artistName = formatName(artistName)
# print(artistName)
# print("#####")
maxScore = 0
maxHandle = "None"
for acj in searchAccounts:
thisScore = 0
account = acj._json
# print(account.keys())
thisScreenName = formatName(account['screen_name'])
thisName = formatName(account['name'])
# print("screen name: " + thisScreenName)
# print("name: " + thisName)
# check name
if (artistName == thisName):
# print("name perfect match +50")
thisScore = thisScore + 40
elif (artistName in thisName):
# print("name sub match +25")
thisScore = thisScore + 25
else:
thisScore = thisScore - 25
if ("band" in thisName):
# print("name band match +30")
thisScore = thisScore + 30
if ("music" in thisName):
# print("name music match +30")
thisScore = thisScore + 30
# check screen name
if (artistName == thisScreenName):
# print("screen name perfect match +25")
thisScore = thisScore + 25
elif (artistName in thisScreenName):
# print("screen name sub match +13")
thisScore = thisScore + 13
if ("band" in thisScreenName):
# print("screen name band match +15")
thisScore = thisScore + 15
if ("music" in thisScreenName):
# print("screen name music match +15")
thisScore = thisScore + 15
# check verified
if (account['verified'] == True):
# print("verified +20")
thisScore = thisScore + 10
# check followers
followersPoints = math.ceil(account['followers_count']/100)
if (followersPoints > 20):
followersPoints = 20
thisScore = thisScore + followersPoints
# print("followers: +" + str(followersPoints))
# if they have an image
if (account['default_profile_image']):
# print("no background image: -50")
thisScore = thisScore - 50
# see if they have a url
if (account['url'] != None):
try:
r = requests.head(account['url'], allow_redirects=True)
accountLink = r.url.lower()
musicSites = ['soundcloud''bandcamp','spotify','itunes','music']
for musicSite in musicSites:
if musicSite in accountLink:
# print("music site link " + musicSite + ": +50")
thisScore = thisScore + 30
# print(r.url)
except requests.exceptions.RequestException as e:
print(e)
# private/public
if (account['protected']):
# print("protected: -50")
thisScore = thisScore - 50
# check description
description = account['description'].lower()
descriptionWordList = ['mgmt', 'managment','band','inquiries','music','tour','album','stream','song','single', 'booking']
for word in descriptionWordList:
if word in description:
# print("description match " + word + ": +20")
thisScore = thisScore + 20
# look for fan accounts
if ("fan account" in description):
# print("fan account: -100")
thisScore = thisScore - 100
# print("total score: " + str(thisScore))
# print("----")
if (thisScore > maxScore):
maxScore = thisScore
maxHandle = account['screen_name']
if (maxScore > 99):
return maxHandle
else:
return "None"
# this runs every hour, it sends the first tweet that is qualified if there is one
def sendNextTweet(toTweet):
if (len(toTweet) != 0):
timeNow = datetime.now(eastern)
if (timeNow.hour < 21 and timeNow.hour > 7):
didWeTweet = False
for index,row in toTweet.iterrows():
# did we not already tweet something this hour and did it not already happen and did we not already tweet it?
if ((didWeTweet == False) and (row['concertTime'] > timeNow) and (row['tweeted'] == 0)):
didWeTweet = True
thisTweet = row['content']
handleResponse = findHandle(row['artistName'])
if (handleResponse == "None"):
thisTweet = row['artistName'] + thisTweet
else:
thisTweet = row['artistName'] + " @" + handleResponse + thisTweet
print(thisTweet)
sendTweet(thisTweet)
toTweet.loc[index,'tweeted'] = 1
else:
print("nothing to send")
return toTweet
def getVenueHandle(venueName):
venueHandle = "None"
venues = venue_name_dict.keys()
if venueName in venues:
venueHandle = venue_name_dict[venueName]
return venueHandle
# this runs once a day, it finds new artists in the area
def runBot(days, cityName, cityId, artistsWhoPlayedInDC):
upcomingShows = getUpcomingShows(days,cityId)
upcomingShows = upcomingShows[upcomingShows["locationCity"] == cityName]
upcomingShows.drop_duplicates(subset ="artistId", inplace = True)
contents = []
artistIds = []
artistNames = []
artistNames = []
concertTimes = []
eventDates = []
artistUrls = []
billingIndexes = []
tweetedes = []
for index, row in upcomingShows.iterrows():
artistId = row['artistId']
eventDate = row['eventDate']
concertTime = row['concertTime']
venueName = row['venueName']
eventUrl = row['eventUrl']
artistName = row['artistName']
billingIndex = row['billingIndex']
if artistId not in artistsWhoPlayedInDC:
if not wasArtistInCity(cityName, artistId):
# make the tweet string
dateString = datetime.strptime(eventDate, "%Y-%m-%d").strftime("%B") + " " + ordinal(datetime.strptime(eventDate, "%Y-%m-%d").day)
content = " is playing their first show in DC!"
venueHandle = getVenueHandle(venueName)
if (billingIndex == 1):
content = content + " They are headlining at " + venueName
else:
content = content + " They are opening at " + venueName
if venueHandle != "None":
content = content + " " + venueHandle + " on " + dateString + " " + eventUrl
else:
content = content + " on " + dateString + " " + eventUrl
print(content)
# fix times that are null with noon of the day of cencert so it goes first
if (concertTime == None):
concertTime = datetime.strptime(eventDate,"%Y-%m-%d")
concertTime = concertTime.replace(tzinfo=eastern)
concertTime = concertTime + timedelta(hours=(12))
else:
concertTime = datetime.strptime(concertTime,"%Y-%m-%dT%H:%M:%S%z")
concertTime = concertTime.replace(tzinfo=eastern)
# add one second per billing index
concertTime = concertTime - timedelta(seconds=(billingIndex - 1))
# add values to lists to make dataframe later
artistIds.append(artistId)
artistNames.append(artistName)
contents.append(content)
concertTimes.append(concertTime)
eventDates.append(eventDate)
billingIndexes.append(billingIndex)
tweetedes.append(0)
toTweetNew = pd.DataFrame(
{
"artistId" : artistIds,
"artistName" : artistNames,
"concertTime" : concertTimes,
"content" : contents,
"eventDate" : eventDates,
"billingIndex" : billingIndexes,
"tweeted" : tweetedes
})
return toTweetNew
def onceADay():
toTweet = readTable()
artistsWhoPlayedInDC = toTweet['artistId']
cityName = "Washington, DC, US"
cityId = "1409"
days = 3
toTweetNew = runBot(days,cityName,cityId,artistsWhoPlayedInDC)
# make sure we don't already have a artist
toTweetNew = toTweetNew[~toTweetNew['artistId'].isin(artistsWhoPlayedInDC)]
# combine results
toTweet = pd.concat([toTweet, toTweetNew], ignore_index=True)
# remove shows that are very old
twoWeeksAgo = datetime.now(eastern) - timedelta(weeks=(2))
toTweet = toTweet[toTweet['concertTime'] > twoWeeksAgo]
# sort by time
toTweet = toTweet.sort_values(by=['concertTime'], ascending=True)
# clear
clearTable()
# upload
writeTable(toTweet)
print("got artists")
def everyHour():
toTweet = readTable()
print("Start sending new tweet now")
toTweet = sendNextTweet(toTweet)
clearTable()
writeTable(toTweet)