-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullAndPushBot.py
More file actions
executable file
·277 lines (274 loc) · 10.3 KB
/
pullAndPushBot.py
File metadata and controls
executable file
·277 lines (274 loc) · 10.3 KB
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
import pywikibot
import pprint
import sys
import os.path
import time
import MySQLdb
from pywikibot import pagegenerators
import operator
#import HTMLParser
from xml.sax.saxutils import unescape
class pullAndPushRevisions:
def __init__(self, test, wikipedia):
self.siteTest = test
self.siteWikipedia = wikipedia
def pullAndPush( self, cursor, currentRevision, increment, threshold, useCursorFile,
sleepInterval, oneRevision, db, dbCursor ):
#count = currentRevision
while 1:
count = currentRevision
endCount = currentRevision + increment * 50 # Get 50 revisions
if oneRevision == True:
endCount = currentRevision + increment
revids = ""
firstOne = True
while count < endCount:
if firstOne == True:
firstOne = False
else:
revids = revids + "|"
revids = revids + str(count)
count = count + increment
#pprint.pprint(revids)
#print(endCount)
#continue
#sys.exit()
pullParameters = {
'action': 'query',
'prop': 'revisions',
'rvslots': 'main',
'rvprop': 'ids|flags|timestamp|user|userid|comment|content|size|tags',
#'revids': '123456|123457'
'revids': revids
}
#pprint.pprint(pullParameters)
pullGen = pywikibot.data.api.Request(
self.siteWikipedia, parameters=pullParameters )
pullData = pullGen.submit()
unsortedRevisions=[]
revisions = []
#pprint.pprint ( pullData )
try:
pullData['query']['pages']
except KeyError:
#if len ( pullData['query']['pages'] ) ==0:
if currentRevision < threshold:
currentRevision = endCount
print ( "Empty; continuing with " + str(currentRevision)
+ " after sleeping " + str(sleepInterval) + " seconds" )
time.sleep( sleepInterval )
continue
for pageId,page in pullData['query']['pages'].items():
title = page['title']
ns = page['ns']
pageid = page['pageid']
for thisRevision in page['revisions']:
thisRevision['title'] = title
thisRevision['ns'] = ns
thisRevision['pageid'] = pageid
unsortedRevisions.append( thisRevision )
revisions = sorted(unsortedRevisions, key=lambda revision: revision['revid'] )
#for revision in revisions:
# print( revision['revid'] )
#sys.exit()
for revision in revisions:
pushParameters = {
'action': 'edit',
'title': 'Revision:' + str(revision['revid']),
'namespace': revision['ns'],
'remotetitle': unescape( revision['title'] ),
'page': revision['pageid'],
'token': self.siteTest.tokens['edit'],
'sdtags': '|'.join(revision['tags']),
'timestamp': revision['timestamp'],
'remoterev': revision['revid']
}
if 'minor' in revision:
pushParameters['minor'] = 'true'
if 'bot' in revision:
pushParameters['bot'] = 'true'
# Potentially hidden fields
pushParameters['deleted'] = 0
if 'userhidden' in revision:
pushParameters['deleted'] = pushParameters['deleted'] + 4
pushParameters['user'] = ''
pushParameters['userid'] = 0
else:
pushParameters['user'] = unescape( revision['user'] )
pushParameters['userid'] = revision['userid']
if 'commenthidden' in revision:
pushParameters['deleted'] = pushParameters['deleted'] + 2
pushParameters['summary'] = ''
else:
pushParameters['summary'] = unescape( revision['comment'] )
if 'texthidden' in revision['slots']['main']:
pushParameters['deleted'] = pushParameters['deleted'] + 1
pushParameters['text'] = ''
pushParameters['size'] = revision['size']
else:
pushParameters['text'] = unescape( revision['slots']['main']['*'] )
pushParameters['size'] = len( pushParameters['text'] )
pprint.pprint(pushParameters)
#pushGen = pywikibot.data.api.Request(
# self.siteTest, parameters=pushParameters )
#pushData = pushGen.submit()
#pprint.pprint(pushData)
#if pushData['edit']['result'] == 'Success':
#if currentRevision > threshold:
newRevTimestamp = revision['timestamp'][0:4]
newRevTimestamp = newRevTimestamp + revision['timestamp'][5:7]
newRevTimestamp = newRevTimestamp + revision['timestamp'][8:10]
newRevTimestamp = newRevTimestamp + revision['timestamp'][11:13]
newRevTimestamp = newRevTimestamp + revision['timestamp'][14:16]
newRevTimestamp = newRevTimestamp + revision['timestamp'][17:19]
sql = "INSERT INTO page(page_namespace, "
sql = sql + "page_title, page_restrictions, page_is_new,"
sql = sql + "page_random, page_touched, page_links_updated,"
sql = sql + "page_latest, page_len, page_content_model) "
sql = sql + "VALUES (1000, '" + str(revision['revid']) + "', '', 1,"
#sql = sql + str(float(random.randint(1, 999999999999)/1000000000000)) + ","
#sql = sql + "0." + random.randint(1,9) + random.randint(1,9) + random.randint(1,9)
#sql = sql + random.randint(1,9) + random.randint(1,9) + random.randint(1,9)
#sql = sql + random.randint(1,9) + random.randint(1,9) + random.randint(1,9)
#sql = sql + random.randint(1,9) + random.randint(1,9) + random.randint(1,9)
#sql = sql + random.randint(1,9) + random.randint(1,9) + ","
sql = sql + "RAND(), "
sql = sql + "20301231010203, 20301231010203, 0,"
sql = sql + str( pushParameters['size'] ) + ","
sql = sql + "'wikitext')"
print (sql)
try:
dbCursor.execute(sql)
db.commit()
except TypeError as e:
print (e)
db.rollback()
sys.exit()
pageRowId = dbCursor.lastrowid
print("last page row id:" + str(pageRowId))
sql = "INSERT INTO text(old_text,old_flags) VALUES ('"
sql = sql + MySQLdb.escape_string( pushParameters['text'].encode('utf-8') ) + "','')"
try:
dbCursor.execute(sql)
except TypeError as e:
print (e)
db.rollback()
sys.exit()
textRowId = dbCursor.lastrowid
#m = hashlib.md5()
#m.update( unescape( data['text'] ) )
#hash = hashlib.md5( unescape( data['text'] ) ).hexdigest()
print("last text row id:" + str(textRowId))
revision['title'] = revision['title'].replace(' ', '_')
revMinorEdit = 0
if 'minor' in revision:
revMinorEdit = 1
sql = "INSERT INTO revision(rev_page,rev_text_id,rev_comment,rev_user,rev_user_text,"
sql = sql + "rev_timestamp, rev_minor_edit, rev_deleted, rev_len," # rev_parent_id omitted
sql = sql + "rev_sha1, rev_content_model, rev_content_format, rev_remote_page,"
sql = sql + "rev_remote_namespace, rev_remote_title, rev_remote_rev, rev_remote_user) "
sql = sql + "VALUES ( " + str(pageRowId) + "," + str(textRowId) + ","
sql = sql + "'" + MySQLdb.escape_string( pushParameters['summary'] ) + "', "
sql = sql + "0, "
sql = sql + "'" + MySQLdb.escape_string( pushParameters['user'].encode('utf-8') ) + "', "
sql = sql + str(newRevTimestamp) + ", " + str(revMinorEdit) + ", "
sql = sql + str(pushParameters['deleted']) + ", "
sql = sql + str( pushParameters['size'] ) + ", '"
#sql = sql + MySQLdb.escape_string( m.digest() ) + "','wikitext',"
#sql = sql + MySQLdb.escape_string( hash ) + "','wikitext',"
sql = sql + "','wikitext',"
sql = sql + "'text/x-wiki'," + str(revision['pageid']) + ", " + str(revision['ns']) + ", '"
sql = sql + MySQLdb.escape_string( revision['title'].encode('utf-8') ) + "', "
sql = sql + str( revision['revid'] ) + ", " + str( pushParameters['userid'] )
sql = sql + ")"
print (sql)
try:
dbCursor.execute(sql)
except TypeError as e:
print (e)
db.rollback()
sys.exit()
revRowId = dbCursor.lastrowid
print("last rev row id:" + str(revRowId))
sql = "UPDATE page SET page_latest=" + str(revRowId) + " WHERE page_id=" + str(pageRowId)
try:
dbCursor.execute(sql)
db.commit()
except TypeError as e:
print (e)
db.rollback()
sys.exit()
#sys.exit()
currentRevision = revision['revid'] + increment
cursorFilename = 'cursor' + str(cursor) + '.txt'
if useCursorFile == True:
f = open( cursorFilename, 'w')
f.write( str( currentRevision ) + "\n" )
f.close()
#else:
# print ( 'Edit failure' )
# pprint.pprint(pushData['edit']['result'])
# sys.exit()
if oneRevision == True:
sys.exit()
siteTest = pywikibot.Site(code='en', fam='test2')
if not siteTest.logged_in():
siteTest.login()
siteWikipedia = pywikibot.Site(code='en', fam='wikipedia')
# Defaults
cursor = -1
currentRevision = 0
increment = 10
threshold = 800000000 # 900 million
sleepInterval = 3
useCursorFile = True
oneRevision = False
if len(sys.argv) < 2:
print ( 'Usage: python pullAndPushBot.py [--cursor] [--increment] [--currentrevision] [--threshold] [--onerevision]' )
print ( '--cursor is the offset' )
print ( '--currentrevision is the starting revision' )
print ( '--threshold sets a revid after which bad revisions are not skipped' )
print ( '--onerevision stops after one revision' )
print ( 'Example: python pullAndPushBot.py --cursor=1' )
print ( 'Example: python pullAndPushBot.py --currentrevision=8000001' )
print ( 'Example: python pullAndPushBot.py --currentrevision=8000001 --onerevision' )
sys.exit()
# Command line arguments
for arg in sys.argv:
if arg[0:9] == '--cursor=':
cursor = int( arg[9:] )
if arg[0:12] == '--increment=':
cursor = int( arg[12:] )
if arg[0:18] == '--currentrevision=':
currentRevision = int( arg[18:] )
if arg[0:12] == '--threshold=':
threshold = int( arg[12:] )
if arg[0:12] == '--sleepinterval=':
threshold = int( arg[12:] )
if arg[0:13] == '--onerevision':
oneRevision = True
useCursorFile = False
#print ( arg )
#print ( arg[0:18] )
#print ( arg[18:] )
if cursor == -1:
if currentRevision == 0:
print ( 'You must use either the --cursor or --currentrevision argument' )
sys.exit()
else:
cursor = currentRevision % increment
cursorFilename = 'cursor' + str(cursor) + '.txt'
# Command line current revision overrides cursor file
if currentRevision == 0 and os.path.isfile( cursorFilename ):
f = open( cursorFilename, 'r')
currentRevision = int( f.readline() )
myTestScript = pullAndPushRevisions( siteTest, siteWikipedia )
if ( currentRevision % increment != cursor ):
print ( 'Error: Modulus is ' + str( currentRevision % increment ) )
sys.exit()
print ( 'Resuming with ' + str( currentRevision ) )
db = MySQLdb.connect("localhost","admin","314159265","test2" )
# prepare a cursor object using cursor() method
dbCursor = db.cursor()
myTestScript.pullAndPush( cursor, currentRevision, increment, threshold, useCursorFile,
sleepInterval, oneRevision, db, dbCursor )