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

def makeSource():
    refUrl = pywikibot.Claim(repo, 'P854')
    refUrl.setTarget(value='http://www.worldrowing.com/athletes/thomas-keller-medal')
    retrieved = pywikibot.Claim(repo, 'P813')
    retrieved.setTarget(value=pywikibot.WbTime(year=2017, month=1, day=20))
    extTitle = pywikibot.Claim(repo, 'P1476')
    extTitle.setTarget(value=pywikibot.WbMonolingualText(text='Thomas Keller Medal', language='en'))
    publisher = pywikibot.Claim(repo, 'P123')
    publisher.setTarget(value=pywikibot.ItemPage(repo, 'Q684885'));
    return [refUrl, retrieved, extTitle, publisher]

# all are properly sourced meanwhile; don't run again
awardees = () # 'Q1441022', 'Q586223', 'Q636112', 'Q683554', 'Q451642', 'Q721353', 'Q1369096', 'Q276075', 'Q99417', 'Q445352', 'Q530803', 'Q52679', 'Q445381', 'Q2073706', 'Q46095', 'Q52680', 'Q395556', 'Q379430', 'Q233689', 'Q262404', 'Q1681200', 'Q704436', 'Q1339220', 'Q719576', 'Q1258722', 'Q1230878', 'Q452954', 'Q452962');
for awardee in awardees:
    Qitem = pywikibot.ItemPage(repo, awardee)
    Qitem.get()
    if Qitem.claims:
        if 'P166' in Qitem.claims.keys():
            claims = Qitem.claims['P166']
            for claim in claims:
                if claim._type == 'wikibase-item':
                    if claim.target.id == 'Q634513':
                        print('Found P166:Q634513 in ', Qitem, sep='')                        
                        source = makeSource()
                        #print('Potential source: ', source)
                        #claim.addSources(source)
                    #else:
                        #print('No claim P166:Q634513 found in ', Qitem, sep='')
                else:
                    print('Claim P166 not of wikibase-item type')
        else:
            print('No P166 claim found')
    else:
        print('No claims found')
Found P166:Q634513 in [[wikidata:Q1441022]]
Found P166:Q634513 in [[wikidata:Q586223]]
Sleeping for 8.7 seconds, 2017-01-25 08:24:46
Found P166:Q634513 in [[wikidata:Q636112]]
Sleeping for 9.4 seconds, 2017-01-25 08:24:55
Found P166:Q634513 in [[wikidata:Q683554]]
Sleeping for 9.3 seconds, 2017-01-25 08:25:05
Found P166:Q634513 in [[wikidata:Q451642]]
Sleeping for 8.9 seconds, 2017-01-25 08:25:16
Found P166:Q634513 in [[wikidata:Q721353]]
Sleeping for 8.8 seconds, 2017-01-25 08:25:26
Found P166:Q634513 in [[wikidata:Q1369096]]
Sleeping for 8.9 seconds, 2017-01-25 08:25:36
Found P166:Q634513 in [[wikidata:Q276075]]
Sleeping for 8.8 seconds, 2017-01-25 08:25:46
Found P166:Q634513 in [[wikidata:Q99417]]
Sleeping for 9.4 seconds, 2017-01-25 08:25:55
Found P166:Q634513 in [[wikidata:Q445352]]
Sleeping for 8.8 seconds, 2017-01-25 08:26:06
Found P166:Q634513 in [[wikidata:Q530803]]
Sleeping for 9.4 seconds, 2017-01-25 08:26:15
Found P166:Q634513 in [[wikidata:Q52679]]
Sleeping for 8.7 seconds, 2017-01-25 08:26:26
Found P166:Q634513 in [[wikidata:Q445381]]
Sleeping for 9.2 seconds, 2017-01-25 08:26:36
Found P166:Q634513 in [[wikidata:Q2073706]]
Sleeping for 9.4 seconds, 2017-01-25 08:26:45
Found P166:Q634513 in [[wikidata:Q46095]]
Sleeping for 9.4 seconds, 2017-01-25 08:26:55
Found P166:Q634513 in [[wikidata:Q52680]]
Sleeping for 9.3 seconds, 2017-01-25 08:27:05
Found P166:Q634513 in [[wikidata:Q395556]]
Sleeping for 9.3 seconds, 2017-01-25 08:27:16
Found P166:Q634513 in [[wikidata:Q379430]]
Sleeping for 8.6 seconds, 2017-01-25 08:27:26
Found P166:Q634513 in [[wikidata:Q233689]]
Sleeping for 9.4 seconds, 2017-01-25 08:27:35
Found P166:Q634513 in [[wikidata:Q262404]]
Sleeping for 9.1 seconds, 2017-01-25 08:27:46
Found P166:Q634513 in [[wikidata:Q1681200]]
Sleeping for 8.8 seconds, 2017-01-25 08:27:56
Found P166:Q634513 in [[wikidata:Q704436]]
Sleeping for 9.2 seconds, 2017-01-25 08:28:06
Found P166:Q634513 in [[wikidata:Q1339220]]
Sleeping for 8.8 seconds, 2017-01-25 08:28:16
Found P166:Q634513 in [[wikidata:Q719576]]
Sleeping for 9.3 seconds, 2017-01-25 08:28:26
Found P166:Q634513 in [[wikidata:Q1258722]]
Sleeping for 9.3 seconds, 2017-01-25 08:28:36
Found P166:Q634513 in [[wikidata:Q1230878]]
Sleeping for 9.0 seconds, 2017-01-25 08:28:46
Found P166:Q634513 in [[wikidata:Q452954]]
Sleeping for 8.5 seconds, 2017-01-25 08:28:56
Found P166:Q634513 in [[wikidata:Q452962]]
Sleeping for 9.4 seconds, 2017-01-25 08:29:05