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)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-a12d18299b5c> in <module>()
      2 #import datetime
      3 import pywikibot
----> 4 from mwviews.api import PageviewsClient
      5 
      6 def hasreferences(articletitle):

ModuleNotFoundError: No module named 'mwviews'
import 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)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-0ea9880ed1e5> in <module>()
----> 1 import mwviews
      2 from datetime import date,datetime,timedelta
      3 #import datetime
      4 import pywikibot
      5 from mwviews.api import PageviewsClient

ModuleNotFoundError: No module named 'mwviews'