Gebruiker:Edoderoobot/AWL insert

Uit Wikipedia, de vrije encyclopedie
import pywikibot
import os
from os import listdir
from os.path import isfile, join
mypath='./import/'

# IMPORT
#
# #######    $$      $    %%%%%%   ****
#    #      $  $    $ $  %     %  *    *
#    #      $   $  $  $  %     % *      *
#    #      $    $    $  %%%%%%  *      *
#    #     $          $ %         *    *
# #######  $          $ %          ****

site=pywikibot.Site('nl','wikipedia')
repo=site.data_repository()
commit=False
commit=True

def upload(folder,filename):
  pagetitle=filename.replace('.txt','')  
  print(f'Processing: {folder}{filename} as {pagetitle}')  
  page=pywikibot.Page(site,pagetitle)  
  if (len(page.text)>0):
    print('----->bestaat al')    
    return()    
  with open('%s%s'%(folder,filename)) as f:
    lines=f.readlines()    
    for line in lines:
        page.text=page.text+line
  pos=page.text.find('<!--Q')    
  if pos>0:
     end=page.text[pos:].find('-->')   
  if (end-pos<20):
    wd=pywikibot.ItemPage(repo,page.text[pos+4:pos+end]);
    wd.get(get_redirect=True)
    if not('nl' in wd.sitelinks):
      description='nieuwe pagina'
      if 'nl' in wd.descriptions:
        description=wd.descriptions['nl']  
      if (commit):  
        page.put(page.text,summary=description)
        wd.setSitelink(sitelink={'site':'nlwiki','title':pagetitle},summary=f'new page for nl-wiki created')
        os.rename('%s%s'%(folder,filename),'%s%s%s'%(folder,'done/',filename))
        print('Gelukt!')  
      else:
        print(f'Would write {pagetitle} to wd-item {wd.title()}')
        print(page.text)
    else:
      print(f'{pagetitle} bestaat al: {wd.title()}')  
    

onlyfiles=[f for f in listdir(mypath) if isfile(join(mypath,f))]
for file in onlyfiles:
  upload(mypath,file)  
  #print (file)
  

# IMPORT
#
# #######    $$      $    %%%%%%   ****
#    #      $  $    $ $  %     %  *    *
#    #      $   $  $  $  %     % *      *
#    #      $    $    $  %%%%%%  *      *
#    #     $          $ %         *    *
# #######  $          $ %          ****