import pywikibot
import time

project_site = pywikibot.Site('wikidata', 'wikidata')
repo = project_site.data_repository()

inputdata = { 'Q1123466' : '221', 'Q14850809' : '<1', 'Q15632898' : '587', 'Q161267' : '5508', 'Q161268' : '193', 'Q161276' : '6586', 'Q161285' : '167', 'Q161472' : '1734', 'Q161480' : '1', 'Q161601' : '2345', 'Q16258456' : '19396', 'Q172275' : '20', 'Q179088' : '<1', 'Q1869625' : '96', 'Q209389' : '1', 'Q21098854' : '223', 'Q2242152' : '<1', 'Q2249494' : '966', 'Q225045' : '2019', 'Q2354896' : '454', 'Q23978438' : '16', 'Q2468529' : '1337', 'Q2474643' : '24', 'Q2683759' : '3860', 'Q27236466' : '155', 'Q27254088' : '149', 'Q27254799' : '20', 'Q27260938' : '79', 'Q27264960' : '<1', 'Q27270694' : '210', 'Q27272092' : '981', 'Q27273701' : '2', 'Q27274552' : '312', 'Q27277290' : '23', 'Q27278812' : '54', 'Q27278838' : '633', 'Q27279946' : '492', 'Q27282088' : '447', 'Q279055' : '26087', 'Q2806557' : '1', 'Q30715333' : '2', 'Q3074512' : '4', 'Q3291714' : '8780', 'Q32921' : '50', 'Q3375325' : '10213', 'Q385448' : '938', 'Q410612' : '13856', 'Q412460' : '<1', 'Q412659' : '9878', 'Q413018' : '7154', 'Q413079' : '10592', 'Q413677' : '7977', 'Q413835' : '17885', 'Q413918' : '595', 'Q416740' : '705', 'Q417035' : '12340', 'Q417982' : '<1', 'Q418163' : '179', 'Q419065' : '3691', 'Q419383' : '2143', 'Q419394' : '262', 'Q419702' : '8516', 'Q419723' : '2070', 'Q420380' : '9615', 'Q421426' : '8456', 'Q421736' : '1', 'Q421738' : '141', 'Q421742' : '817', 'Q421748' : '11', 'Q421758' : '3', 'Q422709' : '15', 'Q422994' : '2106', 'Q423000' : '5352', 'Q423021' : '11547', 'Q423029' : '1549', 'Q423046' : '15451', 'Q423055' : '7349', 'Q423633' : '4732', 'Q4545640' : '8998', 'Q5462894' : '20', 'Q5945644' : '8681', 'Q60459040' : '17', 'Q619868' : '<1', 'Q63441378' : '397', 'Q7164959' : '1032', 'Q7168152' : '9484', 'Q752098' : '635', 'Q905027' : '280' }
editSummary = 'fix mistake in previous import; [[:de:Special:Diff/190734901/190756709]]'

for item in inputdata:
    Qitem = pywikibot.ItemPage(repo, item)
    Qitem.get()
    
    if not Qitem.claims or 'P2565' not in Qitem.claims:
        print('{}: claim not found'.format(item))
        continue
        
    for claim in Qitem.claims['P2565']:
        claimJSON = claim.toJSON()
        for source in claimJSON['references']:
            if 'P248' in source['snaks']:
                for P248 in source['snaks']['P248']:
                    if P248['datavalue']['value']['numeric-id'] == 65923906:
                        oldTarget = claim.getTarget()
                        newValueStr = inputdata[item]
                        if newValueStr[0] == '<':
                            newValue = int(newValueStr[1:])
                            qual = True
                        else:
                            newValue = int(newValueStr)
                            qual = False
                        hasQual = False
                        if 'qualifiers' in claimJSON and 'P1480' in claimJSON['qualifiers']:
                            for qualifier in claimJSON['qualifiers']['P1480']:
                                if qualifier['datavalue']['value']['numeric-id'] == 52834024:
                                     hasQual = True
                        if hasQual==True:
                            oldValueStr = '<{}'.format(oldTarget.amount)
                        else:
                            oldValueStr = '{}'.format(oldTarget.amount)
                        print('{}: to be changed: {} (means {} and qual={}) to {} (means {} and qual={})'.format(
                            item,
                            oldValueStr,
                            oldTarget.amount,
                            hasQual,
                            inputdata[item],
                            newValue,
                            qual
                        ))
                        if oldTarget.amount != newValue:
                            newTarget = oldTarget
                            newTarget.amount = newValue
                            claim.changeTarget(value=newTarget, summary=editSummary)
                        if hasQual==False and qual==True:
                            sc = pywikibot.Claim(repo, 'P1480')
                            sc.setTarget(value=pywikibot.ItemPage(repo, 'Q52834024'))
                            claim.addQualifier(qualifier=sc, summary=editSummary)
                        if hasQual==True and qual==False:
                            for q in claim.qualifiers:
                                if q=='P1480':
                                    claim.removeQualifiers(claim.qualifiers[q], summary=editSummary)
        if True:
            break
