import pywikibot
import csv
import re
import requests

from pywikibot import pagegenerators as pg

with open('makers.rq', 'r') as query_file:
    QUERY = query_file.read()

wikidata_site = pywikibot.Site("wikidata", "wikidata")
generator = pg.WikidataSPARQLPageGenerator(QUERY, site=wikidata_site)

for item in generator:
    
    # site = pywikibot.Site("wikidata", "wikidata")
    # repo = site.data_repository()
    # item = pywikibot.ItemPage(repo, "Q362796")


    item_dict = item.get() #Get the item dictionary
    clm_dict = item_dict["claims"] # Get the claim dictionary
    clm_list = clm_dict["P6582"] #instrumentmakers 
    for clm in clm_list:
        clm_trgt = clm.getTarget()
        print(clm_trgt)
        url = "http://www.dwc.knaw.nl/biografie/scientific-instrument-makers/?pagetype=authorDetail&aId=" + clm_trgt
        print (url)

        r = requests.get(url)
        r.text

        x = re.search(">Born: ?([^<]+)", r.text) 
        y = re.search(">Died: ?([^<]+)", r.text) 
        print ("RUN")
        birthdate =""
        deathdate=""
        if x:
            print (x.group(1))
            birthdate = x.group(1)
        if y:
            print (y.group(1))
            deathdate = y.group(1)
        with open('makers.csv', 'a', newline='') as csvfile:
            instrumentwriter = csv.writer(csvfile, delimiter=' ',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
            instrumentwriter.writerow([item.getID(), clm_trgt, birthdate, deathdate, url])
 
PE00001998
http://www.dwc.knaw.nl/biografie/scientific-instrument-makers/?pagetype=authorDetail&aId=PE00001998
RUN
Oudemirdum, 19-08-1743
Den Haag, 01-11-1810
PE00000918
http://www.dwc.knaw.nl/biografie/scientific-instrument-makers/?pagetype=authorDetail&aId=PE00000918
RUN
Haarlem, 20-03-1886
Waalre, 11-10-1968
PE00001775
http://www.dwc.knaw.nl/biografie/scientific-instrument-makers/?pagetype=authorDetail&aId=PE00001775
RUN
Delft, 20-03-1750
Haarlem, 26-12-1837
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-b72a81247a1e> in <module>()
     47             instrumentwriter.writerow([item.getID(), clm_trgt, birthdate, deathdate, url])
     48 
---> 49 instrumentwriter.close()

AttributeError: '_csv.writer' object has no attribute 'close'