import pywikibot                            # Tell python that we will be using pywikibot
site=pywikibot.Site('nl', 'wikipedia')       # Identify the site we will use
pagename = 'User talk:%s' % site.username() # Testing, so use your bots own talk page
page = pywikibot.Page(site, pagename)       # Retrieve the current page
text = page.text                            # Retrieve the current text, which we'll change
text += '\n== Happy hacking ==\n'           # Add text. \n tells to add a newline to the text, so the content will be on different lines 
                                            # NB: in Wikicode, doesn't mean it will be so in the shown content.
text += 'Let\'s do some Python exercise. ~~~~\n'
page.text = text                            # update the text in the page object in memory
page.save ('I\'m trying to use Python and Pywikibot')  #save everything to the page
Sleeping for 19.2 seconds, 2018-10-26 10:01:27
Page [[nl:Overleg gebruiker:Elvalente]] saved
sudo pip install mwviews


from datetime import date,datetime,timedelta
#import datetime
import pywikibot
from mwviews.api import PageviewsClient

def hasreferences(articletitle):
  site=pywikibot.Site()
  print('Artikel: [%s]' % (articletitle))
  if (articletitle.find(':')>0):
    return('')
  page=pywikibot.Page(site, articletitle);
  if (page.text.lower().find('<ref>') > 0):
    return(u'√')
  return('')
    
  

def yesterdaysrank(listofranks, article, rankyesterday):
  thisrank = 0
  for onerank in listofranks:
    thisrank += 1
    if article==onerank['article']: 
      if thisrank>rankyesterday:
        return str(thisrank)+u'       \u2191'+str(thisrank-rankyesterday)
      elif thisrank<rankyesterday:
        return str(thisrank)+u'       \u2193'+str(rankyesterday-thisrank)  
      else:
        return (u'nieuw')
  return str(thisrank)+u'='
  
  
maxlst = 100 #a short list for now
nrdays=7 #1 week for now
topxxlist = []
debugmodus=True
debugmodus=False

endday = date.today()-timedelta(days=1)
startday = endday - timedelta(days=nrdays)
yesterday = endday-timedelta(days=1)  #to calculate position of day before
mywiki = u'nl.wikipedia' 

p = PageviewsClient() #initialize

todaystoplist = p.top_articles(mywiki, limit=maxlst)  #get the current topxx of articles
yesterdaystoplist = p.top_articles(mywiki, limit=2*maxlst, year=yesterday.year, month=yesterday.month, day=yesterday.day)
for articles in todaystoplist:
   if debugmodus:
     print (articles['article'])
     probe=p.article_views(mywiki,[articles['article']],start=startday,end=endday)
   topxxlist.append(articles['article'])
topxxtable = p.article_views(mywiki,topxxlist,start=startday,end=endday)

wikistr = (u'Onderstaande tabel geeft de statistieken van de meest bezochte artikelen op de Nederlandstalige Wikipedia van %s. \n\n{| class=\"wikitable sortable\"\n|-\n! Positie !! Gisteren !! Artikel !! A !! Aantal views op ') % (str(endday))

dayloop=endday
while dayloop>=startday:
  wikistr += str(dayloop)+'!!' 
  dayloop -= timedelta(days=1) 
wikistr = wikistr[:len(wikistr)-2] + '\n'  
rank = 0
for article in topxxlist:
   rank += 1
   rankyesterday = yesterdaysrank(yesterdaystoplist,article,rank)
   prnarticle = article.replace('Bestand',':Bestand',1)
   prnarticle = prnarticle.replace('Categorie',':Categorie',1)
   prnarticle = prnarticle.replace('Sjabloon',':Sjabloon',1)
   
   linestr = u'|-\n|%i\n|%s\n|[[%s]]\n|%s\n' % (rank,rankyesterday,prnarticle.replace('_',' '),hasreferences(article))
   dayloop=endday
   while (dayloop>=startday):
      indexdate = datetime.combine(dayloop,datetime.min.time()) #make date in right format
      hitsoneday = topxxtable[indexdate][article]
      if hitsoneday is None: hitsoneday = 0
      linestr += '|%s\n' % '{0:6d}'.format(hitsoneday)
      dayloop -= timedelta(days=1) 
   wikistr += linestr   
wikistr += "\n|}\n"   

if not debugmodus:
      site = pywikibot.getSite(u'nl')
      yesterday=date.today()-timedelta(days=1)
      
      pywikibot.Page(site, u'User:Edoderoo/nl-stats-gisteren/%4d%02d%02d'%(yesterday.year,yesterday.month,yesterday.day)).put(wikistr, comment=u'Update stats. Source on https://goo .gl/tKMxHv') #Save page
else:
  print("%s" % wikistr)
  File "<ipython-input-2-406f084ec65c>", line 1
    sudo pip install mwviews
           ^
SyntaxError: invalid syntax