-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd_add.py
208 lines (182 loc) · 9.31 KB
/
sd_add.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
# See sd_run.py for status and copyright release information
import datetime
import time
from sd_edit_allowed import ok_to_edit
from sd_functions import *
# Main function for 'edit' mode. Write the descriptions to mainspace, reading in from local staging_file
def shortdesc_add():
ecount = ecount_success = ecount_failure = skip_count = 0
esuccess_str = efailure_str = ''
# Check login
if username not in ('MichaelMaggs', 'ShortDescBot'):
print('STOPPING - unexpected username: ', username)
return
if username != 'ShortDescBot' and not assisted_mode:
print(f'STOPPING - cannot run bot in automatic mode with Username:{username}')
return
# Get the list of articles and the new short descriptions from local staging_file
try:
with open('staged.tsv') as sfile:
data = sfile.read()
lines = data.splitlines()
except:
print("STOPPING - can't open staging file")
return
# Work through lines of staging_file, one by one
for line in lines:
# Skip line if it's a table header
if '{|' in line or '|}' in line or line.strip() == '':
continue
# Set up for this page, from current line
values = line.split('\t')
title = values[1].strip()
description = values[2].strip() # This is the new description we want to use
page = pywikibot.Page(wikipedia, title)
# Get existing description and type: manual or embedded
existing_desc, existing_type = existing_shortdesc(page)
# Check for various things before allowing a page edit
try:
if not ok_to_edit(page, title, description, username, existing_desc, existing_type, override_manual,
override_embedded, existing_desc_regex, existing_desc_required_words,
existing_desc_excluded_words):
skip_count += 1
continue # Go on to next line, ie page to edit
except AssertionError:
return # Unexpected error in page title
# Get manual confirmation if in assisted mode
if assisted_mode:
key_input = confirm_edit(title, existing_desc, existing_type, description)
if key_input == 's':
break
if key_input != 'y':
continue
# OK to edit at this point
ecount += 1
# Add a new description where none currently exists
if existing_type is None:
edit_text = 'Adding [[Wikipedia:Short description|short description]]'
if username == 'ShortDescBot':
edit_text = '[[User:ShortDescBot|ShortDescBot]] adding [[Wikipedia:Short description|short ' \
'description]]'
page.text = '{{Short description|' + description + '}}\n' + page.text
print(str(ecount + 1) + ': ' + title + f' - WRITING NEW SD: ' + description)
try:
page.save(edit_text + ' "' + description + '"', minor=False)
except:
print(f'UNABLE TO EDIT {title}. Will retry in 1 minute')
time.sleep(60)
try:
page.save(edit_text + ' "' + description + '"', minor=False)
except:
print(f'STILL UNABLE TO EDIT {title}. Mark as failed')
# Build up efailure_str string ready to log to local file
efailure_str += title + '\t FAILED: ' + description + '\n'
ecount -= 1
ecount_failure += 1
else: # OK on second attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
else: # Succeeded on first attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
# Override an existing embedded description
if existing_type == 'embedded':
edit_text = 'Overriding [[Wikipedia:Short description|short description]] from infobox with'
if username == 'ShortDescBot':
edit_text = '[[User:ShortDescBot|ShortDescBot]] overriding [[Wikipedia:Short description|short ' \
'description]] from infobox with'
page.text = '{{Short description|' + description + '}}\n' + page.text
print(str(ecount + 1) + ': ' + title + f' - OVERRIDING EMBEDDED SD: ' + description)
try:
page.save(edit_text + ' "' + description + '"', minor=False)
except:
print(f'UNABLE TO EDIT {title}. Will retry in 1 minute')
time.sleep(60)
try:
page.save(edit_text + ' "' + description + '"', minor=False)
except:
print(f'STILL UNABLE TO EDIT {title}. Mark as failed')
# Build up efailure_str string ready to log to local file
efailure_str += title + '\t FAILED: ' + description + '\n'
ecount -= 1
ecount_failure += 1
else: # OK on second attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
else: # Succeeded on first attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
# Replace/edit a manual short description
if existing_type == 'manual':
# Construct a {{Short description}} template we know should exist, then remove it
sd_constructed = '{{Short description|' + existing_desc + '}}'
sd_constructed2 = '{{short description|' + existing_desc + '}}'
if sd_constructed not in page.text and sd_constructed2 not in page.text:
print(page.title() + ' - ERROR: Cannot locate SD template, though it should exist')
continue
page.text = page.text.replace(sd_constructed + '\n', '')
page.text = page.text.replace(sd_constructed2 + '\n', '')
edit_text = 'Changing [[Wikipedia:Short description|short description]] from'
if username == 'ShortDescBot':
edit_text = '[[User:ShortDescBot|ShortDescBot]] changing [[Wikipedia:Short description|short ' \
'description]] from'
page.text = '{{Short description|' + description + '}}\n' + page.text
print(str(ecount + 1) + ': ' + title + f' - CHANGING MANUAL SD: ' + description)
try:
page.save(edit_text + ' "' + existing_desc + '" to "' + description + '"', minor=False)
except:
print(f'UNABLE TO EDIT {title}. Will retry in 1 minute')
time.sleep(60)
try:
page.save(edit_text + ' "' + description + '"', minor=False)
except:
print(f'STILL UNABLE TO EDIT {title}. Mark as failed')
# Build up efailure_str string ready to log to local file
efailure_str += title + '\t FAILED: ' + description + '\n'
ecount -= 1
ecount_failure += 1
else: # OK on second attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
else: # Succeeded on first attempt
# Build up esuccess_str string ready to log to local file
esuccess_str += title + '\t' + description + '\n'
ecount_success += 1
time.sleep(wait_time)
# Now write to one-off edit logging files
print('\n')
now = datetime.datetime.now()
dt_extension = f'{now:%Y-%m-%d (%H %M).tsv}'
name_start = 'log_success '
if username == 'MichaelMaggs':
name_start = 'MNM non-bot log_success ' # Distinguish assisted (non-bot) edits with my username
try:
if esuccess_str:
log_success_file = name_start + f'({ecount_success}) {dt_extension}'
with open(log_success_file, 'w') as ls_file:
ls_file.write(esuccess_str)
print('Successes logged in ' + log_success_file)
if efailure_str:
log_fail_file = 'log_fail ' + f'({ecount_failure}) {dt_extension}'
with open(log_fail_file, 'w') as lf_file:
lf_file.write(efailure_str)
print('Failures logged in ' + log_fail_file)
except:
print(f'\nSTOPPING: Unable to create or write to logging files')
return
try:
etargets = ecount_failure + ecount_success
esucc_pc = round(100 * ecount_success / etargets, 2)
efail_pc = round(100 * ecount_failure / etargets, 2)
print(
f'EDIT TARGETS: {etargets} SUCCESS: {ecount_success} ({esucc_pc}%) FAILURE: {ecount_failure} ('
f'{efail_pc}%)')
print(f'SKIPPED: {skip_count} articles')
except:
print('\nNo edit target articles found.')
return