import pywikibot as bot
from pywikibot import pagegenerators as pg

site = bot.Site('wikidata', 'wikidata')
enwiki = bot.Site("en", "wikipedia")

def id_via_enwiki(pagetitle):
    page = bot.Page(enwiki, pagetitle)
    if page.exists() and not page.isRedirectPage() and not page.isDisambig():
        item = bot.ItemPage.fromPage(page)
        item_dict = item.get()
        instance_of = item_dict["claims"]["P31"]
        if len(instance_of) == 1:
            clm_trgt = instance_of[0].getTarget()
            if clm_trgt.getID() == "Q5":
                return item.getID()
        
def qs_row_depicts(painting, depicted):
    return painting + "\t" + "P180" + "\t" + depicted
        
with open('query_npg.txt', 'r') as query_file:
    QUERY = query_file.read().replace('\n', '')

generator = pg.WikidataSPARQLPageGenerator(QUERY, site=site)

counter = 0
for item in generator:
    if counter > 99:
        break
    item_dict = item.get()
    itemID = item.getID()
    label = item_dict['labels']['en']
    wikidata_id = id_via_enwiki(label)
    if wikidata_id:
        print(qs_row_depicts(itemID, wikidata_id))
        counter = counter + 1

print("DONE")
Q28051948	P180	Q154594
Q28050959	P180	Q15526726
Q28051875	P180	Q460061
Q28048865	P180	Q333220
Sleeping for 5.0 seconds, 2017-05-27 21:32:26
Sleeping for 5.0 seconds, 2017-05-27 21:32:31
Sleeping for 7.0 seconds, 2017-05-27 21:32:36
Sleeping for 11.0 seconds, 2017-05-27 21:32:43
Q28043315	P180	Q313817
Q28051367	P180	Q459872
Q28049996	P180	Q157259
Q28050063	P180	Q193656
Q28044323	P180	Q953718
Q28045227	P180	Q1360330
Q28047646	P180	Q510281
Q28051415	P180	Q936439
Q28052256	P180	Q332323
Q28052082	P180	Q156814
Q28051595	P180	Q242666
Q28047862	P180	Q2619915
Q28050842	P180	Q211763
Q28052097	P180	Q37388
Q28051106	P180	Q922483
Q28051513	P180	Q334974
Q28050417	P180	Q243705
Q28051546	P180	Q6139120
Q28052049	P180	Q3843128
Q28052099	P180	Q188502
Q28052048	P180	Q320152
Q28051698	P180	Q734355
Q28051953	P180	Q965379
Q28051385	P180	Q134982
Q28050098	P180	Q207380
Q28052324	P180	Q206939
Q28050351	P180	Q2960442
Q28051898	P180	Q220078
Q28042564	P180	Q6245516
Q28051319	P180	Q5345969
Q28047851	P180	Q236236
Q28052150	P180	Q738016
Q28042561	P180	Q372211
Q28052088	P180	Q529849
Q28052163	P180	Q461571
Q28047812	P180	Q469269
Q28047999	P180	Q454635
Q28051448	P180	Q5343969
Q28050783	P180	Q935119
Q28052089	P180	Q9576
Q28048838	P180	Q294662
Q28051701	P180	Q734355
Q28044978	P180	Q16859394
Q28051412	P180	Q1282985
Q28051958	P180	Q428560
Q28051612	P180	Q7319337
Q28051355	P180	Q684978
Q28050242	P180	Q315511
Q28052061	P180	Q128976
Q28043719	P180	Q7788653
Q28050522	P180	Q333413
Q28049657	P180	Q173869
Q28051201	P180	Q540427
Q28051941	P180	Q718637
Q28048027	P180	Q2783637
Q28050135	P180	Q333072
Q28050414	P180	Q184366
Q28051391	P180	Q3049418
Q28049565	P180	Q167768
Q28049365	P180	Q860469
Q28052090	P180	Q2599
Q28048925	P180	Q25880
Q28052093	P180	Q7416
Q28047629	P180	Q6288018
Q28050534	P180	Q6259525
Q28048420	P180	Q82409
Q28051636	P180	Q573813
Q28052069	P180	Q9685
Q28051394	P180	Q3372615
Q28052560	P180	Q15437768
Q28051086	P180	Q4999130
Q28051842	P180	Q976071
Q28052253	P180	Q1077608
Q28052154	P180	Q241001
Q28051712	P180	Q599476
Q28051759	P180	Q359930
Q28051126	P180	Q40909
Q28052397	P180	Q3395844
Q28050637	P180	Q132805
Q28052394	P180	Q158370
Q28051983	P180	Q235308
Q28052071	P180	Q361396
Q28048095	P180	Q316016
Q28051083	P180	Q3568633
Q28044513	P180	Q2296770
Q28045040	P180	Q2072512
Q28050483	P180	Q314773
Q28051095	P180	Q200867
Q28052492	P180	Q5661381
Q28052457	P180	Q9545
Q28043529	P180	Q93128
Q28045025	P180	Q337505
Q28048334	P180	Q1398771
Q28042777	P180	Q354309
Q28050268	P180	Q182589
Q28048043	P180	Q898147
DONE