summaryrefslogtreecommitdiff
path: root/wikibot/currentwarriorproject.py
blob: 3e4cb8794075ae348c1db014ca59bdb1883f5cc4 (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
42
43
44
45
46
47
import pywikibot
import requests

warriorProjects = requests.get('https://warriorhq.archiveteam.org/projects.json').json()

# Retrieve current project
def get_project(projectName):
	for p in warriorProjects['projects']:
		if p['name'] == projectName:
			return p
	return None

def projectToWikitext(project, weight=None):
	if weight is not None:
		weightSection = '|%s' % (weight)
	else:
		weightSection = ''

	return '{{CurrentWarrior|' + project['name'] + '|' + project['title'] + weightSection + '}}' + '<!-- Project Description:' + project['description'] + ' -->'

# Update the wiki page if necessary
def maybe_edit_wiki(pageText, reason="Obtained data from WarriorHQ file"):
	site = pywikibot.Site('en', 'ArchiveTeam')
	page = pywikibot.Page(site, 'Main_Page/Current_Warrior_Project')
	if page.text != pageText:
		site.login() # Only log in when necessary
		page.text = pageText
		page.save(reason)


defaultProjects = []
defaultProject = [{'project': get_project(warriorProjects['auto_project']), 'weight': None}]

for autoProject in warriorProjects['auto_projects_config']:
    defaultProjects += [{'project': get_project(autoProject['project']), 'weight': autoProject['weight']}]

defaultProjectsWikitext = "\n".join([projectToWikitext(entry['project'], entry['weight']) for entry in defaultProjects+defaultProject])

wikiUpdateMessage = 'Default projects are now ' + ', '.join([
    '%(name)s%(weight)s' % {
        'name': entry['project']['name'],
        'weight': ' (weight %s)' % entry['weight'] if entry['weight'] is not None else ''
    }
    for entry in defaultProjects
])

maybe_edit_wiki(defaultProjectsWikitext, reason=wikiUpdateMessage)