import pywikibot
from pywikibot import pagegenerators as pg

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

positionProperty = "P39" # Position held
position = "Q27169" # Member of the European Parliament
termProperty = "P2937" # Parliamentary Term
term = "Q17315703" # 2nd European Parliament
elected = 'P2715' # elected in
old_qualifier = 'Q1376076' # European Parliament election, 1989
new_qualifier = 'Q1376075' # European Parliament election, 1984

# MEPs with 'parliamentary term' as '1st european parliament' and 'elected in' as 'European Parliament Elections 1989'
QUERY = """
SELECT ?item WHERE {
  ?item p:%s ?mep.
  ?mep ps:%s wd:%s.
  ?mep pq:%s wd:%s.
  ?mep pq:%s wd:%s.
} 
""" % (positionProperty, positionProperty, position, termProperty, term, elected, old_qualifier)
generator = pg.WikidataSPARQLPageGenerator(QUERY, site=site)


for index, entity in enumerate(generator):
    print("assessing item " + str(index +1) + ": " + entity.id)
    # Get the item
    item = pywikibot.ItemPage(repo, entity.id)
    item_dict = item.get()
    # Get the position held claims
    claims = item_dict["claims"][positionProperty]
    # For each claim
    for claim in claims:
        qualifiersList = []
        # If position held is MEP
        if claim.getTarget().id == position:
            # For each qualifier
            if claim.qualifiers.get(termProperty):
                # if the claim has a qualifier for the right parliamentary term
                if claim.qualifiers.get(termProperty)[0].getTarget().id == term:
                    qualifier = claim.qualifiers.get(elected)
                    # if the elected qualifier is the required qualifier
                    if qualifier[0].getTarget().id == old_qualifier:
                        # remove the old qualifier
                        claim.removeQualifiers(qualifier)
                        # make a qualifier from the new qualifier id 
                        qualifier_claim = pywikibot.Claim(site, elected, is_qualifier=True)
                        qualifier_claim.setTarget(pywikibot.ItemPage(repo, new_qualifier))
                        # add the new qualifier
                        claim.addQualifier(qualifier_claim)
                        print("changed %s from %s to %s" % (elected, old_qualifier, new_qualifier))

print("Done.")
                        
assessing item 1: Q328504
changed P2715 from Q1376076 to Q1376075
assessing item 2: Q12310227
changed P2715 from Q1376076 to Q1376075
assessing item 3: Q4932182
changed P2715 from Q1376076 to Q1376075
assessing item 4: Q7493042
changed P2715 from Q1376076 to Q1376075
assessing item 5: Q1970552
changed P2715 from Q1376076 to Q1376075
assessing item 6: Q3167719
changed P2715 from Q1376076 to Q1376075
assessing item 7: Q3351443
changed P2715 from Q1376076 to Q1376075
assessing item 8: Q3293913
changed P2715 from Q1376076 to Q1376075
assessing item 9: Q3438812
changed P2715 from Q1376076 to Q1376075
assessing item 10: Q6835285
changed P2715 from Q1376076 to Q1376075
assessing item 11: Q3762932
changed P2715 from Q1376076 to Q1376075
assessing item 12: Q5112627
changed P2715 from Q1376076 to Q1376075
assessing item 13: Q7235539
changed P2715 from Q1376076 to Q1376075
assessing item 14: Q7151404
changed P2715 from Q1376076 to Q1376075
assessing item 15: Q1561145
changed P2715 from Q1376076 to Q1376075
assessing item 16: Q16760465
changed P2715 from Q1376076 to Q1376075
assessing item 17: Q835948
changed P2715 from Q1376076 to Q1376075
assessing item 18: Q3769950
changed P2715 from Q1376076 to Q1376075
assessing item 19: Q1656969
changed P2715 from Q1376076 to Q1376075
assessing item 20: Q17420845
changed P2715 from Q1376076 to Q1376075
assessing item 21: Q12735378
changed P2715 from Q1376076 to Q1376075
assessing item 22: Q3421265
changed P2715 from Q1376076 to Q1376075
assessing item 23: Q4864729
changed P2715 from Q1376076 to Q1376075
assessing item 24: Q3852796
changed P2715 from Q1376076 to Q1376075
assessing item 25: Q3609131
changed P2715 from Q1376076 to Q1376075
assessing item 26: Q182192
changed P2715 from Q1376076 to Q1376075
assessing item 27: Q4013310
changed P2715 from Q1376076 to Q1376075
assessing item 28: Q3726658
changed P2715 from Q1376076 to Q1376075
assessing item 29: Q451544
changed P2715 from Q1376076 to Q1376075
assessing item 30: Q516318
changed P2715 from Q1376076 to Q1376075
assessing item 31: Q328881
changed P2715 from Q1376076 to Q1376075
assessing item 32: Q2481787
changed P2715 from Q1376076 to Q1376075
assessing item 33: Q1497837
changed P2715 from Q1376076 to Q1376075
assessing item 34: Q3915832
changed P2715 from Q1376076 to Q1376075
assessing item 35: Q3658825
changed P2715 from Q1376076 to Q1376075
assessing item 36: Q2037085
changed P2715 from Q1376076 to Q1376075
assessing item 37: Q274735
changed P2715 from Q1376076 to Q1376075
assessing item 38: Q664465
changed P2715 from Q1376076 to Q1376075
assessing item 39: Q3169770
changed P2715 from Q1376076 to Q1376075
assessing item 40: Q780885
changed P2715 from Q1376076 to Q1376075
assessing item 41: Q12309860
changed P2715 from Q1376076 to Q1376075
assessing item 42: Q2834022
changed P2715 from Q1376076 to Q1376075
assessing item 43: Q3767005
changed P2715 from Q1376076 to Q1376075
assessing item 44: Q16266609
changed P2715 from Q1376076 to Q1376075
assessing item 45: Q3618333
changed P2715 from Q1376076 to Q1376075
assessing item 46: Q568949
changed P2715 from Q1376076 to Q1376075
assessing item 47: Q3886985
changed P2715 from Q1376076 to Q1376075
assessing item 48: Q62420
changed P2715 from Q1376076 to Q1376075
Done.