Repository navigation
Expand file tree
/
Copy pathformatting.py
More file actions
168 lines (138 loc) · 6.19 KB
/
Copy pathformatting.py
File metadata and controls
168 lines (138 loc) · 6.19 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
import pywikibot
import time
from tqdm import tqdm
import concurrent.futures
import queue
# Configure your site (e.g., 'en', 'wikipedia')
site = pywikibot.Site()
site.login()
def process_page(page):
text = page.text
lines = text.split('\n')
made_changes = False
i = 0
in_codebox_section = False
while i < len(lines):
line = lines[i]
# Detect start of Codebox section
if line.startswith('{{Codebox'):
in_codebox_section = True
# Detect end of Codebox section
if in_codebox_section and line.startswith('==See also=='):
in_codebox_section = False
# If in Codebox section, skip processing
if in_codebox_section:
i += 1
continue
# Remove trailing whitespaces from the end of each line
original_line = lines[i]
cleaned_line = original_line.rstrip()
lines[i] = cleaned_line
# Check if this operation actually changed the line
if cleaned_line != original_line:
made_changes = True
# Check for Infobox start
if line.startswith('{{Infobox'):
j = i + 1
while j < len(lines) and not lines[j].startswith('}}'):
j += 1
if j < len(lines) and lines[j].startswith('}}'):
# Remove empty lines immediately after '}}' until a non-empty line is found
k = j + 1
while k < len(lines) and lines[k].strip() == '':
lines.pop(k) # Do not increment k, since pop shifts subsequent items left
made_changes = True
i = j + 1 # Move index past the Infobox section
continue
# Check for second-level section header (== Header ==)
if line.startswith('==') and not line.startswith('==='):
# Ensure there is a blank line before the header if there isn't one already
if i > 0 and lines[i - 1].strip():
lines.insert(i, '')
made_changes = True
i += 1 # Adjust index for inserted line
# Ensure the line following the header is not empty
if i + 1 < len(lines):
j = i + 1
while j < len(lines) and lines[j].strip() == '':
lines.pop(j) # Remove the empty line
made_changes = True
i = j - 1 # Adjust index to point to the header line
# Check for third-level section header (=== Subheader ===)
elif line.startswith('===') and not line.startswith('===='):
if i > 0:
prev_line = lines[i - 1].strip()
# If the line above starts with ==, do nothing
if prev_line.startswith('==') and not prev_line.startswith('==='):
i += 1
continue
# If the line above is empty, check the line above it
elif prev_line == '':
if i > 1 and not lines[i - 2].strip():
# Remove one of the empty lines if the one above the empty line is also empty
lines.pop(i - 1)
made_changes = True
i -= 1 # Adjust index for removed line
# If the line above has text but does not start with ==, insert an empty line
elif not prev_line.startswith('=='):
lines.insert(i, '')
made_changes = True
i += 1 # Adjust index for inserted line
# Check for Navbox
if line.startswith('{{Navbox'):
# Ensure there is a blank line before the Navbox if there isn't one already
if i > 0 and lines[i - 1].strip():
lines.insert(i, '')
made_changes = True
i += 1 # Adjust index for inserted line
# Ensure there is a blank line after the Navbox if there isn't one already
if i + 1 < len(lines) and lines[i + 1].strip():
lines.insert(i + 1, '')
made_changes = True
i += 1 # Normal increment of i
# Remove double empty lines
cleaned_lines = []
previous_line_empty = False
for line in lines:
if line.strip() == '':
if not previous_line_empty:
cleaned_lines.append(line)
previous_line_empty = True
else:
made_changes = True
else:
cleaned_lines.append(line)
previous_line_empty = False
if made_changes:
page.text = '\n'.join(cleaned_lines)
return True
return False
def check_page(title, change_queue):
page = pywikibot.Page(site, title)
original_text = page.text
if process_page(page) and page.text != original_text:
change_queue.put(title)
def main():
with open('wiki_directory.txt', 'r', encoding='utf-8') as file:
titles = file.read().splitlines()
# Thread-safe queue for pages that need changes
change_queue = queue.Queue()
# Multithreaded check with tqdm progress bar
with concurrent.futures.ThreadPoolExecutor(max_workers=75) as executor:
futures = {executor.submit(check_page, title, change_queue): title for title in titles if title}
for future in tqdm(concurrent.futures.as_completed(futures), total=len(futures), desc="Checking pages"):
future.result() # Ensure any exceptions are raised
# Convert queue to a list and sort alphabetically
pages_to_process = sorted(list(change_queue.queue))
# Single-threaded processing with rate limiting
for title in tqdm(pages_to_process, desc="Processing pages"):
page = pywikibot.Page(site, title)
if process_page(page):
try:
# Save the page with a summary
page.save(summary="Automated Formatting", tags="bot")
time.sleep(7) # Adhere to rate limiting
except pywikibot.exceptions.Error as e:
print(f"Error saving page '{title}': {e}")
if __name__ == "__main__":
main()