summaryrefslogtreecommitdiff
path: root/wikibot/urlteam.py
blob: f10d561835d7463dd972299335e3bb32bc270b79 (about) (plain) (blame)
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
import collections
import pywikibot
import re

def main():
	site = pywikibot.Site('en', 'ArchiveTeam')

	page = pywikibot.Page(site, 'URLTeam/Dead')

	entries = collections.deque(page.text.split('\n'))

	# Identify blocks of URLs and sort them
	entries.append(None) # Dummy entry at the end to trigger a last sorting if necessary
	output = []
	currentBlock = []
	urlCount = 0
	while entries:
		line = entries.popleft()
		if not line or not line.startswith('* '):
			# Either a line that isn't a list item or the dummy entry at the end
			if currentBlock:
				currentBlock.sort(key = str.lower)
				output.extend(currentBlock)
				currentBlock = []
			if line is not None: # Ignore the dummy entry
				output.append(line)
		else:
			# It's a list item and not the dummy entry.
			currentBlock.append(line)

	outputStr = '\n'.join(output)

	# Update if necessary
	if page.text != outputStr:
		site.login() # Only log in when necessary
		page.text = outputStr
		page.save("Sorted url list")


main()