-
Notifications
You must be signed in to change notification settings - Fork 3
/
howstat.py
126 lines (108 loc) · 4.35 KB
/
howstat.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
#!/usr/bin/env python
# Main howstat module.
# Resident /r/Cricket Stats generator.
# Pranav Ravichandran <me@onloop.net>
import praw
from utils import Mapper, PlayerFinder, Prettifier
from time import sleep
from user import username, password
# Get the stats from the utils module helpers.
# Creates a response based on the data received.
def fetch_stats(request):
# Create a mapper instance
init = Mapper()
# Create an URL mapping using the input request.
try:
mapped = init.map_string(request)
except:
return "Uh, like..I mean, like..really..like, y'know...What?"
# Find the player using the player name in the request.
try:
player_url = PlayerFinder(init.player_name)
except:
return "Sorry, the service seems to be unavailable right now."
# Scrape and parse the statistics for the corresponding player.
try:
zeroed_in = player_url.zero_in()
if not player_url.test_player:
base_url = zeroed_in.replace("class=11;", "")
else:
base_url = zeroed_in.replace("class=1;", "")
except:
return "I couldn't find that, sorry."
# Create a Prettifier instance if it's a valid stats url.
try:
if base_url[-1] == ";":
base_url += mapped
prettifier = Prettifier(base_url, player_url.test_player)
else:
return base_url
except:
return base_url
# Format the content for a reddit comment.
try:
final = prettifier.prettify(init.class_allround)
except:
return request + ":\n\n" + "Ouch, nothing to see here, I think. " + \
"You can check out the [records](%s)." % base_url
# Url for complete stats.
elaborate = "Detailed Stats [here.](%s)" % base_url
return request + ':\n\n' + final + '\n\n' + elaborate
# Check if howstat has already replied to the comment.
def dealt_with(comment):
try:
for reply in comment.replies:
if str(reply.author) == 'howstat':
return True
except:
pass
return False
if __name__ == "__main__":
r = praw.Reddit(user_agent = "Howstat v 1.0 by /u/pranavrc"
"http://github.com/pranavrc/howstat/")
r.login(username, password)
subreddit = r.get_subreddit('cricket')
footer = "\n^(/u/howstat - Resident /r/Cricket Statbot. ) " + \
"^(Check out the) [^code](http://github.com/pranavrc/howstat/) " + \
"^(and the) [^(HOW TO.)](http://redd.it/1i7lh3) " + \
"^(For testing, use) [^(THIS THREAD)](http://redd.it/1i7lh3) ^(please.)"
#old_list = []
last_comment_time = 0
while True:
# Get the latest comments from /r/cricket.
latest_comments = [cmt for cmt in subreddit.get_comments()]
#new_list = []
for comment in latest_comments:
if any(x in comment.body for x in ['howstat', 'Howstat']) \
and not dealt_with(comment) \
and str(comment.author) != "howstat" \
and not comment.created_utc <= last_comment_time:
response = ""
#pending_comments.append(comment)
request_limit = 1
for each_line in comment.body.split('\n'):
if each_line.strip()[0:7] in ['howstat', 'Howstat']:
if request_limit <= 5:
pass
else:
# More than 5 requests in one comment.
response += '\n\nOnly five requests per Comment, sorry.' + \
'\n\n_____\n\n'
break
request = each_line.replace('howstat', '').\
replace('Howstat','').strip('., ')
response += fetch_stats(request) + '\n\n_____\n\n'
request_limit += 1
if response:
response += footer
try:
#comment.upvote()
#print response
last_comment_time = comment.created_utc
comment.reply(response)
#new_list.append(comment.id)
#pending_comments.remove(comment)
except:
continue
#old_list = new_list
#sleep(20)