import pywikibot
import time
from pywikibot import pagegenerators

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

inputdata = []
dataset_query = """SELECT DISTINCT ?item WHERE {
  ?item p:P1087 [ pq:P585 ?date; wikibase:rank ?rank ]; wdt:P570 ?dod .
  FILTER(?rank != wikibase:DeprecatedRank) .
  FILTER(?date >= ?dod) .
}"""
for itemkey in pagegenerators.WikidataSPARQLPageGenerator(dataset_query, site=wikidata_site):
    inputdata.append(itemkey.title())

for i, item in enumerate(inputdata):
    Qitem = pywikibot.ItemPage(repo, item)
    Qitem.get()
    
    print()
    print('=== Item {} ({}/{}) ==='.format(item, i+1, len(inputdata)))
    if not Qitem.claims:
        print('* No claims found; skip item')
        continue
    
    if 'P570' not in Qitem.claims:
        print('* No date of death found; skip item')
        continue
        
    if len(Qitem.claims['P570'])>1:
        print('* More than one date of death found; skip item')
        continue
        
    dod = Qitem.claims['P570'][0].getTarget()
    if dod.precision < 9:
        print('* Date of death precision less than "year"; skip item')
        continue
    
    if 'P1087' not in Qitem.claims:
        print('* No Elo numbers found; skip item')
        continue
        
    for claim in Qitem.claims['P1087']:
        if not claim.qualifiers or 'P585' not in claim.qualifiers:
            print('* Found claim (value {}) without P585 qualifier; skip claim'.format(claim.getTarget().amount))
            continue
        
        if claim.qualifiers['P585'][0].getTarget().precision < 10:
            print('* Elo point in time precision less than "month"; skip claim')
            continue
        
        if claim.getRank() == 'deprecated':
            #print('* Claim is already deprecated; skip claim')
            continue
            
        if dod.year < claim.qualifiers['P585'][0].getTarget().year or (dod.precision>=10 and dod.year == claim.qualifiers['P585'][0].getTarget().year and dod.month < claim.qualifiers['P585'][0].getTarget().month):
            claim.changeRank('deprecated', summary='deprecate [[Property:P1087]] claim for point of time after subject’s date of death')
            print('* Set {} --> P1087 --> "{}" --> P585 --> "{:04d}-{:02d}" to "deprecated rank" (dod: {:04d}-{:02d}-{:02d}, precision {})'.format(item, claim.getTarget().amount, claim.qualifiers['P585'][0].getTarget().year, claim.qualifiers['P585'][0].getTarget().month, dod.year, dod.month, dod.day, dod.precision))

print()
print('All done, task finished')
=== Item Q12630999 (1/22) ===

=== Item Q27523580 (2/22) ===

=== Item Q4252801 (3/22) ===

=== Item Q4421791 (4/22) ===

=== Item Q11698838 (5/22) ===

=== Item Q24844895 (6/22) ===

=== Item Q4191878 (7/22) ===

=== Item Q27533972 (8/22) ===

=== Item Q4320456 (9/22) ===

=== Item Q18243395 (10/22) ===

=== Item Q4105332 (11/22) ===

=== Item Q2069351 (12/22) ===

=== Item Q15310325 (13/22) ===

=== Item Q358808 (14/22) ===

=== Item Q2650747 (15/22) ===

=== Item Q4212847 (16/22) ===

=== Item Q4496452 (17/22) ===

=== Item Q4259216 (18/22) ===

=== Item Q4346051 (19/22) ===

=== Item Q28058362 (20/22) ===
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2110" --> P585 --> "2009-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2010-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2011-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2010-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2011-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2010-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2011-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2010-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2011-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2013-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2014-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2013-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2014-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2110" --> P585 --> "2010-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2011-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2013-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2014-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2013-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2014-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2110" --> P585 --> "2010-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2108" --> P585 --> "2011-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2012-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2120" --> P585 --> "2013-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2094" --> P585 --> "2014-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2015-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2016-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-12" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-03" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-04" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-05" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-10" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-02" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2017-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-01" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-06" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-07" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-08" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-09" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-11" to "deprecated rank" (dod: 2009-09-05, precision 11)
* Set Q28058362 --> P1087 --> "2082" --> P585 --> "2018-10" to "deprecated rank" (dod: 2009-09-05, precision 11)

=== Item Q15116820 (21/22) ===

=== Item Q11711661 (22/22) ===

All done, task finished