Iōng-chiá:A-lú-mih/botsamples
(Tùi Iōng-chiá:A-lú-mih/bot-sample choán--lâi)
add_sitelink
siu-kái# -*- coding: utf-8 -*-
import pywikibot
def add_sitelink(item, nan_title):
# Adds sitelink to wikidata if not added yet.
try:
if u'zh_min_nanwiki' not in item.sitelinks:
# add sitelink to wikidata
item.setSitelink(sitelink={'site': 'zh_min_nanwiki', 'title': nan_title},
summary=u'Added new link to [zh_min_nanwiki]')
pywikibot.output(u'Added new link to wikidata')
else:
pywikibot.output(u'Sitelink already exists, item not edited')
except pywikibot.data.api.APIError:
# Bot got an error with warning for editing Yolo County (Q109709).
pywikibot.output(u'data.api.APIError occured')
pass
1
siu-kái# -*- coding: utf-8 -*-
import pywikibot
#NOTE: chhōe zh-min-nan bûn-chiuⁿ "Bí-kok" ê wikidata item
site = pywikibot.Site() # chí-tēng wikipedia pán-pún
page_title = u'Bí-kok' # bûn-chiuⁿ title
page = pywikibot.Page(site, page_title)
wd_item = pywikibot.ItemPage.fromPage(page)
# chhōe wikidata téng Bí-kok (Q30) ê jîn-kháu (P1082) chu-liāu
if 'P1082' in wd_item.claims:
# P1082 chu-liāu ē-té ū chē chōa bô-kâng sî-kan ê jîn-kháu
p1082_list = wd_item.claims['P1082']
# ti̍t-chiap the̍h jîn-kháu sò͘-jī, khah kán-tan ê hong-sek
pop_amount = wd_item.claims['P1082'][0].getTarget().amount
print pop_amount
else:
print u'Pún item bô chi̍t hāng chu-liāu'
2
siu-kái# -*- coding: utf-8 -*-
import pywikibot
#NOTE: chhōe zh-min-nan bûn-chiuⁿ "Bí-kok" ê wikidata item
site = pywikibot.Site() # chí-tēng wikipedia pán-pún
page_title = u'Bí-kok' # bûn-chiuⁿ title
page = pywikibot.Page(site, page_title)
wd_item = pywikibot.ItemPage.fromPage(page)
# chhōe wikidata téng Bí-kok (Q30) ê jîn-kháu (P1082) chu-liāu
if 'P1082' in wd_item.claims:
# P1082 chu-liāu ē-té ū chē chōa bô-kâng sî-kan ê jîn-kháu
p1082_list = wd_item.claims['P1082']
# kā kui-ê P1082 chū-liāu kái chò json kè-sek, thang chhōe tio̍h khah siōng-sè ê chu-sìn
# chhōe siāng téng-bīn chi̍t hūn jîn-kháu chu-liāu
pop_json = wd_item.claims['P1082'][0].toJSON()
# jîn-kháu sò͘-jī
pop_amount = int(pop_json['mainsnak']['datavalue']['value']['amount'])
# jîn-kháu sî-kan
pop_date = pop_json['qualifiers']['P585'][0]['datavalue']['value']['time']
# kā sî-kan chu-liāu kán-hòa
pop_date = pop_date.split('+0000000')[1].split('T')[0]
print pop_amount, pop_date
else:
print u'Pún item bô chi̍t hāng chu-liāu'
3
siu-kái# -*- coding: utf-8 -*-
import pywikibot
from pywikibot import pagegenerators as pg
query_string = """
SELECT ?item WHERE {
?item wdt:P585 [] .
MINUS { ?item wdt:P31 wd:Q577 }
MINUS { ?item wdt:P31 wd:Q14795564 }
}
"""
# téng-bīn query ê ì-sù:
# chhōe ū P585 (point in time) chū-liāu ê wikidata item,
# pâi-tû kî-tiong P31 (instance of) sī Q577, Q14795564 ... --ê
wikidata_site = pywikibot.Site("wikidata", "wikidata")
generator = pg.WikidataSPARQLPageGenerator(query_string, site=wikidata_site)
# tùi chhâ-sûn kiat-kó loop chhut bí chi̍t hāng
for wd_item in generator:
wd_item.get()
# sòa--lo̍h-lāi g to̍h kap téng-bīn script ê wd_item sī kâng khoán ê mi̍h
# ...
# in-ūi ta̍k khoán chū-liāu kè-sek lóng bô siáⁿ kâng, khó-lêng ài chiàu lí beh
# ê chū-liāu ēng toJSON() piàn chò json liáu-āu tùi lāi-bīn chhōe beh ài ê pō͘-hūn.