User:Varsha ahirwar from India/Outreachy 2
Code of Task 2:[edit]
from email import message
import pywikibot
site = pywikibot.Site('en', 'wikipedia') repo = site.data_repository()
# method to print content
def get_task1_outreachy(task1_outreachy):
message = task1_outreachy.get() task1_outreachy.message = message try: print(message) except: print("This page is not available!")
def get_task1_outreachy_page(Qid):
return pywikibot.ItemPage(repo, Qid)
# method to add message 'Hello' at the end of an article
def edit_article(page, addmessage= "Hello"):
message = page.get() message = message + "\n" + addmessage page.message = message try: print(message) page.save("test edit is being saved") return 1 except: print("It is not working!") return 0
# method to print first and last name of author
def get_claims_name(page_info, Pid, error):
try: for claim in page_info['claims'][Pid]: instance_value = claim.getTarget() instance_page_info = instance_value.get() print('\t\t\t' + instance_page_info['labels']['en']) print('\t\tQ identifier: ' + instance_value.title() + '\n') except: print(error)
# method to print qualifier information of author
def get_qualifiers_author_statement(claim, Pid, title):
if Pid in claim.qualifiers: qualifier = claim.qualifiers[Pid] print('\t'+ title +': ' + qualifier[0].getTarget())
def print_article(wd_item):
"""Prints article information from the wikidata site.""" item_code = wd_item.title() page_info = wd_item.get()
#method for printing title and Q identifier of article. try: print('\nTitle of article: \n' + page_info['labels']['en']) print('\nQ identifier of article:\n \t' + item_code + '\n') except: print('Item Name: This item has no English label!' + '\n')
#method for Printing author information in article. try: for claim in page_info['claims']['P50']: print('Author Information:') name_value = claim.getTarget() name_page_info = name_value.get() title = name_value.title() print('\tName: ' + name_page_info['labels']['en']) print('\tQ identifier: ' + title + '\n') print('\tQualifiers:') get_qualifiers_author_statement(claim, 'P1932', 'Stated as') get_qualifiers_author_statement(claim, 'P1545', 'Series ordinal') #Print given name and family name of author print('\n\tName info:') name_of_author = pywikibot.ItemPage(repo, title) author_dict = name_of_author.get() print('\t\tGiven name:') get_claims_name(author_dict, 'P735', '\t\t*No Given name info*') print('\t\tFamily name:') get_claims_name(author_dict, 'P734', '\t\t*No family name info*') print('\n') except: pass try: for claim in page_info['claims']['P2093']: print('Author Name String:') name_string_value = claim.getTarget() print('\tName: ' + name_string_value) get_qualifiers_author_statement(claim, 'P1545', 'Series ordinal') get_qualifiers_author_statement(claim, 'P1932', 'Stated as') print('\n') except: pass
def print_all_articles(articles):
#Prints all articles in Outreachy task 1 which are passed in articles variable. for item in articles: article = get_task1_outreachy_page(item) print_article(article) task1_outreachy = pywikibot.Page(repo, 'User:Varsha ahirwar from India/Outreachy 1') articles = ['Q86839085','Q58889074','Q110028642','Q33360138','Q60101261' , 'Q38193292','Q74515767','Q59065222'] article = get_task1_outreachy_page('Q4115189') get_task1_outreachy(task1_outreachy) edit_article(task1_outreachy) print_article(article) print_all_articles(articles)
Output: Title of article: Wikidata Sandbox
Q identifier of article:
Q4115189
Title of article:
Plasma physics. Understanding particle acceleration in astrophysical plasmas
Q identifier of article:
Q86839085
Author Information:
Name: Ellen Gould Zweibel Q identifier: Q29052332
Qualifiers: Stated as: Ellen Zweibel Series ordinal: 2
Name info: Given name: Ellen Q identifier: Q2235472
Family name: *No family name info*
Author Information:
Name: Hantao Ji Q identifier: Q80741709
Qualifiers: Stated as: Hantao Ji Series ordinal: 1
Name info: Given name: *No Given name info* Family name: *No family name info*
Title of article: Plasma physics of liquids—A focused review
Q identifier of article:
Q58889074
Author Information:
Name: Annemie Bogaerts Q identifier: Q28364722
Qualifiers: Stated as: Annemie Bogaerts Series ordinal: 2
Name info: Given name: Annemie Q identifier: Q566390
Family name: Bogaerts Q identifier: Q27974402
Author Name String:
Name: Patrick Vanraes Series ordinal: 1
Title of article: Plasma physics of magnetic island coalescence during magnetic reconnection Q identifier of article:
Q110028642
Author Name String:
Name: Meng Zhou Series ordinal: 1
Author Name String:
Name: Ye Pang Series ordinal: 2
Author Name String:
Name: Xiaohua Deng Series ordinal: 3
Author Name String:
Name: Shiyong Huang Series ordinal: 4
Author Name String:
Name: Xiangsheng Lai Series ordinal: 5
Title of article: Plasma physics. Stochastic electron acceleration during spontaneous turbulent reconnection in a strong shock wave Q identifier of article:
Q33360138
Author Information:
Name: Masahiro Hoshino Q identifier: Q57717525 Qualifiers: Stated as: M Hoshino Series ordinal: 4 Name info: Given name: *No Given name info* Family name: *No family name info*
Author Name String:
Name: Y Matsumoto Series ordinal: 1
Author Name String:
Name: T Amano Series ordinal: 2
Author Name String:
Name: T N Kato Series ordinal: 3
Title of article: Plasma physics and radiation hydrodynamics in developing an extreme ultraviolet light source for lithography Q identifier of article:
Q60101261
Author Information:
Name: Noriaki Miyanaga Q identifier: Q57472159 Qualifiers: Stated as: Noriaki Miyanaga Series ordinal: 21 Name info: Given name: *No Given name info* Family name: *No family name info*
Author Information:
Name: Atsushi Sunahara Q identifier: Q43104083 Qualifiers: Stated as: Atsushi Sunahara Series ordinal: 2 Name info: Given name: Atsushi Q identifier: Q4817792
Family name: *No family name info*
Author Information:
Name: Shinsuke Fujioka Q identifier: Q58050005 Qualifiers: Stated as: Shinsuke Fujioka Series ordinal: 6 Name info: Given name: *No Given name info* Family name: *No family name info*
Author Name String:
Name: Katsunobu Nishihara Series ordinal: 1
Author Name String:
Name: Akira Sasaki Series ordinal: 3
Author Name String:
Name: Masanori Nunami Series ordinal: 4
Author Name String:
Name: Hajime Tanuma Series ordinal: 5
Author Name String:
Name: Yoshinori Shimada Series ordinal: 7
Author Name String:
Name: Kazumi Fujima Series ordinal: 8
Author Name String:
Name: Hiroyuki Furukawa Series ordinal: 9
Author Name String:
Name: Takako Kato Series ordinal: 10
Author Name String:
Name: Fumihiro Koike Series ordinal: 11
Author Name String:
Name: Richard More Series ordinal: 12
Author Name String:
Name: Masakatsu Murakami Series ordinal: 13
Author Name String:
Name: Takeshi Nishikawa Series ordinal: 14
Author Name String:
Name: Vasilii Zhakhovskii Series ordinal: 15
Author Name String:
Name: Kouhei Gamata Series ordinal: 16
Author Name String:
Name: Akira Takata Series ordinal: 17
Author Name String:
Name: Hirofumi Ueda Series ordinal: 18
Author Name String:
Name: Hiroaki Nishimura Series ordinal: 19
Author Name String:
Name: Yasukazu Izawa Series ordinal: 20
Author Name String:
Name: Kunoki Mima Series ordinal: 22
Title of article: Plasma physics of extreme astrophysical environments. Q identifier of article:
Q38193292
Author Information:
Name: Shane Rightley Q identifier: Q87402106 Qualifiers: Series ordinal: 2 Name info: Given name: Shane Q identifier: Q2800558
Family name: *No family name info*
Author Name String:
Name: Dmitri A Uzdensky Series ordinal: 1
Title of article: Plasma physics aspects of tunnel-ionized gases Q identifier of article:
Q74515767
Author Information:
Name: Warren B. Mori Q identifier: Q97630190 Qualifiers: Stated as: Mori WB Series ordinal: 3 Name info: Given name: Warren Q identifier: Q2256715
Family name: *No family name info*
Author Information:
Name: Wim Leemans Q identifier: Q92459955 Qualifiers: Stated as: Leemans WP Series ordinal: 1 Name info: Given name: Wim Q identifier: Q1459852
Pieter Q identifier: Q1237447
Author Name String:
Name: M Mitchell Waldrop Series ordinal: 1
PS C:\Users\91835\Desktop\git\pywikibot\scripts\userscripts>