lng = 'nl'

import pywikibot
from pywikibot import pagegenerators as pg
import urllib
from pywikibot.data import api

import codecs
import sys
import datetime
from datetime import datetime, date, time




querylist=[ 
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2002-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2003-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1000-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2000-01-01T00:00:00Z"^^xsd:dateTime)) }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1990-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1991-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1991-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1992-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1992-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1993-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1993-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1994-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1994-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1995-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1995-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1996-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1996-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1997-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1997-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1998-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1998-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1999-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1999-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2000-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2000-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2001-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2001-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2002-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2003-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2004-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2004-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2005-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2005-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2006-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2006-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2007-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2007-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2008-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2008-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2009-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2009-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2010-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2010-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2011-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }' ,
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2011-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2012-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }' ,
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2012-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2013-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }' ,
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2013-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2014-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }' ,
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2014-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2015-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }' ,
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2015-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2016-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2016-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2017-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2017-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2018-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2018-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2019-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',
           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2019-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2020-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }',

           'SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     optional {?item wdt:P577 ?published} .     filter (!bound(?published))}',
           'SELECT ?item WHERE { ?item wdt:P31 wd:Q13442814.   ?item wdt:P577 ?published.   ?item wdt:P407 wd:Q1860.   FILTER((?published > "1900-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2021-01-01T00:00:00Z"^^xsd:dateTime))}',
           ]
 

sca={'de':'wissenschaftlicher Artikel', 
     'da':'videnskabelig artikel',
     'en':'scientific article',
     'fr':'article scientifique',
     'it':'articolo scientifico',
     'nl':'wetenschappelijk artikel', 
     'pt':'artigo científico',
     'sr':'научни чланак',
     'sv':'vetenskaplig artikel',
     'tr':'bilimsel makale',
     'ca':'article científic',
     'cs':'vědecký článek',
     'sk':'vedecký článok',
     }

pubtxt_full_date={
        'de':'veröffentlicht in',
        'da':'udgivet ',        
        'en':'published on',
        'fr':'publié',
        'it':'pubblicato il',
        'nl':'gepubliceerd op',
        'pt':'publicado na',
        'sr':'објављен',
        'sv':'publicerad på',
        'tr':'günü yayınlanan',
        'ca':'publicat el',
        'cs':'publikovaný v roce',
        'sk':'publikovaný',
        
        }     

pubtxt_year={
        'de':'veröffentlicht in',
        'da':'udgivet ',        
        'en':'published in',
        'fr':'publié',
        'it':'pubblicato il',
        'nl':'gepubliceerd in',
        'pt':'publicado na',
        'sr':'објављен',
        'sv':'publicerad på',
        'tr':'günü yayınlanan',
        'ca':'publicat el',
        'cs':'publikovaný v roce',
        'sk':'publikovaný',
        
        }     

month= {'de':{'Januar','Februar','Marz','','','',''}}
        
def wd_from_file():
  repo=pywikibot.Site('wikidata','wikidata').data_repository()
  csvfile=open('scientific-query.csv','r')
  for alllines in csvfile:
    try:
      qitem=alllines[alllines.find('Q'):alllines.find(',')]
      if (len(qitem)>0):
        wditem=pywikibot.ItemPage(repo,qitem)
        wditem.get(get_redirect=True)
        yield wditem
    except:
      pass
     
def wd_sparql_query(spq):
  wikidatasite=pywikibot.Site('wikidata','wikidata') 
  generator=pg.WikidataSPARQLPageGenerator(spq,site=wikidatasite)
  for wd in generator:
    try:
      #log_premature(wd.title())
      wd.get(get_redirect=True)
      yield wd
    except:
      pass    

def sparql_nodescription(sparql):
  return 'select distinct ?item where {{%s}filter (!bound(?itemDescription))}' % sparql 
  
def log_premature(itemno):
  with codecs.open("scientific-article-cs.prelog.csv","a", encoding="utf-8") as logfile:
    logfile.write('%s\n' % (itemno))
  logfile.close
   
def go_sparql():  
 written=0
 for sparql_query in querylist: #need to split the query in at least two parts to avoid time outs
    print('Next SparQL: %s' % (sparql_query))
    for item in wd_sparql_query(sparql_query):
        if not(lng in item.descriptions) or (lng=='de'):
          replace=True
        else:
          replace=item.descriptions[lng]==sca[lng]
        
        if (replace):
            if ('P577' in item.claims):
              publicationdate=item.claims.get('P577')[0].getTarget()
              if (publicationdate.year==0):
                  publicationtxt = ''
              if (lng=='cs'):
                 publicationtxt = '%s %s' % (pubtxt_year[lng],publicationdate.year)
              else :
                if (publicationdate.month==0):
                   publicationtxt='(%s %04d)' % (pubtxt_year[lng],publicationdate.year)
                elif (publicationdate.day==0):
                   publicationtxt='(%s %04d-%02d)' % (pubtxt_year[lng],publicationdate.year,publicationdate.month)
                else:
                  publicationtxt='(%s %04d/%02d/%02d)' % (pubtxt_full_date[lng],publicationdate.year,publicationdate.month,publicationdate.day)
            else:
              publicationtxt=''       
            
            if (publicationtxt!=''):
              data={}
              data.update( {'descriptions': {lng:'%s %s'%(sca[lng],publicationtxt)}} )
              if (True):
                try:
                  item.editEntity(data,summary=u'%s description scientific article' % (lng))
                  written += 1
                except:
                  pass		   
                print('%s %d %s: %s' % (item.title(),written, lng,publicationtxt))
                #print(0/(written%5))
              else:
                print('Would add: %s' % (data))
        else:
          #print('%s is: %s' % (item.title(),item.descriptions[lng]))   
          pass
  
def main():
 site=pywikibot.Site('nl')
 repo=site.data_repository()
 
 go_sparql()
 print('Klaar!')
       
       
main()
 
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1000-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2000-01-01T00:00:00Z"^^xsd:dateTime)) }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1990-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1991-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1991-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1992-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1992-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1993-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1993-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1994-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1994-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1995-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1995-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1996-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1996-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1997-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1997-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1998-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1998-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "1999-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "1999-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2000-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2000-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2001-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2001-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2002-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2002-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2003-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2003-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2004-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2004-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2005-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2005-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2006-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2006-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2007-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2007-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2008-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2008-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2009-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2009-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2010-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2010-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2011-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2011-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2012-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2012-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2013-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2013-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2014-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2014-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2015-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2015-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2016-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2016-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2017-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2017-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2018-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2018-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2019-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     ?item wdt:P577 ?published .     filter ((?published > "2019-01-01T00:00:00Z"^^xsd:dateTime) && (?published < "2020-01-01T00:00:00Z"^^xsd:dateTime)) ?item wdt:P407 wd:Q150 . }
Next SparQL: SELECT ?item  WHERE {?item wdt:P31 wd:Q13442814 .     optional {?item wdt:P577 ?published} .     filter (!bound(?published))}
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/srv/paws/lib/python3.6/site-packages/urllib3/response.py in _update_chunk_length(self)
    571         try:
--> 572             self.chunk_left = int(line, 16)
    573         except ValueError:

ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

IncompleteRead                            Traceback (most recent call last)
/srv/paws/lib/python3.6/site-packages/urllib3/response.py in _error_catcher(self)
    330             try:
--> 331                 yield
    332 

/srv/paws/lib/python3.6/site-packages/urllib3/response.py in read_chunked(self, amt, decode_content)
    636             while True:
--> 637                 self._update_chunk_length()
    638                 if self.chunk_left == 0:

/srv/paws/lib/python3.6/site-packages/urllib3/response.py in _update_chunk_length(self)
    575             self.close()
--> 576             raise httplib.IncompleteRead(line)
    577 

IncompleteRead: IncompleteRead(0 bytes read)

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
/srv/paws/lib/python3.6/site-packages/requests/models.py in generate()
    748                 try:
--> 749                     for chunk in self.raw.stream(chunk_size, decode_content=True):
    750                         yield chunk

/srv/paws/lib/python3.6/site-packages/urllib3/response.py in stream(self, amt, decode_content)
    460         if self.chunked and self.supports_chunked_reads():
--> 461             for line in self.read_chunked(amt, decode_content=decode_content):
    462                 yield line

/srv/paws/lib/python3.6/site-packages/urllib3/response.py in read_chunked(self, amt, decode_content)
    664             if self._original_response:
--> 665                 self._original_response.close()
    666 

/usr/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     98             try:
---> 99                 self.gen.throw(type, value, traceback)
    100             except StopIteration as exc:

/srv/paws/lib/python3.6/site-packages/urllib3/response.py in _error_catcher(self)
    348                 # This includes IncompleteRead.
--> 349                 raise ProtocolError('Connection broken: %r' % e, e)
    350 

ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

During handling of the above exception, another exception occurred:

ChunkedEncodingError                      Traceback (most recent call last)
<ipython-input-3-b31c9c7b9150> in <module>()
    187 
    188 
--> 189 main()
    190 

<ipython-input-3-b31c9c7b9150> in main()
    183  repo=site.data_repository()
    184 
--> 185  go_sparql()
    186  print('Klaar!')
    187 

<ipython-input-3-b31c9c7b9150> in go_sparql()
    139  for sparql_query in querylist: #need to split the query in at least two parts to avoid time outs
    140     print('Next SparQL: %s' % (sparql_query))
--> 141     for item in wd_sparql_query(sparql_query):
    142         if not(lng in item.descriptions) or (lng=='de'):
    143           replace=True

<ipython-input-3-b31c9c7b9150> in wd_sparql_query(spq)
    118 def wd_sparql_query(spq):
    119   wikidatasite=pywikibot.Site('wikidata','wikidata')
--> 120   generator=pg.WikidataSPARQLPageGenerator(spq,site=wikidatasite)
    121   for wd in generator:
    122     try:

/srv/paws/pwb/pywikibot/pagegenerators.py in WikidataSPARQLPageGenerator(query, site, item_name, endpoint, entity_url, result_type)
   3000     data = query_object.get_items(query,
   3001                                   item_name=item_name,
-> 3002                                   result_type=result_type)
   3003     items_pages = (pywikibot.ItemPage(repo, item) for item in data
   3004                    if pywikibot.ItemPage.is_valid_id(item))

/srv/paws/pwb/pywikibot/data/sparql.py in get_items(self, query, item_name, result_type)
    192         @rtype: same as result_type
    193         """
--> 194         res = self.select(query, full_data=True)
    195         if res:
    196             return result_type(r[item_name].getID() for r in res)

/srv/paws/pwb/pywikibot/data/sparql.py in select(self, query, full_data, headers)
    112         @return: List of query results or None if query failed
    113         """
--> 114         data = self.query(query, headers=headers)
    115         if data and 'results' in data:
    116             result = []

/srv/paws/pwb/pywikibot/data/sparql.py in query(self, query, headers)
    145         while True:
    146             try:
--> 147                 self.last_response = http.fetch(url, headers=headers)
    148                 if not self.last_response.text:
    149                     return None

/srv/paws/pwb/pywikibot/comms/http.py in fetch(uri, method, params, body, headers, default_error_handling, use_fake_user_agent, data, **kwargs)
    528     # may be caught.
    529     if default_error_handling:
--> 530         error_handling_callback(request)
    531     return request

/srv/paws/pwb/pywikibot/comms/http.py in error_handling_callback(request)
    413     # if all else fails
    414     if isinstance(request.data, Exception):
--> 415         raise request.data
    416 
    417     if request.status == 504:

/srv/paws/pwb/pywikibot/comms/http.py in _http_process(session, http_request)
    392                                    headers=headers, auth=auth, timeout=timeout,
    393                                    verify=not ignore_validation,
--> 394                                    **http_request.kwargs)
    395     except Exception as e:
    396         http_request.data = e

/srv/paws/lib/python3.6/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    510         }
    511         send_kwargs.update(settings)
--> 512         resp = self.send(prep, **send_kwargs)
    513 
    514         return resp

/srv/paws/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    660 
    661         if not stream:
--> 662             r.content
    663 
    664         return r

/srv/paws/lib/python3.6/site-packages/requests/models.py in content(self)
    825                 self._content = None
    826             else:
--> 827                 self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
    828 
    829         self._content_consumed = True

/srv/paws/lib/python3.6/site-packages/requests/models.py in generate()
    750                         yield chunk
    751                 except ProtocolError as e:
--> 752                     raise ChunkedEncodingError(e)
    753                 except DecodeError as e:
    754                     raise ContentDecodingError(e)

ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))