summaryrefslogtreecommitdiff
path: root/wikibot/inthemedia.py
blob: d5a14f91d235b1bdfbb21a08dcdb801bf4985e4f (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
41
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('<!-- !!! Do not edit this page. It is updated automatically by a bot. !!! -->')
	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()