-
Notifications
You must be signed in to change notification settings - Fork 40
/
continuous_tally.py
143 lines (115 loc) · 3.48 KB
/
continuous_tally.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
# Currently, in the workNewComments function, it handles comment loading well
# It will load comments sequentially and breaks when at where it left off
# Does not handle 1/vote per person due to 'voter' list not updating with
# each iteration of the loop through s.comments
import pickle
import json
import re
import time
import sys
import praw
import post_templates as posts
import config as c
# Setup
r = c.getReddit()
sr = c.getSubReddit(r, False)
path = c.pathPrefix()
## Get necessary files
# Comment log, in case one exists (vestigial file)
try:
with open(path+'continuous_tally/cont_comment_log.txt', 'r+') as f:
try:
processed = pickle.load(f)
except EOFError:
processed = []
f.close()
except:
processed = []
print str(processed)
# Similar to above, if there is already a comment, it's written here
try:
with open(path+'continuous_tally/cont_comment_id.txt', 'r') as f:
update_comment = f.read()
f.close()
except:
update_comment = False
# Get the submission -- written by Daily Water thread script
with open(path+'daily_thread.txt', 'r+') as f:
thread = f.read()
f.close()
## Functions
def get_comment_score(x):
yes = re.search(r'\byes\b', x.body, re.IGNORECASE)
no = re.search(r'\bno\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("1")
elif score == -1:
comment.reply("2")
def workNewComments(submission, record):
done = [x[1] for x in record]
voters = [x[2]+str(x[4]) for x in record]
for x in s.comments:
if not type(x) is praw.objects.Comment:
s.get_more_comments(limit=10)
workNewComments(s, record)
if x.name in done:
print('id')
break
score = get_comment_score(x)
if x.author.name+str(score) in voters:
print('name')
continue
if score == 0:
continue
tuple_log = (s.id, x.name, x.author.name, x.body, score)
#reply_to_vote(x, score)
record = [tuple_log] + record
voters = [x.author.name+str(score)] + voters
return record
def getScores(record=processed):
y = 0
n = 0
for x in processed:
if x[4] == 1:
y += 1
elif x[4] == -1:
n += 1
return y, n
def makeBars(yes, no):
yes_pct = yes*100/(yes + no)
no_pct = 100 - yes_pct
yes_bar = "`Y`: `"+"|"*(yes_pct/2)+"` (%d)"%(yes)
no_bar = "`N `: `"+"|"*(no_pct/2)+"` (%d)"%(no)
return yes_bar, no_bar
# WHO RUN IT
s = r.get_submission(submission_id = thread)
print s.id
processed = workNewComments(submission=s,record=processed)
yes, no = getScores()
yes_bar, no_bar = makeBars(yes, no)
continuous_score_body = posts.continuous_vote_display%(yes_bar, no_bar)
print continuous_score_body
print update_comment
if not update_comment:
update = s.add_comment(continuous_score_body)
update_comment = update.name
else:
update = r.get_info(thing_id = update_comment)
update.edit(continuous_score_body)
update.distinguish(sticky=True)
with open(path+'continuous_tally/cont_comment_log.txt', 'w') as f:
pickle.dump(processed, f)
with open(path+'continuous_tally/cont_comment_id.txt', 'w') as f:
f.write(update.name)