#2019 10 16 21:03
import pywikibot
from pywikibot import pagegenerators as pg

language='nl'
site=pywikibot.Site(language,'wikipedia')
repo=site.data_repository()
ldesc={'nl':'Wikimedia-categorie'}
done=[]

def addCat2WD(cat):
  if (cat.title() in done):
    return()
  if not (cat.title()[:10].lower()=='categorie:'): 
    print(cat.title()[:10])
    print(1/0)
    
    
  if not('wikibase_item' in cat.properties()):
    print(cat.title())
    wd=repo.editEntity({},{},summary='#addnewcat')
    wd=pywikibot.ItemPage(repo,wd['entity']['id'])
    wd.get()
    ld={'labels':{language:cat.title()}}
    ld.update({'descriptions':{language:ldesc[language]}})
    wd.editEntity(ld,summary='add label and description for category')
    wd.setSitelink(sitelink={'site':language+'wiki','title':cat.title()},summary='set cat-link')
    claim=pywikibot.Claim(repo,'P31')
    claim.setTarget(pywikibot.ItemPage(repo,'Q4167836'))
    wd.addClaim(claim,summary='it is a category')
    done.append(cat.title())
    #if (len(done)>100):
    #    done.pop(0)

def allCatsfromCat(rootcat):    
  cat=pywikibot.Category(site,rootcat)
  for subcat in pg.SubCategoriesPageGenerator(cat,123):
    #print(subcat.title())
    addCat2WD(subcat)

    
