User talk:WDBot/Bot Script nominal GDP

From Wikidata
Jump to navigation Jump to search
# -*- coding: utf-8  -*-
#CODE WRITEN FOR AND TESTED ON TEST.WIKIDATA.ORG
import pywikibot
import requests
import datetime

site = pywikibot.Site("test", "wikidata")
#print(site)
repo = site.data_repository()
#print(repo)

'''
1. load the country information (retrieved_now from query.wikidata.org and copy-pasted in the script)
2. iterate over each country
3. ckech if data on WorldBank is available - if not fo to next country
4. load the first value of wb data
4.1 check over all nominal gdp properties if the value is available
4.2 skip if value is available skip
4.3 write if value is not available
'''

#here an example for 2 countries (Bulgaria and Germany)
countries = [{"country":"Q170871","countryLabel":"Bulgaria","ISO":"BG"},{"country":"Q343","countryLabel":"Germany","ISO":"DE"}]

for j in countries:
    item = pywikibot.ItemPage(repo,j["country"])
    #print(item)
    #call the items data
    item.get()
    #print(item.labels) #print item labels
    #print(item.claims) #print the labels
    
    #---------------------------------------
    #get GDP-data from WorldBank-api
    url = 'http://api.worldbank.org/v2/countries/' + str(j["ISO"]) + '/indicators/NY.GDP.MKTP.CD?format=json'
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
    wb_request = requests.get(url, headers=headers)
    wb_data = wb_request.json()
    #check if there is data for this country
    try:
            print(wb_data[1])
    except:
            continue
            
    wb_data_entries = wb_data[1]#[0:3]
    retrieved_now = datetime.datetime.now()
    print("\n")
    print(wb_data_entries)

    '''
    for i in wb_data_entries:
        print(i)
    '''
    
    for i in wb_data_entries:
        #print(i)
        print(i["value"])
        #print(type(i["value"]))
        #check if i has a value. If there is no value (tyoe None) go to next i.
        if i["value"] is None:
            continue
        #----------------
        #check if there is already the claim
        if 'P78548' in item.claims: # nominal GDP claim for the country
            #print('number of claims = '+str(len(item.claims['P78548']))+'\n')
            #len_claims = len(item.claims['P78548'])
            #define a list to fill with the values available and then to search for the value from the WorldBank DB
            list_amount = []
            for k in item.claims['P78548']:
                item_value = k.getTarget()
                print(item_value.amount)
                list_amount.append(float(item_value.amount))
            #if value is already available - then break the loop and go to the next value
            if float(i["value"]) in list_amount:
                list_amount = []
                continue
            #get the claim/statement for the property
            claim = pywikibot.Claim(repo, 'P78548')
            entity_helper_string = "http://test.wikidata.org/entity/Q171019" # wd object for unity (US-Dollar, Euro...)
            claim.setTarget(pywikibot.WbQuantity(i["value"], entity_helper_string, site=site))
            item.addClaim(claim, bot=False, summary="Adding GDP-data from WorldBank-Database.")
            #add qualifyer to the claim
            qualifier = pywikibot.Claim(repo, 'P66')
            qualifier.setTarget(pywikibot.WbTime(year=i["date"]))
            claim.addQualifier(qualifier, summary='Adding point in time qualifier.')
            #add referrence
            ref_url = pywikibot.Claim(repo, 'P93')
            ref_url.setTarget("https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations="+str(j["ISO"]))
            retrieved = pywikibot.Claim(repo, 'P388')
            date = pywikibot.WbTime(year=retrieved_now.year, month=retrieved_now.month, day=retrieved_now.day)
            retrieved.setTarget(date)
            claim.addSources([ref_url, retrieved], summary='Adding sources.')
        else:
            #when there is no claim, then add all possible claims
            #get the claim/statement for the property
            claim = pywikibot.Claim(repo, 'P78548')
            entity_helper_string = "http://test.wikidata.org/entity/Q171019" # wd object for unity (US-Dollar, Euro...)
            claim.setTarget(pywikibot.WbQuantity(i["value"], entity_helper_string, site=site))
            item.addClaim(claim, bot=False, summary="Adding GDP-data from WorldBank-Database.")
            #add qualifyer to the claim
            qualifier = pywikibot.Claim(repo, 'P66')
            qualifier.setTarget(pywikibot.WbTime(year=i["date"]))
            claim.addQualifier(qualifier, summary='Adding point in time qualifier.')
            #add referrence
            ref_url = pywikibot.Claim(repo, 'P93')
            ref_url.setTarget("https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations="+str(j["ISO"]))
            retrieved = pywikibot.Claim(repo, 'P388')
            date = pywikibot.WbTime(year=retrieved_now.year, month=retrieved_now.month, day=retrieved_now.day)
            retrieved.setTarget(date)
            claim.addSources([ref_url, retrieved], summary='Adding sources.')