# -*- coding: utf-8  -*-

import pywikibot
import json
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()

politician = u"Q1895447"
fraktion = "Q27950685" #
landtag_period = u"Q27949660" # 15th Saarland
landtag_membership_id = u'Q18130478' #Saarland

def mark_as_part_of_current_landtag(politican_item_id, politician_fraktion_id, landtag_period, landtag_membership_id):
    item = pywikibot.ItemPage(repo, politican_item_id)
    claims = item.get()['claims']['P39']
    
    legislaturperiode = u'P2937'
    

    # Q17781726 is: Mitglied des Lantags Nordrhein-Westfahlen
    landtag_membership = next(claim for claim in claims if claim.target.id == landtag_membership_id)
    qualifier = pywikibot.Claim(repo, legislaturperiode)
    target = pywikibot.ItemPage(repo, landtag_period)
    qualifier.setTarget(target)    
    landtag_membership.addQualifier(qualifier)
    
    claim_fraktion = pywikibot.Claim(repo, u'P1416')
    target_fraktion = pywikibot.ItemPage(repo, politician_fraktion_id)
    claim_fraktion.setTarget(target_fraktion)
    item.addClaim(claim_fraktion)
    
    item.editEntity(summary='pyWikibot politican in Wahlperiode')
    
mark_as_part_of_current_landtag(politician, fraktion, landtag_period, landtag_membership_id)
Sleeping for 9.0 seconds, 2016-12-04 10:53:09
Sleeping for 9.6 seconds, 2016-12-04 10:53:18