#!/usr/bin/python
# -*- coding: utf-8 -*-
 
# let's declare all global values here
mydir = "./"
pwbdir = mydir + "pywikipedia/"
language = "te"
family = "wikipedia"
mynick = "arjunaraocbot"

# importing modules
import sys
sys.path.append(pwbdir)
from wikipedia import *

import wikipedia, time, catlib, codecs

####################################################################################################
# This function returns the list of articles as a list object
# in given category.  Please give only the Category Name,
# namespace will be addd automatically.
# --function requires both 'wikipedia' and 'catlib' to be imported
def getArticleList(catTitle):
    cat = catlib.Category(wikipedia.getSite(), u'Category:'+catTitle)
    listOfArticles = cat.articlesList()
    return listOfArticles
####################################################################################################


####################################################################################################
# Replace the contents in the page 'pageTitle' with data 'pageData' 
# and add the comment 'comment'
def writeData(pageTitle, pageData, comment):
  page = wikipedia.Page(wikipedia.getSite(), pageTitle)
  try:
    # Load the page's text from the wiki
    data = page.get()
  except wikipedia.NoPage:
    data = u''
  data = pageData
  try:
    page.put(data, comment = comment)
  except wikipedia.EditConflict:
    wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
  except wikipedia.SpamfilterError, url:
    wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), url))
####################################################################################################


####################################################################################################
# Calculates the project statistics for the given input templates and the projects
# and then updates the statistics on to the wikipedia.
def calculateProjectStatistics(ProjectTemplates, ProjectTemplateBase):
  # open all the required files
  logfile = codecs.open('eduemp.ProjectStatistics.log', encoding='utf-8', mode='wb')
  templateFile = open(ProjectTemplates, 'rb' )
  templateBase = open(ProjectTemplateBase, 'rb' )

  #omit 3 characters if it is UTF-8
  #comment the below lines if you are not using notepad to save telugu text
  #templateFile.read(3)
  #templateBase.read(3)

  templateTitle = u"" + unicode(templateBase.readline(), 'utf8')
  templateLine = u""
  newText = u""

  i=0
  sum=0
  extra = 0
  readNext = 1
  grandTotal = 0
  counts = [0, 0, 0, 0, 0]

  for line in templateFile:
      line = unicode(line, 'utf8')
      line = line.replace(u'\n', u'')
      line = line.replace(u'\r', u'')

      catList = line.split('$$')
      articleCount = 0
      for categoryName in catList:
         aList = []
         aList = getArticleList(categoryName)
         articleCount = articleCount + len(aList)

      if i>=30:
         extra = articleCount
         continue
      else:
         counts[i%5] += articleCount

      sum += articleCount
      i = i + 1

      while readNext == 1:
         logfile.write(templateLine)
         newText += templateLine
         templateLine = u"" + unicode(templateBase.readline(), 'utf8')
         if templateLine.find('$$') != -1:
            readNext = 0

      templateLine = templateLine.replace('$$', str(articleCount), 1)

      if i%5 == 0:
         templateLine = templateLine.replace('$$', str(sum), 1)
         sum = 0

      if templateLine.find('$$') == -1:
         readNext = 1

  while readNext == 1:
     logfile.write(templateLine)
     newText += templateLine
     templateLine = u"" + unicode(templateBase.readline(), 'utf8')
     if templateLine.find('$$') != -1:
        readNext = 0

  templateLine = templateLine.replace('$$', str(extra), 1)
  if templateLine.find('$$') == -1:
     readNext = 1

  # write the column sums
  for sum in counts:
     while readNext == 1:
        logfile.write(templateLine)
        newText += templateLine
        templateLine = u"" + unicode(templateBase.readline(), 'utf8')
        if templateLine.find('$$') != -1:
           readNext = 0
   
     templateLine = templateLine.replace('$$', str(sum), 1)
     grandTotal += sum
     if templateLine.find('$$') == -1:
        readNext = 1

  while readNext == 1:
     logfile.write(templateLine)
     newText += templateLine
     templateLine = u"" + unicode(templateBase.readline(), 'utf8')
     if templateLine.find('$$') != -1:
        readNext = 0

  # write the grand total
  templateLine = templateLine.replace('$$', str(grandTotal + extra), 1)

  logfile.write(templateLine)
  newText += templateLine
  for line in templateBase:
     logfile.write(line)
     newText += line

  writeData(templateTitle, newText, u"Bot updating at time: " + time.strftime("[%a, %d %b %Y %H:%M:%S]  ", time.localtime(time.time())))

  templateBase.close()
  templateFile.close()
  logfile.close()
####################################################################################################


####################################################################################################
calculateProjectStatistics('EduEmpTemplates.txt', 'EduEmpTemplateBase.txt')