Q1123466: to be changed: 71 (means 71 and qual=False) to 221 (means 221 and qual=False)
Q14850809: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q15632898: to be changed: 190 (means 190 and qual=False) to 587 (means 587 and qual=False)
Q161267: to be changed: 3693 (means 3693 and qual=False) to 5508 (means 5508 and qual=False)
Q161268: to be changed: 63 (means 63 and qual=False) to 193 (means 193 and qual=False)
Q161276: to be changed: 5880 (means 5880 and qual=False) to 6586 (means 6586 and qual=False)
Q161285: to be changed: 54 (means 54 and qual=False) to 167 (means 167 and qual=False)
Q161472: to be changed: 676 (means 676 and qual=False) to 1734 (means 1734 and qual=False)
Q161480: to be changed: <1 (means 1 and qual=True) to 1 (means 1 and qual=False)
Q161601: to be changed: 858 (means 858 and qual=False) to 2345 (means 2345 and qual=False)
Q16258456: to be changed: 24271 (means 24271 and qual=False) to 19396 (means 19396 and qual=False)
Q172275: to be changed: 6 (means 6 and qual=False) to 20 (means 20 and qual=False)
Q179088: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q1869625: to be changed: 31 (means 31 and qual=False) to 96 (means 96 and qual=False)
Q209389: to be changed: <1 (means 1 and qual=True) to 1 (means 1 and qual=False)
Q21098854: to be changed: 72 (means 72 and qual=False) to 223 (means 223 and qual=False)
Q2242152: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q2249494: to be changed: 317 (means 317 and qual=False) to 966 (means 966 and qual=False)
Q225045: to be changed: 915 (means 915 and qual=False) to 2019 (means 2019 and qual=False)
Q2354896: to be changed: 147 (means 147 and qual=False) to 454 (means 454 and qual=False)
Q23978438: to be changed: 5 (means 5 and qual=False) to 16 (means 16 and qual=False)
Q2468529: to be changed: 441 (means 441 and qual=False) to 1337 (means 1337 and qual=False)
Q2474643: to be changed: 8 (means 8 and qual=False) to 24 (means 24 and qual=False)
Q2683759: to be changed: 2294 (means 2294 and qual=False) to 3860 (means 3860 and qual=False)
Q27236466: to be changed: 50 (means 50 and qual=False) to 155 (means 155 and qual=False)
Q27254088: to be changed: 48 (means 48 and qual=False) to 149 (means 149 and qual=False)
Q27254799: to be changed: 6 (means 6 and qual=False) to 20 (means 20 and qual=False)
Q27260938: to be changed: 25 (means 25 and qual=False) to 79 (means 79 and qual=False)
Q27264960: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q27270694: to be changed: 68 (means 68 and qual=False) to 210 (means 210 and qual=False)
Q27272092: to be changed: 318 (means 318 and qual=False) to 981 (means 981 and qual=False)
Q27273701: to be changed: 1 (means 1 and qual=False) to 2 (means 2 and qual=False)
Q27274552: to be changed: 101 (means 101 and qual=False) to 312 (means 312 and qual=False)
Q27277290: to be changed: 7 (means 7 and qual=False) to 23 (means 23 and qual=False)
Q27278812: to be changed: 17 (means 17 and qual=False) to 54 (means 54 and qual=False)
Q27278838: to be changed: 205 (means 205 and qual=False) to 633 (means 633 and qual=False)
Q27279946: to be changed: 159 (means 159 and qual=False) to 492 (means 492 and qual=False)
Q27282088: to be changed: 144 (means 144 and qual=False) to 447 (means 447 and qual=False)
Q279055: to be changed: 33631 (means 33631 and qual=False) to 26087 (means 26087 and qual=False)
Q2806557: to be changed: <1 (means 1 and qual=True) to 1 (means 1 and qual=False)
Q30715333: to be changed: 1 (means 1 and qual=False) to 2 (means 2 and qual=False)
Q3074512: to be changed: 1 (means 1 and qual=False) to 4 (means 4 and qual=False)
Q3291714: to be changed: 11316 (means 11316 and qual=False) to 8780 (means 8780 and qual=False)
Q32921: to be changed: 16 (means 16 and qual=False) to 50 (means 50 and qual=False)
Q3375325: to be changed: 13137 (means 13137 and qual=False) to 10213 (means 10213 and qual=False)
Q385448: to be changed: 309 (means 309 and qual=False) to 938 (means 938 and qual=False)
Q410612: to be changed: 15622 (means 15622 and qual=False) to 13856 (means 13856 and qual=False)
Q412460: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q412659: to be changed: 12705 (means 12705 and qual=False) to 9878 (means 9878 and qual=False)
Q413018: to be changed: 5703 (means 5703 and qual=False) to 7154 (means 7154 and qual=False)
Q413677: to be changed: 10222 (means 10222 and qual=False) to 7977 (means 7977 and qual=False)
Q413835: to be changed: 21852 (means 21852 and qual=False) to 17885 (means 17885 and qual=False)
Q413918: to be changed: 192 (means 192 and qual=False) to 595 (means 595 and qual=False)
Q416740: to be changed: 228 (means 228 and qual=False) to 705 (means 705 and qual=False)
Q417035: to be changed: 16016 (means 16016 and qual=False) to 12340 (means 12340 and qual=False)
Q417982: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q418163: to be changed: 57 (means 57 and qual=False) to 179 (means 179 and qual=False)
Q419065: to be changed: 1766 (means 1766 and qual=False) to 3691 (means 3691 and qual=False)
Q419383: to be changed: 713 (means 713 and qual=False) to 2143 (means 2143 and qual=False)
Q419394: to be changed: 84 (means 84 and qual=False) to 262 (means 262 and qual=False)
Q419702: to be changed: 10749 (means 10749 and qual=False) to 8516 (means 8516 and qual=False)
Q419723: to be changed: 739 (means 739 and qual=False) to 2070 (means 2070 and qual=False)
Q420380: to be changed: 10579 (means 10579 and qual=False) to 9615 (means 9615 and qual=False)
Q421426: to be changed: 10894 (means 10894 and qual=False) to 8456 (means 8456 and qual=False)
Q421736: to be changed: <1 (means 1 and qual=True) to 1 (means 1 and qual=False)
Q421738: to be changed: 45 (means 45 and qual=False) to 141 (means 141 and qual=False)
Q421742: to be changed: 265 (means 265 and qual=False) to 817 (means 817 and qual=False)
Q421748: to be changed: 3 (means 3 and qual=False) to 11 (means 11 and qual=False)
Q421758: to be changed: 1 (means 1 and qual=False) to 3 (means 3 and qual=False)
Q422709: to be changed: 5 (means 5 and qual=False) to 15 (means 15 and qual=False)
Q422994: to be changed: 708 (means 708 and qual=False) to 2106 (means 2106 and qual=False)
Q423046: to be changed: 19144 (means 19144 and qual=False) to 15451 (means 15451 and qual=False)
Q423055: to be changed: 9563 (means 9563 and qual=False) to 7349 (means 7349 and qual=False)
Q423633: to be changed: 2671 (means 2671 and qual=False) to 4732 (means 4732 and qual=False)
Q4545640: to be changed: 10267 (means 10267 and qual=False) to 8998 (means 8998 and qual=False)
Q5462894: to be changed: 7 (means 7 and qual=False) to 20 (means 20 and qual=False)
Q5945644: to be changed: 11183 (means 11183 and qual=False) to 8681 (means 8681 and qual=False)
Q60459040: to be changed: 5 (means 5 and qual=False) to 17 (means 17 and qual=False)
Q619868: to be changed: <1 (means 1 and qual=True) to <1 (means 1 and qual=True)
Q63441378: to be changed: 128 (means 128 and qual=False) to 397 (means 397 and qual=False)
Q7164959: to be changed: 337 (means 337 and qual=False) to 1032 (means 1032 and qual=False)
Q7168152: to be changed: 12253 (means 12253 and qual=False) to 9484 (means 9484 and qual=False)
Q752098: to be changed: 206 (means 206 and qual=False) to 635 (means 635 and qual=False)
Q905027: to be changed: 90 (means 90 and qual=False) to 280 (means 280 and qual=False)