/
fc.py
353 lines (340 loc) · 19.1 KB
/
fc.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Featured article candidates and featured list candidates archiver and promoter
# By Vacation9 (Samuel Damashek)
import datetime
import urllib2 as url
import urllib
import ClientCookie
import re
import sys
import socket
import string
import os
import io
from xml.sax import saxutils as su
from logininfo import * # import various libraries and the login info
from includes import *
usernameKey = 'lgname'
passwordKey = 'lgpassword' # Define POST variable names
loginUrl = "http://en.wikipedia.org/w/api.php?format=xml&action=login" # Define request URL
loginInformation = {usernameKey: uname, passwordKey: password}
# Creates a request dictionary
loginInformatione = urllib.urlencode(
loginInformation) # Encode the login information.
loginRequest = url.Request(loginUrl, loginInformatione)
# Creates a Request that is used to login
loginResponse = ClientCookie.urlopen(
loginRequest) # Sends the login to Wikipedia and stores the PHP session ID.
loginResponse = loginResponse.read() # Get the HTML/XML from Wikipedia.
loginToken = loginResponse.split('token="')[
1].split('" cookie')[0] # Get 1st login token
loginInformation["lgtoken"] = loginToken # Add token to dictionary
loginInformatione = urllib.urlencode(
loginInformation) # Re-encode with new login token
loginRequest = url.Request(loginUrl, loginInformatione)
# Login again (required by API), passing the login
# token this time
loginResponse = ClientCookie.urlopen(
loginRequest) # Get the cookie from the login request
loginResponse = loginResponse.read()
# Set the response to the .read of the
# response (parsable)
print loginResponse # Print login response for debugging
x = ClientCookie.urlopen("http://en.wikipedia.org/w/api.php?action=tokens&format=xml").read().split(
'edittoken="')[1].split('" />')[0] # Get edit token - required to edit the template
print x # Print the token for debugging
# ~~~GLOBAL FUNCTIONS~~~#
montharray = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
month = montharray[datetime.date.today().month - 1]
year = datetime.date.today().year
print year
def getContent(page):
url = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvlimit=1&format=xml&titles=" + \
page.replace(" ", "_")
print url
pagecontent = ClientCookie.urlopen(url).read()
flcre = re.compile(r'(?<=preserve">).*?(?=</rev>)', re.DOTALL)
flccontent = re.findall(flcre, pagecontent)[0]
return flccontent
def rmGA(page):
facats = [
"Agriculture, food and drink", "Art and architecture", "Engineering and technology", "Geography and places", "History", "Language and literature", "Mathematics",
"Music", "Natural sciences", "Philosophy and religion", "Social sciences and society", "Sports and recreation", "Theatre, film and drama", "Video games", "Warfare"]
for facat in facats:
catcontent = getContent("Wikipedia:Good articles/" + facat)
newcontent = catcontent.replace(page + "\n", "")
if newcontent != catcontent:
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Removing FA promoted page from Good articles category", "text": newcontent.unescape(
updatedTalk).replace(""", "\""), "bot": "true", "title": "Wikipedia:Good articles/" + facat})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
# ~~~FLC~~~#
flcapproved = ["The Rambling Man",
"Dabomb87", "Giants2008", "NapHit", "Hahc21"]
#Users who are allowed to approve/disapprove FLC candidates
# ~~~FLC PROMOTIONS~~~#
# ~~~FLC FUNCTIONS~~~#
def flcProcess(link, user, diff, oldlink, result):
flccontent = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=" + link.replace(" ", "_")).read()
if r"<!--FLtop-->" not in flccontent:
# if True:
print link
print user
print diff
print oldlink
flcre = re.compile(r"""(?<=preserve">).*?(?=</rev>)""", re.DOTALL)
flccontent = re.findall(flcre, flccontent)[0]
flccontent = "{{subst:User:Hahc21/FLTop|result=" + result + "|closer=" + \
user + "|time=~~~~~|link=diff=" + diff + \
"&oldid=" + oldlink + "}}\n" + flccontent
flccontent = flccontent + "{{subst:User:Hahc21/FLBottom}}"
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Archiving FLC Nomination", "text": su.unescape(
flccontent).replace(""", "\""), "bot": "true", "title": link.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
flcpage = re.findall(
r"(?<=Featured list candidates/).*?(?=/archive)", link.replace("_", " "))[0]
print flcpage
if result == "promoted":
pagecontent = getContent(flcpage)
pagecontent = "{{featured list}}\n" + pagecontent
if "#REDIRECT" in pagecontent:
print "redirecting"
flcpage = re.findall(
r"(?<=#REDIRECT \[\[).*?(?=\]\])", pagecontent)[0]
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode(
{"format": "xml", "action": "edit", "token": x, "summary": "Adding Featured List Star", "text": su.unescape(pagecontent).replace(""", "\""), "title": flcpage})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
talkUrl = "Talk:" + flcpage
talkPage = getContent(talkUrl)
if result == "promoted":
talkPage = talkPage.replace("|class=list", "|class=FL")
talkPage = re.sub(r"{{featured list candidates.+?}}", "", talkPage)
if len(re.findall(r"{{[Aa]rticle ?[Hh]istory", talkPage)) > 0:
print talkPage
latestgan = re.compile(
"action[0-9]+ *?=.+?date *?=.+?[0-9][0-9][0-9][0-9]", re.DOTALL).findall(talkPage)[-1]
print latestgan
latestgannumber = str(
int(re.findall(r"(?<=action)[0-9]+?(?=date)", latestgan)[0]) + 1)
articleHistoryStart = re.findall(
re.compile(r"{{[Aa]rticle ?[Hh]istory.*?(?=\| ?currentstatus)", re.DOTALL), talkPage)[0]
actionTime = time.strftime(
"%H:%M, %d %B %Y", (time.gmtime(time.time())))
updatedTalk = talkPage.replace(articleHistoryStart, articleHistoryStart + "\n|action" + latestgannumber + "=FLC" + "\n|action" + latestgannumber +
"date=" + actionTime + "\n|action" + latestgannumber + "link=" + link + "\n|action" + latestgannumber + "result=" + result + "\n|action" + latestgannumber + "oldid=\n")
newstatus = ""
if result == "promoted":
newstatus = r"currentstatus=FL"
else:
newstatus = r"currentstatus=FFLC"
updatedTalk = re.sub(
r"currentstatus ?= ?.*?(?=(\||}|\n| ))", newstatus, updatedTalk)
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Updating Article history", "text": su.unescape(
updatedTalk).replace(""", "\""), "bot": "true", "title": talkUrl.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
else:
newstatus = ""
if result == "promoted":
newstatus = r"currentstatus=FL"
else:
newstatus = r"currentstatus=FFLC"
actionTime = time.strftime(
"%H:%M, %d %B %Y", (time.gmtime(time.time())))
updatedTalk = "{{Article history\n|action1=FLC" + "\n|action1date=" + actionTime + "\n|action1link=" + \
link + "\n|action1result=" + result + \
"\n|action1oldid=" + "\n|" + newstatus + "}}" + talkPage
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Adding Article history", "text": su.unescape(
updatedTalk).replace(""", "\""), "bot": "true", "title": talkUrl.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
# ~~~FLC MAIN FUNCTION~~~#
# ~~~ACCEPTED FLC~~~#
flcpromoted = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content|user|ids&rvlimit=5000&format=xml&titles=Wikipedia:Featured_list_candidates/Featured_log/" + month + "_" + str(year)).read()
print flcpromoted
dom = minidom.parseString(flcpromoted)
flccontent = ""
approvededit = False
revisionnumber = 0
while approvededit == False:
print revisionnumber
flccontent = dom.getElementsByTagName("rev")[revisionnumber]
if flccontent.attributes['user'].value in flcapproved:
flccontentnew = flccontent.childNodes[0].data.encode("utf8")
approvinguser = flccontent.attributes['user'].value.encode("utf8")
revid = flccontent.attributes['revid'].value.encode("utf8")
oldid = flccontent.attributes['parentid'].value.encode("utf8")
approvededit = True
revisionnumber = revisionnumber + 1
print flccontentnew
for acceptedflc in re.findall(r"(?<=\{\{)Wikipedia:.*?(?=\}\})", flccontentnew):
print acceptedflc
flcProcess(acceptedflc, approvinguser, revid, oldid, "promoted")
# ~~~DECLINED FLC~~~#
flcnotpromoted = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content|user|ids&rvlimit=5000&format=xml&titles=Wikipedia:Featured_list_candidates/Failed_log/" + month + "_" + str(year)).read()
print flcnotpromoted
dom = minidom.parseString(flcnotpromoted)
flccontent = ""
approvededit = False
revisionnumber = 0
while approvededit == False:
print revisionnumber
flccontent = dom.getElementsByTagName("rev")[revisionnumber]
if flccontent.attributes['user'].value in flcapproved:
flccontentnew = flccontent.childNodes[0].data.encode("utf8")
approvinguser = flccontent.attributes['user'].value.encode("utf8")
revid = flccontent.attributes['revid'].value.encode("utf8")
oldid = flccontent.attributes['parentid'].value.encode("utf8")
approvededit = True
revisionnumber = revisionnumber + 1
print flccontentnew
for declinedflc in re.findall(r"(?<=\{\{)Wikipedia:.*?(?=\}\})", flccontentnew):
print declinedflc
flcProcess(declinedflc, approvinguser, revid, oldid, "not promoted")
# ~~~FAC~~~#
facapproved = ["Uchucha", "GrahamColm", "Ian Rose", "Raul654"]
#Users who are allowed to approve/disapprove FLC candidates
# ~~~FAC PROMOTIONS~~~#
# ~~~FAC FUNCTIONS~~~#
def facProcess(link, user, diff, oldlink, result):
faccontent = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=" + link.replace(" ", "_")).read()
if "<!--FAtop-->" not in faccontent and "<!--FLtop-->" not in faccontent:
# if True:
print link
print user
print diff
print oldlink
facre = re.compile(r"""(?<=preserve">).*?(?=</rev>)""", re.DOTALL)
faccontent = re.sub(
r">noinclude<{{la.+?}}>/noinclude<", "", re.findall(facre, faccontent)[0])
faccontent = "{{subst:User:Hahc21/FLTop|result=" + result + "|closer=" + user + \
"|time=~~~~~|type=FA|link=diff=" + diff + \
"&oldid=" + oldlink + "}}\n" + faccontent
faccontent = faccontent + "{{subst:User:Hahc21/FLBottom}}"
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Archiving FAC Nomination", "text": su.unescape(
faccontent).replace(""", "\""), "bot": "true", "title": link.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
facpage = re.findall(
r"(?<=Featured article candidates/).*?(?=/archive)", link.replace("_", " "))[0]
print facpage
if result == "promoted":
pagecontent = getContent(facpage).replace("{{good article}}", "").replace(
"{{goodarticle}}", "").replace("{{Good article}}", "").replace("{{Goodarticle}}", "")
pagecontent = "{{featured article}}\n" + pagecontent
if "#REDIRECT" in pagecontent:
print "redirecting"
facpage = re.findall(
r"(?<=#REDIRECT \[\[).*?(?=\]\])", pagecontent)[0]
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode(
{"format": "xml", "action": "edit", "token": x, "summary": "Adding Featured Article Star", "text": su.unescape(pagecontent).replace(""", "\""), "title": facpage})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
rmGA(facpage)
talkUrl = "Talk:" + facpage
talkPage = getContent(talkUrl)
if result == "promoted":
talkPage = re.sub(
r"\|class=.*?(?=(\||\n|}))", "|class=FA", talkPage)
talkPage = re.sub(r"{{featured article candidates.+?}}", "", talkPage)
if len(re.findall(r"{{[Aa]rticle ?[Hh]istory", talkPage)) > 0:
print talkPage
latestgan = re.compile(
"action[0-9]+ *?=.+?date *?=.+?[0-9][0-9][0-9][0-9]", re.DOTALL).findall(talkPage)[-1]
print latestgan
latestgannumber = str(
int(re.findall(r"(?<=action)[0-9]+?(?=date)", latestgan)[0]) + 1)
articleHistoryStart = re.findall(
re.compile(r"{{[Aa]rticle ?[Hh]istory.*?(?=\| ?currentstatus)", re.DOTALL), talkPage)[0]
actionTime = time.strftime(
"%H:%M, %d %B %Y", (time.gmtime(time.time())))
updatedTalk = talkPage.replace(articleHistoryStart, articleHistoryStart + "\n|action" + latestgannumber + "=FAC" + "\n|action" + latestgannumber +
"date=" + actionTime + "\n|action" + latestgannumber + "link=" + link + "\n|action" + latestgannumber + "result=" + result + "\n|action" + latestgannumber + "oldid=\n")
newstatus = ""
if len(re.findall("currentstatus ?= ?GA", talkPage)) >= 1:
newstatus = r"currentstatus=GA"
elif result == "promoted":
newstatus = r"currentstatus=FA"
else:
newstatus = r"currentstatus=FFAC"
updatedTalk = re.sub(
r"currentstatus ?= ?.*?(?=(\||}|\n| ))", newstatus, updatedTalk)
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Updating Article history", "text": su.unescape(
updatedTalk).replace(""", "\""), "bot": "true", "title": talkUrl.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
else:
newstatus = ""
if result == "promoted":
newstatus = r"currentstatus=FA"
else:
newstatus = r"currentstatus=FFAC"
actionTime = time.strftime(
"%H:%M, %d %B %Y", (time.gmtime(time.time())))
updatedTalk = "{{Article history\n|action1=FAC" + "\n|action1date=" + actionTime + "\n|action1link=" + \
link + "\n|action1result=" + result + "\n|action1oldid=" + \
"\n|currentstatus=" + newstatus + "}}" + talkPage
editInfo = url.Request("http://en.wikipedia.org/w/api.php", urllib.urlencode({"format": "xml", "action": "edit", "token": x, "summary": "Adding Article history", "text": su.unescape(
updatedTalk).replace(""", "\""), "bot": "true", "title": talkUrl.replace("&", "%26")})) # Define the edit query and execute it
response = ClientCookie.urlopen(
editInfo).read() # Get the response for debugging
print response
# ~~~FAC MAIN FUNCTION~~~#
# ~~~ACCEPTED FAC~~~#
facpromoted = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content|user|ids&rvlimit=5000&format=xml&titles=Wikipedia:Featured_article_candidates/Featured_log/" + month + "_" + str(year)).read()
print facpromoted
dom = minidom.parseString(facpromoted)
faccontent = ""
approvededit = False
revisionnumber = 0
while approvededit == False:
print revisionnumber
faccontent = dom.getElementsByTagName("rev")[revisionnumber]
if faccontent.attributes['user'].value in facapproved:
faccontentnew = faccontent.childNodes[0].data.encode("utf8")
approvinguser = faccontent.attributes['user'].value.encode("utf8")
revid = faccontent.attributes['revid'].value.encode("utf8")
oldid = faccontent.attributes['parentid'].value.encode("utf8")
approvededit = True
revisionnumber = revisionnumber + 1
print faccontentnew
for acceptedfac in re.findall(r"(?<=\{\{)Wikipedia:.*?(?=\}\})", faccontentnew):
print acceptedfac
facProcess(acceptedfac, approvinguser, revid, oldid, "promoted")
# ~~~DECLINED FAC~~~#
facnotpromoted = ClientCookie.urlopen(
"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content|user|ids&rvlimit=5000&format=xml&titles=Wikipedia:Featured_article_candidates/Archived_nominations/" + month + "_" + str(year)).read()
print facnotpromoted
dom = minidom.parseString(facnotpromoted)
faccontent = ""
approvededit = False
revisionnumber = 0
while approvededit == False:
print revisionnumber
faccontent = dom.getElementsByTagName("rev")[revisionnumber]
if faccontent.attributes['user'].value in facapproved:
faccontentnew = faccontent.childNodes[0].data.encode("utf8")
approvinguser = faccontent.attributes['user'].value.encode("utf8")
revid = faccontent.attributes['revid'].value.encode("utf8")
oldid = faccontent.attributes['parentid'].value.encode("utf8")
approvededit = True
revisionnumber = revisionnumber + 1
print faccontentnew
for declinedfac in re.findall(r"(?<=\{\{)Wikipedia:.*?(?=\}\})", faccontentnew):
print declinedfac
facProcess(declinedfac, approvinguser, revid, oldid, "not promoted")