User:ANU Outreachy/Outreachy 4

From Wikidata
Jump to navigation Jump to search
import pywikibot
from pywikibot.data import api
import re
from pywikibot import pagegenerators
import datetime

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

def addDateClaim(item='', claim1='', date='', lang=''):
	now = datetime.datetime.now()
	check_ok = True
	if int(date.split('-')[0]) > now.year:
		check_ok = False
	try:
		if int(date.split('-')[0]) == now.year and int(date.split('-')[1]) > now.month:
			check_ok = False
	except:
		null = 0
	if check_ok:
		if enwiki_repo and item and claim1 and date and lang:
			claim = pywikibot.Claim(enwiki_repo, claim1)
			if len(date.split('-')) == 3:
				claim.setTarget(pywikibot.WbTime(year=int(date.split('-')[0]), month=int(date.split('-')[1]), day=int(date.split('-')[2])))
			elif len(date.split('-')) == 2:
				claim.setTarget(pywikibot.WbTime(year=int(date.split('-')[0]), month=int(date.split('-')[1])))
			elif len(date.split('-')) == 1:
				claim.setTarget(pywikibot.WbTime(year=int(date.split('-')[0])))
			print(claim)
			text = input("Save? ")
			if text == 'y' or text == 'Y':
				item.addClaim(claim), summary='Adding 1 dateclaim')
			else:
				return 0



def addImage(wdproperty, imagename):
	claim = pywikibot.Claim(enwiki_repo, wdproperty)
	item1 = pywikibot.ItemPage(enwiki_repo, 'Q7598')
	claims = item1.get().get('claims')
	if wdproperty in claims:
		pywikibot.output('Item {} already contains image ({})'.format(item1.title(), wdproperty))
	else:
		newclaim = pywikibot.Claim(enwiki_repo, wdproperty)
		commonssite = pywikibot.Site('commons', 'commons')
		imagelink = pywikibot.Link(imagename, source=commonssite, default_namespace=6)
		image = pywikibot.FilePage(imagelink)
		if not image.exists():
			pywikibot.output("{} doesn't exist".format(image.title(as_link=True)))
			return 0
		else:
			if image.isRedirectPage():
				image = pywikibot.FilePage(image.getRedirectTarget())
			
		newclaim.setTarget(image)		
		print (newclaim)

		text = input("Save? ")
		if text == 'y' or text == 'Y':
			item1.addClaim(newclaim, summary=u'Adding image filename')
         

def addQID(wd_item, propertyid, value):
	qid = wd_item.title()   
	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' or text == 'Y':
        	wd_item.addClaim(newclaim, summary=u'Adding QID')
		
	
	return 0

def addNumericalID(wd_item, claim):
	
	claim.setTarget('19576468')
	print(claim)
	

	text = input("Save? ")
	if text == 'y' or text == 'Y':
		
        	wd_item.addClaim(claim, summary=u'Adding NumericalID')
		#return 1
	return 0	

def addReference(claim, importedfrom):
	

	text = input("Save Reference? ")
	if text == 'y' or text == 'Y': 
			claim.addSource(importedfrom, summary='Adding 1 reference: [[Property:P31]]: [[%s]]' % ('Q328'))        	
		
	return 0	


def addQualifier(item):
	qualifier = pywikibot.Claim(enwiki_repo, u'P407')
	target = pywikibot.ItemPage(enwiki_repo, "Q1860")
	qualifier.setTarget(target)
	for claim in item.claims['P856']: #Finds all statements (P856)
		if 'P407' not in claim.qualifiers: #If not already exist
					
			text = input("Save Qualifier? ")
			if text == 'y' or text == 'Y':
        			claim.addQualifier(qualifier, summary=u'Adding a qualifier.') #Adding qualifier to all statements (P856)
				
			else:
				return 0


#QID ->
propertyid = 'P31' 
value = 'Q341'  
wd_item = pywikibot.ItemPage(enwiki_repo, 'Q535461')

print (addQID(wd_item, propertyid, value))  #Adding a QID of free software to Wget instance of property

#Image filename ->
propertyid2 = 'P154'
imagename = 'File:Official_gnu.svg'

print(addImage(propertyid2, imagename))  #Adding image filename of logo to GNU project


#DateClaim ->

date = "1983-09-27"
wdproperty= 'P577'
item = pywikibot.ItemPage(enwiki_repo, 'Q7598')
addDateClaim(item, wdproperty, date, 'en')  #Adding publication date to GNU Project
  
#Add Numerical Id ->

wd_item = pywikibot.ItemPage(enwiki_repo, 'Q535461')
stringclaim = pywikibot.Claim(enwiki_repo, u'P3553')
addNumericalID(wd_item, stringclaim) #Addiung Zhihu ID for Wget

#Add Reference ->

item = pywikibot.ItemPage(enwiki_repo, 'Q48524')
claim = pywikibot.Claim(enwiki_repo, 'P31')
target =  pywikibot.ItemPage(enwiki_repo, 'Q341')
claim.setTarget(target)
item.addClaim(claim)
importedfrom = pywikibot.Claim(enwiki_repo, 'P31')
importedwp = pywikibot.ItemPage(enwiki_repo, 'Q328')
importedfrom.setTarget(importedwp)

addReference(claim, importedfrom) 	#Add reference to Chromium instance of free software

#Add Qualifier


item = pywikibot.ItemPage(enwiki_repo, "Q7598")
item.get() #Fetch all page data, and cache it.
addQualifier(item)		#Add qualifier to GNU official website property