User:Srishti0gupta/outreachy2

From Wikidata
Jump to navigation Jump to search

Link to Notebook[edit]

https://public.paws.wmcloud.org/User:Srishti0gupta/Outreachy_2.ipynb

Outreachy Task 2[edit]

Import modules

 import pywikibot
 from pywikibot import pagegenerators
 import numpy as np

Connect to enwiki

 enwiki = pywikibot.Site('en', 'wikipedia')

and then to wikidata

 enwiki_repo = enwiki.data_repository()

Editing the article with the text to be added

 def editarticle(page, strnew):
     text = page.get()
     text = text + strnew
     page.text = text
     try:
         page.save("Saving edited copy")
         return 1
     except:
         print("That didn't work!")
         return 0

Printing title, labels and claims with values of a QNumber

 def printwikidata(wd_item):
     qid = wd_item.title()
     print(qid)
      item_dict = wd_item.get()
     try:
         print('Name: ' + item_dict['labels']['en'])     
     except:
         print('No English label!')
     try:
         print('Page Decription: ' + item_dict['descriptions']['en'])        
     except:
         print('No English description!')
     return 0

Adding new PropertyId and Value to the QNumber Item Page

 def editwikidata(wd_item, propertyid, value):
     qid = wd_item.title()
     print(qid)
     item_dict = wd_item.get()
     claim_target = pywikibot.ItemPage(enwiki_repo, value)
     newclaim = pywikibot.Claim(enwiki_repo, propertyid)
     newclaim.setTarget(claim_target)
     print(newclaim)
     text = input("Save? ")
     if text == 'y':
         wd_item.addClaim(newclaim, summary=u'Adding test claim')
     return 0


Accessing User Page developed in Task 1

 page = pywikibot.Page(enwiki_repo, 'User:Srishti0gupta')

Asking user for text to be added

 strnew=input("What is to be added?")

Calling function to edit the article

 test = editarticle(page, strnew)

Printing the edited article

 print(page.text)

Asking user the QNumber of Data Item to view the claims

 testitem=input("Data item to be viewed?")
 wd_item = pywikibot.ItemPage(enwiki_repo, testitem)
 printwikidata(wd_item)

Editing New Property in the Sandbox Page

 testqid = 'Q4115189' # Wikidata sandbox
 testproperty = 'P31' # instance of
 testvalue = 'Q5'  # Sandbox
 wd_item = pywikibot.ItemPage(enwiki_repo, testqid)
 print(editwikidata(wd_item, testproperty, testvalue))