Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Created November 13, 2010 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junaidpv/675404 to your computer and use it in GitHub Desktop.
Save junaidpv/675404 to your computer and use it in GitHub Desktop.
ഒരു വർഗ്ഗത്തിൽ ഉൾപ്പെട്ട താളുകളിൽ നിന്ന് മറ്റൊരു വർഗ്ഗം നീക്കം ചെയ്യാനുള്ള പൈവിക്കി സ്ക്രിപ്റ്റ്
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ഒരു പ്രതേക വർഗ്ഗത്തിൽ ഉൾപ്പെട്ട താളുകളിൽ നിന്ന് മറ്റൊരു വർഗ്ഗം നീക്കം ചെയ്യാനുള്ള പൈവിക്കി ബോട്ട് പ്രോഗ്രാം.
Author: Junaid P V ([[user:Junaidpv]])
Date: 2010-11-13
No warranty
"""
import wikipedia
import catlib
import unicodedata
import codecs
import re
CAT = ur'വർഗ്ഗം'
# This is function was copied from pywikipedia framework,
# and altered little to handle new category alias.
def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None):
"""Replace the category oldcat with the category newcat and return
the modified text.
"""
if site is None:
site = wikipedia.getSite()
catNamespace = '|'.join(site.category_namespaces()+ (ur"വർഗ്ഗം", ))
title = oldcat.titleWithoutNamespace()
if not title:
return
# title might contain regex special characters
title = re.escape(title)
# title might not be capitalized correctly on the wiki
if title[0].isalpha() and not site.nocapitalize:
title = "[%s%s]" % (title[0].upper(), title[0].lower()) + title[1:]
# spaces and underscores in page titles are interchangeable, and collapsible
title = title.replace(r"\ ", "[ _]+").replace(r"\_", "[ _]+")
categoryR = re.compile(r'\[\[\s*(%s)\s*:\s*%s\s*((?:\|[^]]+)?\]\])'
% (catNamespace, title), re.I)
if newcat is None:
text = wikipedia.replaceExcept(oldtext, categoryR, '',
['nowiki', 'comment', 'math', 'pre', 'source'])
else:
text = wikipedia.replaceExcept(oldtext, categoryR,
'[[%s:%s\\2' % (site.namespace(14),
newcat.titleWithoutNamespace()),
['nowiki', 'comment', 'math', 'pre', 'source'])
return text
siteFamily = 'wikipedia'
siteLangCode = 'ml'
# വർഗ്ഗത്തിന്റെ പേര് ഇവിടെ നൽകുക
#------------------------------------------------
# താളുകൾ തിരയേണ്ട വർഗ്ഗം
catName = ur'ആലപ്പുഴ ജില്ലയിലെ ഗ്രാമപഞ്ചായത്തുകൾ'
# നീക്കം ചെയ്യേണ്ട വർഗ്ഗം
catToRemove = ur'നീക്കേണ്ട വർഗ്ഗം'
#------------------------------------------------
wikiSite = wikipedia.Site(code=siteLangCode, fam=siteFamily)
log = codecs.open('logs/pages-in-cat.log', mode='at', encoding = 'utf-8')
catPage = catlib.Category(site=wikiSite, title=CAT+':'+catName)
catRemovePage = catlib.Category(site=wikiSite, title=CAT+':'+catToRemove)
if not catPage.exists():
log.write("Given category does not exist.\n")
log.flush()
wikipedia.output("Given category does not exist")
wikipedia.stopme()
pages = catPage.articlesList()
for page in pages:
title = page.title()
wikipedia.output(title+'\n')
page.put(replaceCategoryInPlace(page.get(), catRemovePage, None, wikiSite), comment=ur'വർഗ്ഗം ഒഴിവാക്കുന്നു')
log.flush()
log.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment