-
Notifications
You must be signed in to change notification settings - Fork 40
/
water_thread_wrapup.py
113 lines (91 loc) · 2.55 KB
/
water_thread_wrapup.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
# Tally the votes in the daily water thread
import os
import praw
import re
import time
import sys
from datetime import datetime as dt
import gpio_out as g
import config as c
import post_templates as posts
# Set-up
r = c.getReddit()
sr = c.getSubReddit(r)
path = c.pathPrefix()
if c.checkKillSwitch() == 1:
sys.exit()
with open(path+'daily_thread.txt', 'r+') as f:
thread = f.read()
f.close()
s = r.get_submission(submission_id = thread)
s.lock()
s.unsticky()
# pull ALL top-level comments and calculate score
# Takes FOREVVVVEEEERRR
print "more comments starting"
s.replace_more_comments(limit = None, threshold = 0)
comments = s.comments
def get_comment_score(comment):
yes = re.search(r'\byes\b', x.body, re.IGNORECASE) or re.search(r'\baye\b', x.body, re.IGNORECASE)
no = re.search(r'\bno\b', x.body, re.IGNORECASE) or re.search(r'\bnot on your nelly\b', x.body, re.IGNORECASE)
if yes and no:
return 0
elif yes:
return 1
elif no:
return -1
else:
return 0
def reply_to_vote(comment, score=None):
score = get_comment_score(comment) if (not score) else score
if score == 0:
return None
elif score == 1:
comment.reply("thanks, I've recorded your vote to water!")
elif score == -1:
comment.reply("thanks, I've recorded your vote to not water!")
# Record comments
recorded_yes = {}
recorded_no ={}
for x in comments:
try:
score = get_comment_score(x)
tuple_log = (s.id, x.name, x.author.name, x.body, score)
if score == 1:
d = recorded_yes
elif score == -1:
d = recorded_no
else:
continue
if not d.has_key(tuple_log[2]):
d[tuple_log[2]] = tuple_log
reply_to_vote(x, tuple_log[4])
except:
continue
total = len(recorded_yes) - len(recorded_no)
self_text = s.selftext
wrapup = posts.body_edit.format(posts.now_formatted, len(recorded_yes), len(recorded_no))
wrapup = self_text + "\n **** \n" + wrapup
s.edit(wrapup)
with open(path+'yes_votes.txt', 'a+') as f:
f.write(str(recorded_yes))
f.close()
with open(path+'no_votes.txt', 'a+') as f:
f.write(str(recorded_no))
f.close()
if total > 0:
with open(path+'topup.txt', 'w') as f:
f.write('1')
f.close()
g.on_off(15)
else:
with open(path+'topup.txt', 'w') as f:
f.write('0')
f.close()
outcome = 'yes' if total > 1 else 'no'
dow = posts.now_pst.strftime('%A')
history_record = ','.join([dow, outcome, s.id])
with open(path+'history.txt', 'a+') as f:
new_row = str(history_record+"\n")
f.write(new_row)
f.close()