సభ్యులు:Mpradeepbot/mpc.mandalTemplates.py
వికీపీడియా నుండి
ఈ ప్రోగ్రాముకు సహాయకారిగా ఈ ఫైలును ఉపయోగించండి.
import wikipedia, time, catlib, codecs, config #################################################################################################### # 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.articles() return listOfArticles #################################################################################################### #################################################################################################### # Get the data from the given page name def getData(pageTitle): page = wikipedia.Page(wikipedia.getSite(), pageTitle) try: # Load the page's text from the wiki action = 'normal' title = pageTitle pageData = page.get(get_redirect=True) if page.isRedirectPage() : action = 'redirected' title = page.getRedirectTarget() page = wikipedia.Page(wikipedia.getSite(), title) pageData = page.get() except wikipedia.NoPage: action = 'nopage' title = pageTitle pageData = u'' return pageData, action, title #################################################################################################### #################################################################################################### # 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: page.put(pageData, 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)) #################################################################################################### ############################################################################################## datafile = open('mpc.mandalTemplates.txt', 'rb' ) logfile = codecs.open('mpc.mandalTemplates.log', encoding='utf-8', mode='wb') #omit 3 characters if it is UTF-8 datafile.read(3) #first line is the name of text to be appended at the end of each article line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') comment = line.replace(u'\r', u'') line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') vargaM = line.replace(u'\r', u'') line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') kalapaMDi = line.replace(u'\r', u'') line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') noinclude = line.replace(u'\r', u'') line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') templateComment = line.replace(u'\r', u'') line = unicode(datafile.readline(), 'utf8') line = line.replace(u'\n', u'') disambigText = line.replace(u'\r', u'') templateList = getArticleList(kalapaMDi) for template in templateList: pageData, action, title = getData(template.title()) templateName = template.title()[4:] templateData = pageData pageData = pageData[pageData.find('|list1= '):] pageData = pageData[pageData.find('[[')+2:] logfile.write(u'* processing ' + templateName + u'\r\n') while pageData.find(']]{{') != -1: index = pageData.find(']]{{') villageName = pageData[0:index] pageData = pageData[index+9:] index = villageName.find('|') if index!=-1: villageName = villageName[0:index] villageData, action, title = getData(villageName) if action == 'redirected' and title != villageName: logfile.write(u'** [[' + villageName + ']] is redirected to [[' + title + ']]\r\n') villageName = title if villageData.find(disambigText) != -1: logfile.write(u'** [[' + villageName + ']] is a disambiguation page.\r\n') villageData = u''; if villageData.find('{{' + templateName + '}}') == -1: villageData = villageData.replace(vargaM, '\n{{' + templateName + '}}\n\n'+vargaM, 1) writeData(villageName, villageData, comment) # logfile.write(u'** Adding {{tl|'+templateName + u'}} to [['+ villageName +']] \r\n') else: logfile.write(u'** Skipping [['+ villageName +']] because {{tl|'+templateName + u'}} already exist in that page\r\n') index = pageData.find(']]') villageName = pageData[0:index] pageData = pageData[index+9:] index = villageName.find('|') if index!=-1: villageName = villageName[0:index] villageData, action, title = getData(villageName) if action == 'redirected' and title != villageName: logfile.write(u'** [[' + villageName + ']] is redirected to [[' + title + ']]\r\n') villageName = title if villageData.find('{{' + templateName + '}}') == -1: villageData = villageData.replace(vargaM, '\n{{' + templateName + '}}\n\n'+vargaM, 1) writeData(villageName, villageData, comment) # logfile.write(u'** Adding {{tl|'+templateName + u'}} to [['+ villageName +']] \r\n') else: logfile.write(u'** Skipping [['+ villageName +']] because {{tl|'+templateName + u'}} already exist in that page\r\n') # update the template page by removing the 'kalapaMDi' category templateData = templateData.replace(noinclude, '') writeData(template.title(), templateData, templateComment) logfile.write(u'\r\n\r\n') logfile.close() datafile.close()