import pywikibot
import csv
import re
import requests
import urllib.parse
import time
import re
import datetime

from pywikibot import pagegenerators as pg


with open('DACS-addStatus.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_dict = item.get() #Get the item dictionary
    clm_dict = item_dict["claims"] # Get the claim dictionary
    clm_list = clm_dict["P4663"] #DACS
    for clm in clm_list:
        clm_trgt = clm.getTarget()
        print(clm_trgt)
        url = "https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=" + clm_trgt
        print (url)
        r = requests.get(url)
        #time.sleep(2)
        text = r.text 
        text = text.replace('\n', ' ').replace('\r', '')
        pattern = 'DACS (does not represent|represents) this artist for Copyright Licensing'
        #DACS does not represent this artist for Copyright Licensing
        a = re.search(pattern, text)
        rep = ""
        code = clm_trgt
        if a:
            rep = a.group(1)
            print (rep)
            claim = pywikibot.Claim(repo, u'P6275')
            if (rep == "does not represent"):
                target = pywikibot.ItemPage(repo, u"Q71521142")
            if (rep == "represents"):
                target = pywikibot.ItemPage(repo, u"Q71528227")
            claim.setTarget(target)
            statedin = pywikibot.Claim(repo, u'P248')
            itis = pywikibot.ItemPage(repo, "Q71534274")
            statedin.setTarget(itis)
            
            dacscode = pywikibot.Claim(repo, u'P4663')
            dacscode.setTarget(clm_trgt)

            refdate = pywikibot.Claim(repo, u'P813')
            today = datetime.datetime.today()
            date = pywikibot.WbTime(year=today.year, month=today.month, day=today.day)
            refdate.setTarget(date)
#            claim.addSources([statedin, refdate], summary=u'Adding sources.')
            claim.addSources([statedin, refdate, dacscode], summary=u'Adding sources.')
            item.addClaim(claim, summary=u'Add DACS status')
#            with open("DACSstatus.csv", "a", newline="", encoding='utf-8') as csvf:
#                fields = ["qid", "code", "rep"]
#                writer = csv.DictWriter(csvf, fieldnames=fields)
                #writer.writeheader()
#                writer.writerow({"qid":item, "code":code, "rep":rep})
#            break

#            website = x.group(1)
#            if website.find("wikipedia") == -1:
#                if website.find(" ") == -1:
#                    stringclaim = pywikibot.Claim(repo, u'P856')
#                    stringclaim.setTarget(website)
#                    item.addClaim(stringclaim, summary=u'Adding URL from RKD')
                
 
 
6f0c6b84-88ea-e711-8b59-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=6f0c6b84-88ea-e711-8b59-000c29e811b2
does not represent
89d78d19-d9ec-e711-8b59-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=89d78d19-d9ec-e711-8b59-000c29e811b2
represents
Sleeping for 18.7 seconds, 2019-11-22 00:59:30
82c46fd4-af0a-dc11-81da-0015600b8875
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=82c46fd4-af0a-dc11-81da-0015600b8875
does not represent
Sleeping for 16.5 seconds, 2019-11-22 00:59:52
3355c250-c2e5-e711-8b59-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=3355c250-c2e5-e711-8b59-000c29e811b2
represents
Sleeping for 12.4 seconds, 2019-11-22 01:00:16
fdf5c5f3-34e3-e711-8b59-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=fdf5c5f3-34e3-e711-8b59-000c29e811b2
represents
Sleeping for 18.6 seconds, 2019-11-22 01:00:30
b34dea52-cf24-e111-9c2b-000c29604b72
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=b34dea52-cf24-e111-9c2b-000c29604b72
does not represent
Sleeping for 18.2 seconds, 2019-11-22 01:00:50
4aa14e2a-2606-e911-be07-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=4aa14e2a-2606-e911-be07-000c29e811b2
does not represent
Sleeping for 18.0 seconds, 2019-11-22 01:01:10
ca44b241-4e7e-e111-9c2b-000c29604b72
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=ca44b241-4e7e-e111-9c2b-000c29604b72
represents
Sleeping for 18.4 seconds, 2019-11-22 01:01:30
f3228e03-0dec-e711-8b59-000c29e811b2
https://www.dacs.org.uk/licensing-works/artist-search/artist-details?ArtistId=f3228e03-0dec-e711-8b59-000c29e811b2
represents
Sleeping for 17.9 seconds, 2019-11-22 01:01:51