import pywikibot firstXentries = 20 def main(): site = pywikibot.Site('en', 'ArchiveTeam') # Retrieve full "In The Media" page mediaPage = pywikibot.Page(site, 'In The Media') # Parse contents and extract first n entries entries = [] for line in mediaPage.text.split('\n'): if not line: continue if line[0] == ';': entries.append([line]) elif line[0] == ':': entries[-1].append(line) topEntries = entries[:firstXentries] # Construct contents of "Main Page/In The Media" contents = [] contents.append('') contents.append('') for entry in topEntries: contents.append(';*{}'.format(entry[0][1:])) contents.extend(entry[1:]) contents.append('') contents.append(';[[In The Media|More...]]') contentsStr = '\n'.join(contents) # Update if necessary mainPage = pywikibot.Page(site, 'Main Page/In The Media') if mainPage.text != contentsStr: site.login() # Only log in when necessary mainPage.text = contentsStr mainPage.save("Updated from " + mediaPage.title(as_link=True)) main()