#20190722 10:52
import pywikibot
from pywikibot import pagegenerators as pg
from datetime import datetime

wikinewslemma='Q17633526'
languages={'nl':'Q7411','en':'Q1860'}
datetemplate={'nl':'Datum|'}
lng='nl'
neglect=[]
monthconvert={'januari':'01','februari':'02','maart':'03','april':'04','mei':'05','juni':'06','juli':'07','augustus':'08','september':'09','oktober':'10','november':'11','december':'12',}
dateofpub='P577'

class wikiNews():
 wd=None
 title=None
 pubdate=None #P577
 language=None #P407
 mainsubject=None #P921
    
 def createWD(self): 
   self.wd=repo.editEntity({},{},summary='#createwikinewsitem')
   self.wd=pywikibot.ItemPage(repo,self.wd['entity']['id'])
   self.wd.get()

 def addOneProperty(self,P,V,summary='',onlyaddnew=True):
   if (not((P in self.wd.claims) and onlyaddnew)) and (V!=None) and (not(V in neglect)):
      claim=pywikibot.Claim(repo,P)
      target=pywikibot.ItemPage(repo,V)
      claim.setTarget(target)
      self.wd.addClaim(claim,summary=summary)

 def setLabels(self,language,pagename):
   lbldata={'labels':{}}
   lbldata['labels'].update({language:pagename})
   if lbldata['labels']!={}:
     self.wd.editEntity(lbldata,summary=f'set label')

 def attachPage(self,language,pagename):
   self.wd.setSitelink({'site':language+'wikinews','title':pagename},summary='set link')

 def getDate(self,page,lng):
   if (lng in datetemplate):
     spos=page.text.find('{{'+datetemplate[lng])
     if (spos>=0):
       endpos=page.text[spos:].find('}}') 
       datestr=page.text[spos+2+len(datetemplate[lng]):endpos] 
       print(f'Datum: {datestr}')
       start=datestr.find(' ')+1
       end=datestr[start+1:].find(' ')+4
       month=datestr[start:end]
       print(month) 
       if (month in monthconvert): 
         datestr=datestr.replace(month,monthconvert[month])
         print(f'Converted: [{datestr}]')
         self.pubdate=datetime.strptime(datestr, '%d %m %Y')        
       else:
          print(f'Unknown month: [{month}] in [{datestr}]')
            
       

 def setDate(self,lng):
    if (self.pubdate):
      print(self.pubdate)
      wbPubdate=pywikibot.WbTime(year=self.pubdate.year, month=self.pubdate.month, day=self.pubdate.day, precision='day')
      claim=pywikibot.Claim(repo,dateofpub)   
      claim.setTarget(wbPubdate)
      self.wd.addClaim(claim,summary='set publication date from template')  
    
#======[class wikiNews]======

def processWikinewsPage(page,lng):
    title=page.title()
    wdt=None
    wn=wikiNews()  
    if ('wikibase_item' in page.properties()):
      wn.wd=page.data_item()  
      wn.wd.get(get_redirect=True)
      wdt=wn.wd.title()
    else:    
      wn.createWD()
      wn.attachPage(lng,page.title())
        
    if (not(lng in wn.wd.labels)):    
      wn.setLabels(lng,page.title()) 
    print(f'{title}-{wdt}')
    wn.addOneProperty('P31',wikinewslemma,summary='newItem4wikinews')  
    if (lng in languages):
      wn.addOneProperty('P407',languages[lng],summary='set language of item')
    if (not(dateofpub in wn.wd.claims)):
      wn.getDate(page,lng)
      wn.setDate(lng)
    #print(10/0) #force error to check outcome
        
        
    
    
#==============[main()]====================    
print('Start')
site=pywikibot.Site(lng,'wikinews')
repo=site.data_repository()

gen=pg.NewpagesPageGenerator(site,0,500)
for page in gen:
    processWikinewsPage(page,lng)
print('Klaar')
Start
Datum: 

Unknown month: [] in []
WHO verklaart ebola-epidemie in Congo tot PHEIC-Q65792787
Drie doden bij stranden Scheveningen en Wassenaar-Q65845992
Iran confisqueert Britse olietanker in Straat van Hormuz-Q65713119
India: woonflat ingestort in Dongri-Q65769342
Von der Leyen verkozen tot nieuwe EC-voorzitter-Q65622106
Lancering Vega-raket mislukt-Q65502887
Fatale explosie appartement Vlaardingen was opzet-Q65769652
Datum: 6 juli 2019
juli 
Unknown month: [juli ] in [6 juli 2019]
Californië getroffen door twee zware aardbevingen-Q65034920
Groningen: 100.000 kippen omgekomen bij grote schuurbrand-Q65769666
Datum: 4 juli 2019
juli 
Unknown month: [juli ] in [4 juli 2019]
Zeker 53 doden bij luchtaanval op migrantencentrum bij Tripoli-Q65095596
India: zeker 43 doden door moesson-Q65769668
Datum: 4 juli 2019
juli 
Unknown month: [juli ] in [4 juli 2019]
Orgelkids viert tiende verjaardag-Q65769682
Datum: 4 juli 2019
juli 
Unknown month: [juli ] in [4 juli 2019]
Holleeder veroordeeld tot levenslang-Q65769691
Sleeping for 9.4 seconds, 2019-07-23 17:21:11
WARNING: API error failed-save: The save has failed.
---------------------------------------------------------------------------
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: failed-save: The save has failed. [help:See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> 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-52-8e33b7bfaa03> in <module>
    103 gen=pg.NewpagesPageGenerator(site,0,500)
    104 for page in gen:
--> 105     processWikinewsPage(page,lng)
    106 print('Klaar')

<ipython-input-52-8e33b7bfaa03> in processWikinewsPage(page, lng)
     80     else:
     81       wn.createWD()
---> 82       wn.attachPage(lng,page.title())
     83 
     84     if (not(lng in wn.wd.labels)):

<ipython-input-52-8e33b7bfaa03> in attachPage(self, language, pagename)
     38 
     39  def attachPage(self,language,pagename):
---> 40    self.wd.setSitelink({'site':language+'wikinews','title':pagename},summary='set link')
     41 
     42  def getDate(self,page,lng):

/srv/paws/pwb/pywikibot/page.py in setSitelink(self, sitelink, **kwargs)
   4577         or a {'site':dbname,'title':title} dictionary.
   4578         """
-> 4579         self.setSitelinks([sitelink], **kwargs)
   4580 
   4581     def removeSitelink(self, site, **kwargs):

/srv/paws/pwb/pywikibot/page.py in setSitelinks(self, sitelinks, **kwargs)
   4618                 data[db_name] = obj
   4619         data = {'sitelinks': data}
-> 4620         self.editEntity(data, **kwargs)
   4621 
   4622     def mergeInto(self, item, **kwargs):

/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:Q65921522]] failed:
failed-save: The save has failed. [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.]