-
-
Save libkoi/0f328d6bb4158bee89734ba2201031da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from pre_wd import * | |
import mwclient | |
from tqdm import tqdm | |
MOEGIRL_P = 'P5737' | |
# NAMEDAS_P = "P1810" | |
MWPAGEDID_P = 'P9675' | |
LANG_P = 'P407' | |
LANG_ZH_CODE = 'Q7850' | |
SUMMARY_UPDATE = "Bot: Update metadata from moegirl API" | |
config_data = json.load(open("config.json")) | |
moe_token = config_data['moegirl_token'] | |
ua = config_data['ua'] | |
mwcsite = mwclient.Site('zh.moegirl.org.cn', path='/', clients_useragent=ua) | |
mwcsite.login(moe_token[0], moe_token[1]) | |
QUERY = '' | |
with open('moegirl_id_gen.rq', 'r') as query_file: | |
QUERY = query_file.read() | |
gen = pg.WikidataSPARQLPageGenerator(QUERY, site=pwbsite) | |
def moe_get_curid(title: str) -> str: | |
info = mwcsite.api('query', prop='info', titles=title) | |
curid = next(iter(info['query']['pages'])) | |
# print(curid) | |
return curid | |
def get_wikipage_quals(repo, title): | |
curid = moe_get_curid(title) | |
quals = [] | |
LANG_CODE = 'Q7850' | |
if title.startswith("en:"): | |
LANG_CODE = parse_iso_lang("en") | |
elif title.startswith("ja:"): | |
LANG_CODE = parse_iso_lang("ja") | |
curid_claim = pywikibot.Claim(repo, MWPAGEDID_P, is_qualifier=True) | |
curid_claim.setTarget(curid) | |
quals.append(curid_claim) | |
lang_claim = pywikibot.Claim(repo, LANG_P, is_qualifier=True) | |
LANG_ITEM = pywikibot.ItemPage(repo, LANG_CODE) | |
lang_claim.setTarget(LANG_ITEM) | |
quals.append(lang_claim) | |
return quals | |
count = 0 | |
for i in tqdm(gen): | |
item = get_item(i) | |
if not item: | |
continue | |
moe_cliams = get_valid_claims(item, MOEGIRL_P) | |
if not moe_cliams: | |
continue | |
for moe_claim in moe_cliams: | |
title = moe_claim.getTarget() | |
if title: | |
# print(title) | |
quals = get_wikipage_quals(repo, title) | |
if quals: | |
count += 1 | |
# print(count) | |
# print("===") | |
update_qualifiers(repo, moe_claim, quals, comment=SUMMARY_UPDATE) | |
print(f"Updated {count} entries") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment