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()