Instantly share code, notes, and snippets.

Embed
What would you like to do?
# coding: utf8
#
# Copyright (c) 2016 Sergey Leschina (putnik) <mail@putnik.tech>
#
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
import pywikibot
from pywikibot import pagegenerators
site = pywikibot.Site('ru', 'wikipedia')
repo = site.data_repository()
def process_bagde(element_id, ru_name, en_name):
summary = 'Add ' + en_name + ' badge for ruwiki'
category = pywikibot.Category(site, 'Категория:Википедия:' + ru_name + ' без отметки статуса в Викиданных')
generator = pagegenerators.CategorizedPageGenerator(category)
for page in generator:
title = page.title()
print('[%s] %s' % (en_name, title))
sitelink = {'site': 'ruwiki', 'title': title, 'badges': [ element_id ]}
item = pywikibot.ItemPage.fromPage(page, True)
if item.exists():
item.setSitelink(sitelink=sitelink, summary=summary)
else:
item = pywikibot.ItemPage(repo)
data = {
'sitelinks': {
page.site.dbName(): sitelink
},
'labels': {
page.site.lang: {
'language': page.site.lang,
'value': title
}
}
}
item.editEntity(data, summary=summary)
process_bagde('Q17559452', 'Добротные статьи', 'QA')
process_bagde('Q17580674', 'Избранные порталы', 'FP')
process_bagde('Q17506997', 'Избранные списки', 'FL')
process_bagde('Q17437796', 'Избранные статьи', 'FA')
process_bagde('Q17437798', 'Хорошие статьи', 'GA')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment