import pywikibot
from pywikibot import pagegenerators as pg
import requests

site = pywikibot.Site('wikidata','wikidata')           #Geef aan naar welke site je wilt schrijven
repo = site.data_repository()                     #voor ophalen wikidata-items adhv Qxxxx
baseURL='https://www.researcherid.com/rid/%s'


def wd_sparql_query(spq):
   generator=pg.WikidataSPARQLPageGenerator(spq,site=pywikibot.Site('wikidata','wikidata'))
   for wd in generator:
     try:
       wd.get(get_redirect=True)
       yield wd
     except:
       pass

def get_publon(wd):
  if not('P1053' in wd.claims): return('')
  id=wd.claims['P1053'][0].getTarget()
  print(f'wd={wd.title()}, id={id}')  
  f = requests.get(baseURL%id) 
  start=f.text.find('content="https://publons.com/researcher/')
  if start==-1: return('')
  stop=f.text[start+40:].find('/')
  #print(f.text[start+40:start+40+stop])  
  return(f.text[start+40:start+40+stop])

def set_publon(wd,publon):
    publonclaim=pywikibot.Claim();

query='select ?item ?publon where  {?item wdt:P3829 ?publon}'

def delfromoneentry(wd):
  c=wd.claims['P3829']
  if len(c)==2:
    print(wd.title())    
    wd.removeClaims(c[1],summary='enough=enough')
    

print('Begonnen')
counter=0
for wd in wd_sparql_query(query):
  counter+=1
  delfromoneentry(wd)
  
#wd=pywikibot.ItemPage(repo,'Q56887746')
#wd.get()
#delfromoneentry(wd)
print('Klaar')
Begonnen
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
WARNING: wikibase-lexeme datatype is not supported yet.
Klaar