Canadian Elections

This is a little test workbook to add statements to candidates of Canadian federal elections.

import csv
import pywikibot as _

site = _.Site('wikidata', 'wikidata')
repo = site.data_repository()
site
# Constants
P_CANDIDACY = 'P3602'
P_VOTES = 'P1111'
P_DISTRICT = 'P768'
P_REPRESENTS = 'P4100'

P_INSTANCE = 'P31'
P_GENDER = 'P21'
P_CITIZENSHIP = 'P27'
P_OCCUPATION = 'P106'
P_MEMBER = 'P102'

P_REF_URL = 'P854'
P_PUBLISHER = 'P123'
P_RETRIEVED = 'P813'

U_REF = 'https://lop.parl.ca/sites/ParlInfo/default/en_CA/ElectionsRidings/Elections'

Q_ELECTION = 'Q41302'

Q_PERSON = 'Q5'
Q_CANADA = 'Q16'

Q_LOP = 'Q1125633'
# Reference
ref_url = _.Claim(repo, P_REF_URL)
ref_url.setTarget(U_REF)
        
publisher = _.Claim(repo, P_PUBLISHER)
publisher.setTarget(_.ItemPage(repo, Q_LOP))
        
retrieved = _.Claim(repo, P_RETRIEVED)
retrieved.setTarget(_.WbTime(year=2019, month=3, day=16))

Add election result to existing people

Format: District | Person | Party | Votes received

with open("election_40.tsv", "r") as fd:
    rd = csv.reader(fd, delimiter="\t", quotechar='"')
    for row in rd:
        if (row[0] == '#N/A' or row[1] == '#N/A' or row[2] == '#N/A'):
            continue

        item = _.ItemPage(repo, row[1])
        
        # Create claim
        claim = _.Claim(repo, P_CANDIDACY)
        claim.setTarget(_.ItemPage(repo, Q_ELECTION))
        
        # Number of votes
        votes = _.Claim(repo, P_VOTES)
        votes.setTarget(_.WbQuantity(row[3], site=repo))
        claim.addQualifier(votes)
        
        # District
        district = _.Claim(repo, P_DISTRICT)
        district.setTarget(_.ItemPage(repo, row[0]))
        claim.addQualifier(district)
        
        # Party
        if (row[2]):
            party = _.Claim(repo, P_REPRESENTS)
            party.setTarget(_.ItemPage(repo, row[2]))
            claim.addQualifier(party)
        
        claim.addSources([ref_url, publisher, retrieved])
        
        item.addClaim(claim)

        print(row)
        
print("Done!")

Create page for people

Format: Name | Gender | Occupation | Affiliation

ids = []
with open("election_40.tsv", "r") as fd:
    rd = csv.reader(fd, delimiter="\t", quotechar='"')
    for row in rd:
        if (row[0] == '#N/A' or row[1] == '#N/A' or row[2] == '#N/A' or row[3] == '#N/A'):
            continue

        item = _.ItemPage(repo)
        
        # Add name as label
        item.editLabels({'en': row[0]})
        
        # Instance of person
        claim = _.Claim(repo, P_INSTANCE)
        claim.setTarget(_.ItemPage(repo, Q_PERSON))
        claim.addSources([ref_url, publisher, retrieved])
        item.addClaim(claim)
        
        # Gender
        claim = _.Claim(repo, P_GENDER)
        claim.setTarget(_.ItemPage(repo, row[1]))
        claim.addSources([ref_url, publisher, retrieved])
        item.addClaim(claim)
        
        # Citizenship: Canada
        claim = _.Claim(repo, P_CITIZENSHIP)
        claim.setTarget(_.ItemPage(repo, Q_CANADA))
        claim.addSources([ref_url, publisher, retrieved])
        item.addClaim(claim)
        
        # Occupation
        if (row[2]):
            pr = row[2].split("/")
            for p in pr:
                claim = _.Claim(repo, P_OCCUPATION)
                claim.setTarget(_.ItemPage(repo, p))
                claim.addSources([ref_url, publisher, retrieved])
                item.addClaim(claim)
        
        # Party affiliation
        if (row[3]):
            claim = _.Claim(repo, P_MEMBER)
            claim.setTarget(_.ItemPage(repo, row[3]))
            claim.addSources([ref_url, publisher, retrieved])
            item.addClaim(claim)

        ids.append(item.getID())
        print(item.getID())
        
print(ids)

Change "represents" to "parliamentary group"

As that seems to be more specific. Causes >= 2 edits per page (2 for each statement, remove/add)

from pywikibot import pagegenerators as pg

generator = pg.WikidataSPARQLPageGenerator(
    "SELECT ?item WHERE { ?item p:P3602 ?statement. ?statement ps:P3602 ?election. ?election wdt:P17 wd:Q16. ?statement pq:P1268 ?represents. }",
    site=site)

for item in generator:
    item.get()

    for e in item.claims[P_CANDIDACY]:
        try:
            qual = e.qualifiers['P1268'][0]
        except:
            continue
        
        party = qual.target
        
        newQual = _.Claim(repo, P_REPRESENTS)
        newQual.setTarget(party)

        e.removeQualifiers([qual])
        e.addQualifier(newQual)
        
    print(item)