print('Start')
#for c in ['Categorie:Portaal:In het nieuws/Sportnieuws in 2004','Categorie:Portaal:In het nieuws/Sportnieuws in 2005','Categorie:Portaal:In het nieuws/Sportnieuws in 2006','Categorie:Portaal:In het nieuws/Sportnieuws in 2007','Categorie:Portaal:In het nieuws/Sportnieuws in 2008','Categorie:Portaal:In het nieuws/Sportnieuws in 2009','Categorie:Portaal:In het nieuws/Sportnieuws in 2010','Categorie:Portaal:In het nieuws/Sportnieuws in 2011','Categorie:Portaal:In het nieuws/Sportnieuws in 2012','Categorie:Portaal:In het nieuws/Sportnieuws in 2013']:
#  catpage=pywikibot.Page(site,c)
#  addCat2WD(catpage)
#allCatsfromCat('Categorie:Politiek in Nederland')
#allCatsfromCat('Categorie:Bouwwerk in Nederland')
#allCatsfromCat('Categorie:Geografie van Nederland')
#allCatsfromCat('Categorie:Nederlandse organisatie')
allCatsfromCat('Categorie:Lijsten van Nederland')
allCatsfromCat('Categorie:Natuur in Nederland')
allCatsfromCat('Categorie:Cultuur in Nederland')
allCatsfromCat('Categorie:Sport in Nederland')
#allCatsfromCat('')
#allCatsfromCat('')
#allCatsfromCat('')
#print(catpage.properties())
print('Klaar')
Start
Categorie:Lijsten van Friesland
Sleeping for 9.5 seconds, 2019-10-17 12:53:16
Sleeping for 9.7 seconds, 2019-10-17 12:53:26
Sleeping for 9.6 seconds, 2019-10-17 12:53:36
Categorie:Nationale Plantencollectie
Sleeping for 5.7 seconds, 2019-10-17 12:53:50
Sleeping for 9.5 seconds, 2019-10-17 12:53:56
Sleeping for 9.6 seconds, 2019-10-17 12:54:06
Sleeping for 9.6 seconds, 2019-10-17 12:54:16
Categorie:Heemtuin in Nederland
Sleeping for 9.0 seconds, 2019-10-17 12:54:27
Sleeping for 9.6 seconds, 2019-10-17 12:54:36
Sleeping for 9.6 seconds, 2019-10-17 12:54:46
Sleeping for 9.6 seconds, 2019-10-17 12:54:56
Categorie:Den Treek-Henschoten
Sleeping for 7.2 seconds, 2019-10-17 12:55:09
Sleeping for 9.6 seconds, 2019-10-17 12:55:16
Sleeping for 9.7 seconds, 2019-10-17 12:55:26
Sleeping for 9.6 seconds, 2019-10-17 12:55:36
Categorie:Kampgevangene in Amersfoort
Sleeping for 9.3 seconds, 2019-10-17 12:55:47
Sleeping for 9.6 seconds, 2019-10-17 12:55:57
Sleeping for 9.6 seconds, 2019-10-17 12:56:06
Sleeping for 9.5 seconds, 2019-10-17 12:56:17
Categorie:Deltaprogramma
Sleeping for 9.5 seconds, 2019-10-17 12:57:47
Sleeping for 9.3 seconds, 2019-10-17 12:57:57
Sleeping for 8.6 seconds, 2019-10-17 12:58:08
Categorie:Voormalige gracht in Friesland
Sleeping for 9.5 seconds, 2019-10-17 12:58:45
Sleeping for 9.6 seconds, 2019-10-17 12:58:55
Sleeping for 9.6 seconds, 2019-10-17 12:59:05
Categorie:Marken (eiland)
Sleeping for 9.4 seconds, 2019-10-17 13:01:25
Sleeping for 9.7 seconds, 2019-10-17 13:01:35
Sleeping for 9.5 seconds, 2019-10-17 13:01:45
Categorie:Watersnoodmonument in Nederland
Sleeping for 9.6 seconds, 2019-10-17 13:02:18
Sleeping for 9.7 seconds, 2019-10-17 13:02:28
Sleeping for 9.6 seconds, 2019-10-17 13:02:38
Categorie:Ambtenaar van Rijkswaterstaat
Sleeping for 9.3 seconds, 2019-10-17 13:02:48
Sleeping for 9.5 seconds, 2019-10-17 13:02:58
Sleeping for 9.6 seconds, 2019-10-17 13:03:08
Sleeping for 9.3 seconds, 2019-10-17 13:03:18
Categorie:Sluis in Nieuwegein
Sleeping for 9.6 seconds, 2019-10-17 13:04:19
Sleeping for 9.7 seconds, 2019-10-17 13:04:29
Sleeping for 9.6 seconds, 2019-10-17 13:04:39
Categorie:Nederlands hulpschip
Sleeping for 9.5 seconds, 2019-10-17 13:04:59
Sleeping for 9.6 seconds, 2019-10-17 13:05:09
Sleeping for 9.6 seconds, 2019-10-17 13:05:19
Categorie:Voormalig waterschap in de gemeente Baarn
Sleeping for 9.5 seconds, 2019-10-17 13:05:48
Sleeping for 9.6 seconds, 2019-10-17 13:05:58
Sleeping for 9.5 seconds, 2019-10-17 13:06:08
Categorie:Voormalig waterschap in de gemeente Bunschoten
Sleeping for 9.5 seconds, 2019-10-17 13:06:19
Sleeping for 9.5 seconds, 2019-10-17 13:06:29
Sleeping for 9.6 seconds, 2019-10-17 13:06:38
Sleeping for 9.6 seconds, 2019-10-17 13:06:48
Categorie:Hoogheemraadschap Hollands Noorderkwartier
Sleeping for 6.7 seconds, 2019-10-17 13:07:01
Sleeping for 9.6 seconds, 2019-10-17 13:07:08
Sleeping for 9.7 seconds, 2019-10-17 13:07:18
Sleeping for 9.6 seconds, 2019-10-17 13:07:28
Categorie:Waterschapshuis
Sleeping for 9.5 seconds, 2019-10-17 13:07:49
Sleeping for 9.7 seconds, 2019-10-17 13:07:58
Sleeping for 9.6 seconds, 2019-10-17 13:08:09
Categorie:Het Scheepvaartmuseum
Sleeping for 9.5 seconds, 2019-10-17 13:09:50
WARNING: API error modification-failed: Item [[Q62433075|Q62433075]] already has label "Categorie:Het Scheepvaartmuseum" associated with language code nl, using the same description text.
---------------------------------------------------------------------------
APIError                                  Traceback (most recent call last)
/srv/paws/pwb/pywikibot/page.py in handle(func, self, *args, **kwargs)
    106         try:
--> 107             func(self, *args, **kwargs)
    108         # TODO: other "expected" error types to catch?

/srv/paws/pwb/pywikibot/page.py in editEntity(self, data, **kwargs)
   4216         updates = self.repo.editEntity(self._defined_by(singular=True), data,
-> 4217                                        baserevid=baserevid, **kwargs)
   4218         self.latest_revision_id = updates['entity']['lastrevid']

