వాడుకరి:RahmanuddinBot/stublistprepare.py

వికీపీడియా నుండి
Jump to navigation Jump to search



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

import wikipedia, pagegenerators, catlib, config, codecs, userlib
from datetime import date

mt = date.today().month
yr = date.today().year

month_te = {'1': u'జనవరి','2': u'ఫిబ్రవరి','3': u'మార్చ్','4': u'ఏప్రిల్','5': u'మే','6': u'జూన్','7': u'జూలై','8': u'ఆగస్టు','9': u'సెప్టెంబర్','10': u'అక్టోబర్','11': u'నవంబర్','12': u'డిసెంబర్'}
comment = u'బాటు: జూలై నెల మొలకల జాబితా'
disambig = u"{{అయోమయ నివృత్తి}}"
stublistPageTitle = u"వికీపీడియా:మొలకల జాబితా/{} {}".format(str(yr),month_te[str(mt)])
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')
logstublist.write(u"""{| class="wikitable"
|-
! వ్యాసం !! వాడుకరి !! బైట్లలో ప్రస్తుత నిడివి
|-\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
      AnonUser = False
      if (pageCreatorUser.isAnonymous() == True):
        AnonUser = True
      elif (pageCreatorUser.editCount() <= 25):
        NewUser = True
      else :
        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
    len_pg = len(pageData)
    # checking the page length and write to log if it is stub
    if len_pg < 2048:
      if (AnonUser == True):
        logstublist.write(u'| [[' + page.title() + u']] || \'\'\'[[User:'+pageCreator[0]+ u'|'+pageCreator[0]+u']]\'\'\' || '+ str(len_pg)+u' bytes\r\n|-\n')
      elif (NewUser == True) :
        logstublist.write(u'| [[' + page.title() + u']] || \'\'\'[[User:'+pageCreator[0]+ u'|'+pageCreator[0]+u']] (New User)\'\'\' || '+ str(len_pg)+u' bytes\r\n|-\n')
      else :
        logstublist.write(u'| [[' + page.title() + u']] || [[User:'+pageCreator[0]+ u'|'+pageCreator[0]+u']] || ' + str(len_pg)+u' bytes \r\n|-\n')
        
logstublist.write(u'\n|}\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()