forked from theopolisme/theobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsr_notifier.py
192 lines (161 loc) · 4.4 KB
/
bsr_notifier.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
import mwclient
import re
import sys
import datetime
import pickle
from theobot import bot
from theobot import password
# CC-BY-SA Theopolisme
# Task 9 on [[User:Theo's Little Bot]]
def sokay(donenow):
"""This function calls a subfunction
of the theobot module, checkpage().
"""
if donenow >= 55:
return False
if donenow % 5 == 0:
if bot.checkpage("User:Theo's Little Bot/disable/bsr") == True:
return True
else:
return False
else:
return True
def notified_already(user):
""" This function makes sure that
a user doesn't currently have a notification
waiting on their talk page. Returns TRUE
if user hasn't been notified.
"""
usertalk = 'User talk:' + user
page = site.Pages[usertalk]
text = page.edit()
if text.find('<!-- Template:Bsr-user -->') == -1:
return True
else:
return False
def already_told(user):
"""This checks if the user has been
already notified in the past three
days.
"""
now = datetime.datetime.now()
try:
lop = users_notified.index(user)
except (ValueError):
print "User was not in list!"
return False
lop = lop + 1
xyz = users_notified[lop]
if (now - xyz) > datetime.timedelta(days=7):
print "Time greater than 7 days, so we can renotify."
return False
else:
print "User already notified in 7 days."
return True
def notify(user):
"""The function checks that a number of
conditions are true, then notifies the user
in question. Calls generate_subst().
"""
usertalk = 'User talk:' + user
if already_told(user) == False:
if bot.nobots(page=usertalk) == True:
print "We're cleared to edit."
if notified_already(user) == True:
print "User hasn't already been notified!"
page = site.Pages[usertalk]
text = page.edit()
text = text + generate_subst(user)
#text = generate_subst(user)
try:
page.save(text,summary="Notifying user about file(s) with inadequate source information ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/bsr|disable]])")
global total_done_now
total_done_now = total_done_now + 1
global users_notfied
users_notified.append(user)
now123 = datetime.datetime.utcnow()
users_notified.append(now123)
print user.encode('ascii', 'ignore') + " notified"
except:
print "Unknown error // skipping user."
else:
print "User was notifed already."
else:
print "Bot denied!"
else:
print "User has already been notified in past 7 days."
def add_to_dict(user,file):
"""The function adds an item to a list in a dictionary
with user as the key.
"""
if user not in to_notify:
f_l = []
f_l.append(file)
global to_notify
to_notify[user] = f_l
else:
f_l = to_notify[user]
f_l.append(file)
to_notify[user] = f_l
def generate_subst(user):
"""This function creates the
wikicode for the substituted
template, by appending text to
a base string.
"""
files = to_notify[user]
subst = u'\n{{subst:Bsr-user'
for file in files:
subst = subst + u'|' + file
subst = subst + u'}} ~~~~'
return subst
def main():
"""This defines and fills a global
variable for the site, and then runs.
"""
print "Logging in as " + password.username + "..."
global site
site = mwclient.Site('en.wikipedia.org')
site.login(password.username, password.password)
try:
global picklefile
picklefile = open("bsr_notified.txt", 'r')
print picklefile
global users_notified
users_notified = pickle.load(picklefile)
picklefile.close()
except (EOFError, IOError, NameError):
print "Pickle file was empty...we'll have to make do!"
users_notified = []
global to_notify
to_notify = {}
global picklefile_write
picklefile_write = open("bsr_notified.txt", 'w')
files = bot.what_transcludes('Bsr')
global total_done_now
total_done_now = 0
for file in files:
x = site.images[file]
zam = x.revisions(dir='newer',prop='user')
try:
rrev = zam.next()
except:
print file.encode('ascii', 'ignore') + " skipped!"
continue
zammy = rrev['user']
print "Adding " + file.encode('ascii', 'ignore') + " to " + zammy.encode('ascii', 'ignore') + "'s list now."
add_to_dict(zammy,file)
print "Now, let's run through those lists."
donenow = 5
for key in to_notify:
if sokay(donenow) == True:
notify(key)
donenow = donenow + 1
elif sokay(donenow) == False:
pickle.dump(users_notified, picklefile_write)
sys.exit()
print "All done, pickling."
pickle.dump(users_notified, picklefile_write)
print "Pickled!"
if __name__ == '__main__':
main()