/srv/paws/pwb/pywikibot/site.py in callee(self, *args, **kwargs)
   1319                 raise Exception('Not implemented')
-> 1320             return fn(self, *args, **kwargs)
   1321 

/srv/paws/pwb/pywikibot/site.py in editEntity(self, identification, data, bot, **kwargs)
   7838         req = self._simple_request(**params)
-> 7839         data = req.submit()
   7840         return data

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   2078 
-> 2079                 raise APIError(**result['error'])
   2080             except TypeError:

APIError: modification-failed: Item [[Q62433075|Q62433075]] already has label "Categorie:Het Scheepvaartmuseum" associated with language code nl, using the same description text. [messages:[{'name': 'wikibase-validator-label-with-description-conflict', 'parameters': ['Categorie:Het Scheepvaartmuseum', 'nl', '[[Q62433075|Q62433075]]'], 'html': {'*': 'Item <a href="/wiki/Q62433075" title="Q62433075">Q62433075</a> heeft label "Categorie:Het Scheepvaartmuseum" al gekoppeld aan taalcode nl, met dezelfde beschrijving.'}}]; help:See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.]

During handling of the above exception, another exception occurred:

OtherPageSaveError                        Traceback (most recent call last)
<ipython-input-11-962d5c7071c6> in <module>
     50 allCatsfromCat('Categorie:Lijsten van Nederland')
     51 allCatsfromCat('Categorie:Natuur in Nederland')
---> 52 allCatsfromCat('Categorie:Cultuur in Nederland')
     53 allCatsfromCat('Categorie:Sport in Nederland')
     54 #allCatsfromCat('')

<ipython-input-11-962d5c7071c6> in allCatsfromCat(rootcat)
     37   for subcat in pg.SubCategoriesPageGenerator(cat,123):
     38     #print(subcat.title())
---> 39     addCat2WD(subcat)
     40 
     41 

<ipython-input-11-962d5c7071c6> in addCat2WD(cat)
     24     ld={'labels':{language:cat.title()}}
     25     ld.update({'descriptions':{language:ldesc[language]}})
---> 26     wd.editEntity(ld,summary='add label and description for category')
     27     wd.setSitelink(sitelink={'site':language+'wiki','title':cat.title()},summary='set cat-link')
     28     claim=pywikibot.Claim(repo,'P31')

/srv/paws/pwb/pywikibot/page.py in wrapper(self, *args, **kwargs)
    126             pywikibot.async_request(handle, func, self, *args, **kwargs)
    127         else:
--> 128             handle(func, self, *args, **kwargs)
    129 
    130     manage_wrapping(wrapper, func)

/srv/paws/pwb/pywikibot/page.py in handle(func, self, *args, **kwargs)
    118                 if isinstance(err, pywikibot.PageSaveRelatedError):
    119                     raise err
--> 120                 raise pywikibot.OtherPageSaveError(self, err)
    121         if callback:
    122             callback(self, err)

OtherPageSaveError: Edit to page [[wikidata:Q71272988]] failed:
modification-failed: Item [[Q62433075|Q62433075]] already has label "Categorie:Het Scheepvaartmuseum" associated with language code nl, using the same description text. [messages:[{'name': 'wikibase-validator-label-with-description-conflict', 'parameters': ['Categorie:Het Scheepvaartmuseum', 'nl', '[[Q62433075|Q62433075]]'], 'html': {'*': 'Item <a href="/wiki/Q62433075" title="Q62433075">Q62433075</a> heeft label "Categorie:Het Scheepvaartmuseum" al gekoppeld aan taalcode nl, met dezelfde beschrijving.'}}]; help:See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.]
Categorie:Rijksuniversiteit Leuven
Categorie:Leuvense streekbussen
Categorie:Lijsten van Belgische kunstenaars
Categorie:Lijsten van Vlaamse kunstenaars
Categorie:Lijsten van Waalse politici
Categorie:Lijsten van hoge gebouwen in Belgiƫ
    
  File "<ipython-input-8-cc33197285f6>", line 1
    Categorie:Rijksuniversiteit Leuven
                                     ^
SyntaxError: invalid syntax