#!/usr/bin/python
# -*- coding: utf-8  -*-
#Scipt to prepare monthly stub lists

import wikipedia, pagegenerators, catlib, config, codecs, userlib

comment = u'బాటు: జూలై మొలకల జాబితా'
disambig = u"{{అయోమయ నివృత్తి}}"
stublistPageTitle = u"వికీపీడియా:మొలకల జాబితా/2013 జూలై"
village1 = u'మండలంలోని గ్రామాలు}}'
village2 =u'జిల్లా గ్రామాలు]]'
movie = u'{{సినిమా'
firstPageTitle = u'!'

# 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))
  

#opening stublist log and writing header
logstublist = codecs.open('newstublist1.log', encoding='utf-8', mode='wb') 
logstublist.write(u'{{మొలకల జాబితా శీర్షిక}}\r\n') 

## Section for creating new list
##opening stublist wikipage and generating linked page links
namespace = wikipedia.Page(wikipedia.getSite(), firstPageTitle).namespace()
firstPageTitle = wikipedia.Page(wikipedia.getSite(), firstPageTitle).titleWithoutNamespace()
gen = pagegenerators.NewpagesPageGenerator(number=500)
preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber = 500)


for page in preloadingGen:
    try:
      # Load the page's text from the wiki
      pageData = page.get()
      pageCreator = page.getCreator()
      pageCreatorUser = userlib.User(wikipedia.getSite(), pageCreator[0])
      NewUser = False
      if (pageCreatorUser.isAnonymous() == True):
        AnonUser = True
      elif (pageCreatorUser.editCount() <= 25):
        NewUser = True
      else :
        AnonUser = False
        NewUser = False

      if not page.canBeEdited():
         wikipedia.output(u'Skipping locked page %s' % page.title())
         continue
    except wikipedia.NoPage:
       continue
    except wikipedia.IsRedirectPage:
       continue
    # check for disambig template and skip the page if it is disambig page
    if pageData.find(disambig) >= 0: 
       continue
##    if pageData.find(movie) >= 0: 
##       continue
    if pageData.find(village1) >= 0: 
       continue
    if pageData.find(village2) >= 0: 
       continue
      
    # checking the page length and write to log if it is stub
    if len(pageData) < 2048:
      if (AnonUser == True):
        logstublist.write(u'# [[' + page.title() + u']] - \'\'\'[[వాడుకరి:'+pageCreator[0]+ u'|'+pageCreator[0]+u']]\'\'\'\r\n')
      elif (NewUser == True) :
        logstublist.write(u'# [[' + page.title() + u']] - \'\'\'[[వాడుకరి:'+pageCreator[0]+ u'|'+pageCreator[0]+u']] (కొత్త వాడుకరి)\'\'\'\r\n')
      else :
        logstublist.write(u'# [[' + page.title() + u']] - [[వాడుకరి:'+pageCreator[0]+ u'|'+pageCreator[0]+u']]\r\n')
        

logstublist.write(u'\r\n[[వర్గం:వికీపీడియా నిర్వహణ]]\r\n')
# close stublist log handle
logstublist.close()


# upload the stublist to tewiki
logfilestublist = codecs.open('newstublist1.log', encoding='utf-8', mode='rb')
writeData(stublistPageTitle, logfilestublist.read(), comment)
logfilestublist.close()