User:Jiehui Ma/Outreachy 2
Code:
edit# Import modules
import pywikibot
# 0. Connect to Wikidata repository with my account
site = pywikibot.Site('wikidata:wikidata', user='Jiehui Ma')
repo = site.data_repository()
# 1. Add text to outreachy homepage
def add_text(text):
page = pywikibot.Page(site, 'User:Jiehui_Ma/Outreachy_1')
print(page.text)
page.save(summary='Test', appendtext='\n' + text)
return 1
# Try adding 'Hello'
add_text('Hello')
# 2. Print all information from an item page
def print_all_text(qid):
item = pywikibot.ItemPage(repo, qid)
print(item.text)
return 1
# Try printing sandbox
print_all_text('Q4115189')
# 3. Print author information on article item page:
def get_item_cache(qid):
item = pywikibot.ItemPage(repo, qid)
return item.get()
def get_property_label(pid):
property_page = pywikibot.PropertyPage(repo, pid)
property_page.get()
return property_page.labels['en']
def get_qualifiers(claim, pid):
qualifier_label = get_property_label(pid)
qualifier_value = claim.qualifiers.get(pid)[0].target
print(f'{qualifier_label}: {qualifier_value}')
return 1
def get_name_info(item_author, pid):
return item_author.claims[pid][0].getTarget().labels['en']
def print_all_authors(article_qid):
article_item = get_item_cache(article_qid)
print(article_item['labels']['en'])
# Fetch the author(P50) information
if 'P50' not in article_item['claims']:
print('No author stored as items.')
else:
for claim in article_item['claims']['P50']:
# Get author name info & qualifiers
p50_value = claim.getTarget()
p50_item_dict = p50_value.get()
print(f"{get_property_label('P50')}: {p50_item_dict['labels']['en']}")
get_qualifiers(claim, 'P1545')
get_qualifiers(claim, 'P1932')
# Automatically follow links to author's own item page
author_qid = p50_value.getID()
item_author = pywikibot.ItemPage(repo, author_qid)
if item_author.get():
print('Redirected to item page, fetching author name components...')
try:
print(f"{get_property_label('P735')}: {get_name_info(item_author, 'P735')}")
except:
print('Given name item page is not in Wikidata.')
try:
print(f"{get_property_label('P734')}: {get_name_info(item_author, 'P734')}")
except:
print('Family name item page is not in Wikidata.')
# Fetch the author name string(P2093) information
if 'P2093' not in article_item['claims']:
print('No author stored as strings.')
else:
for claim in article_item['claims']['P2093']:
# Get author name string & qualifiers
p2093_value = claim.getTarget()
print(f"{get_property_label('P2093')}: {p2093_value}")
get_qualifiers(claim, 'P1545')
print('-------------------')
return 1
Output (for detecting author information):
edit# Try printing the author info in several articles:
article_qid_list = ['Q60560235','Q69152190','Q104538340','Q59897278']
for article_qid in article_qid_list:
print_all_authors(article_qid)
# Outputs: Low delta-V near-Earth asteroids: A survey of suitable targets for space missions author: Simone Ieva series ordinal: 1 stated as: S. Ieva Redirected to item page, fetching author name components... given name: Simone Family name item page is not in Wikidata. author: A. Rossi series ordinal: 9 stated as: A. Rossi Redirected to item page, fetching author name components... given name: Alessandro family name: Rossi author: Elisabetta Dotto series ordinal: 2 stated as: E. Dotto Redirected to item page, fetching author name components... given name: Elisabetta family name: Dotto author name string: D. Perna series ordinal: 3 author name string: M. A. Barucci series ordinal: 4 author name string: F. Bernardi series ordinal: 5 author name string: S. Fornasier series ordinal: 6 author name string: F. De Luise series ordinal: 7 author name string: E. Perozzi series ordinal: 8 author name string: J. R. Brucato series ordinal: 10 ------------------- An investigation of the low-{Delta}V near-Earth asteroids (341843) 2008 EV5 and (52381) 1993 HA Two suitable targets for the ARM and MarcoPolo-M5 space missions No author stored as items. author name string: Perna D. series ordinal: 1 author name string: Popescu M. series ordinal: 2 author name string: Monteiro F. series ordinal: 3 author name string: Lantz C. series ordinal: 4 author name string: Lazzaro D. series ordinal: 5 author name string: Merlin F. series ordinal: 6 ------------------- Surface composition of near-Earth Asteroid (4953) 1990 MU: Possible fragment of (6) Hebe author: Vishnu Reddy series ordinal: 3 stated as: Vishnu Reddy Redirected to item page, fetching author name components... given name: Vishnu family name: Reddy author name string: Michael S. Kelley series ordinal: 1 author name string: Michael J. Gaffey series ordinal: 2 author name string: Juan A. Sanchez series ordinal: 4 ------------------- In-Flight Calibration of the Near Earth Asteroid Rendezvous Mission's Near Infrared Spectrometer I. Initial Calibrations author: Noam Izenberg series ordinal: 1 stated as: N Izenberg Redirected to item page, fetching author name components... given name: Noam Family name item page is not in Wikidata. No author stored as strings. -------------------