-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattingavgbot.py
More file actions
executable file
·75 lines (68 loc) · 2.85 KB
/
battingavgbot.py
File metadata and controls
executable file
·75 lines (68 loc) · 2.85 KB
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
#! /usr/bin/env python3
import sys
import pprint as pp
import re
from twython import Twython, TwythonStreamer
CONSUMER_KEY = 'Put It Here'
CONSUMER_SECRET = 'Put It Here'
ACCESS_KEY = 'Put It Here'
ACCESS_SECRET = 'Put It Here'
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
expr = r'What is my Twitter batting average?'
def compute_batting_average(username):
tweet_count = 0
hits = 0
user_timeline = twitter.get_user_timeline(screen_name=username,
include_rts=0,
count=200)
min_id = min([tweet['id'] for tweet in user_timeline])
for tweet in user_timeline:
min_id = min_id if min_id < tweet['id'] else tweet['id']
tweet_count = tweet_count+1
tweet_favorites = tweet['favorite_count']
tweet_retweets = tweet['retweet_count']
if tweet_favorites >= 1 or tweet_retweets >= 1:
hits = hits + 1
tweet_favorites = 0
tweet_retweets = 0
while(user_timeline):
user_timeline = twitter.get_user_timeline(screen_name=username,
include_rts=0,
count=200,
max_id = min_id-1)
for tweet in user_timeline:
min_id = min_id if min_id < tweet['id'] else tweet['id']
tweet_count = tweet_count+1
tweet_favorites = tweet['favorite_count']
tweet_retweets = tweet['retweet_count']
if tweet_favorites >= 1 or tweet_retweets >= 1:
hits = hits + 1
tweet_favorites = 0
tweet_retweets = 0
try:
average = hits/tweet_count
except ZeroDivisionError:
average = None
return average
class MyStreamer(TwythonStreamer):
def on_success(self,data):
if 'text' in data:
username = data['user']['screen_name']
if username != "USER NAME OF BOT":
if re.search(expr, data['text']):
print("Computing average for {0}".format(username))
average = compute_batting_average(username)
print("Average computed for {0}".format(username))
if average:
handle = "@{0} ".format(username)
average_str = "Your average is {0:.3}".format(average)
twitter.update_status(status=handle+average_str)
else:
handle = "@{0} ".format(username)
average_err = "Average couldn't be calculated, sorry!"
twitter.update_status(status=handle+average_err)
def on_error(self, status_code, data):
print(status_code)
self.disconnect()
stream = MyStreamer(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)
stream.user()