వాడుకరి:Vyzbot/Villagepages.py

వికీపీడియా నుండి
Jump to navigation Jump to search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""this is Vyzbot.
"""
import wikipedia, pagegenerators, catlib
import login, re

#login
wikisite = wikipedia.Site('te', 'wikipedia')
mysite = wikipedia.Site('te')
login.LoginManager('xxxxx', False, mysite) #replace xxxxx with your bot login's password

#variables
global district_name, mandalname, completed_mandals
starttext=u'begin '
endtext=u'{{end}}'
include = False # if True includes starttext and endtext in pages
force=False # if True overwrites existing pages
#append = False # specifies to skip existing pages
append = "Bottom" # appends the new text at the bottom of an existing page
titlestart = "'''" # page title designation
titleend = "''',"

district_name = u"శ్రీకాకుళం"
completed_mandals = 0 # set it to the number of mandal from where it should start  (0- from the first mandal)

#edit summary messages
commenttext = u'వైజ్‌బాట్: గ్రామ ప్రాథమిక సమాచారంతో యాంత్రిక పేజీ సృష్టి'
msg= u'Automated import of articles'
msg_top= u'పేజీ పైభాగంలో అతికిస్తుంది' # The following messages are added to topic when the page already exists
msg_bottom= u'పేజీ క్రింది భాగంలో అతికిస్తుంది'
msg_force= u'ఇప్పుడున్న పాఠ్యాన్ని తుడిచేసి రాస్తుంది'


def getmandals():

    district_cat = u"వర్గం:" + district_name + u" జిల్లా మండలాలు"
    site = wikipedia.getSite()
    cat = catlib.Category(site, district_cat)
    gen = pagegenerators.CategorizedPageGenerator(cat)
    mandalcounter=1
    for page in gen:
        if mandalcounter > completed_mandals: 
            mandalname = page.title()
            text = []
            text=getvillages(mandalname)
            print text
            findpage(text)
        mandalcounter+=1


def getvillages(mandalname):
    wikipage = wikipedia.Page(wikisite,mandalname)
    mpagetext = wikipage.get()
    
    villagetext=mpagetext.split(u'==మండలంలోని గ్రామాలు==')[1].split(u'{{')[0]

    lines = unicode(villagetext).split(u'\n') #splitting filestring into lines
    while lines and not lines[0]:  del lines[0] #removing empty lines on top
    while lines and not lines[-1]:  del lines[-1]  #removing empty lines at the bottom
    lines = [line.split('[[')[-1].split(']]')[0] for line in lines]  #extracting villagename from sq.brackets
    
    #not creating village page for mandalname and removing (mandal) from names
    for counter,line in enumerate(lines):
        if "(" in line: lines[counter] = lines[counter].split('(')[0]
        if line == mandalname : del lines[counter]

    lines = [(u"begin '''"+line+u"''', [["+district_name+u"]] జిల్లా, [["+mandalname+u"]] మండలానికి చెందిన గ్రామము.\n[[వర్గం:"+district_name+u" జిల్లా గ్రామాలు]]\n{{end}} ") for line in lines]
    villages = ''.join(lines)

    return villages


def findpage(t):
    search_string = titlestart + "(.*?)" + titleend
    try:
        location = re.search(starttext+"([^\Z]*?)"+endtext,t)
        if include:
            contents = location.group()
        else:
            contents = location.group(1)
    except AttributeError:
        print 'Start or end marker not found.'
        return
    try:
        title = re.search(search_string, contents).group(1)
    except AttributeError:
        wikipedia.output(u"No title found - skipping a page.")
        return
    else:
        page = wikipedia.Page(mysite, title)
        wikipedia.output(page.title())
        if page.exists():
     
            if append == "Top":
                old_text = page.get(get_redirect=True)
                contents += old_text
                commenttext_top = commenttext + " - " + msg_top
                wikipedia.output(u"Page %s already exists, appending on top!"%title)
                page.put(contents, comment = commenttext_top, minorEdit = False)
            elif append == "Bottom":
                old_text = page.get(get_redirect=True)
                contents = old_text + "\n" + contents + u"{{తనిఖీ}}"
                commenttext_bottom = commenttext + " - " + msg_bottom
                wikipedia.output(u"Page %s already exists, appending on bottom!"%title)
                page.put(contents, comment = commenttext_bottom, minorEdit = False)
            elif force:
                commenttext_force = commenttext + " *** " + msg_force + " ***"
                wikipedia.output(u"Page %s already exists, ***overwriting!"%title)
                page.put(contents, comment = commenttext_force, minorEdit = False)
            else:
                wikipedia.output(u"Page %s already exists, not adding!"%title)
        else:
            page.put(contents, comment = commenttext, minorEdit = False)
    findpage(t[location.end()+1:])
    return

def main():
    getmandals()

    
try:
    main()
except:
    wikipedia.stopme()
    raise
else:
    wikipedia.stopme()