User:MewBot/mewbot.py

#!/usr/bin/env python
#coding: utf-8

# Copyright CodeCat 2010 - 2013

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import wikipedia, re, string, sys
from caverbformbot import *
from eoverbformbot import *
from fiverbformbot import *
from nladjformbot import *
from nlverbformbot import *

class MewBotBase:
	"""A wrapper class that takes care of functionality specific to MewBot.
	If you want to use this for your own bot, you'll need to make some small
	adjustments to the run method. Everything else in this file can be kept
	as-is."""
	
	def __init__(self, list, simulation, force, verbose):
		self._list = list
		self._simulation = simulation
		self._force = force
		self._verbose = verbose
	
	
	def run(self):
		pass
		
	
	def getList(self, pageName, sectionName):
		"""Get the list of entries given at a pre-specified page."""
		
		page = wikipedia.Page(wikipedia.getSite('en', 'wiktionary'), pageName)
		
		if not page.exists():
			wikipedia.output(u"Oh noes! Can't find list page!")
			return []
		
		contents = page.get()
		sections = getSections(contents, sectionName, 3, False)
		
		if not sections:
			wikipedia.output(u"Can't find the {0} section on the list page!".format(sectionName))
			return []
		
		contents = string.split(string.strip(contents[sections[0][0]:sections[0][1]]), '\n')
		list = []
		
		for line in contents:
			match = re.search(ur'# *\[\[:?(.+)\]\]', line, re.UNICODE)
			
			# We're done
			if not match:
				break
			
			list.append(match.group(1))
		
		return list

class MewBotCAVerbs(MewBotBase):
	
	def run(self):
		if not self._list:
			self._list = self.getList(u'User:MewBot/feedme', u'Catalan verbs')
		
		for entry in self._list:
			bot = CAVerbFormBot(entry, 'Category:Requests for cleanup (MewBot)', self._simulation, self._force, self._verbose)
			bot.run()


class MewBotEOVerbs(MewBotBase):
	
	def run(self):
		if not self._list:
			self._list = self.getList(u'User:MewBot/feedme', u'Esperanto verbs')
		
		for entry in self._list:
			bot = EOVerbFormBot(entry, 'Category:Requests for cleanup (MewBot)', self._simulation, self._force, self._verbose)
			bot.run()

			
class MewBotFIVerbs(MewBotBase):
	
	def run(self):
		if not self._list:
			self._list = self.getList(u'User:MewBot/feedme', u'Finnish verbs')
		
		for entry in self._list:
			bot = FIVerbFormBot(entry, 'Category:Requests for cleanup (MewBot)', self._simulation, self._force, self._verbose)
			bot.run()


class MewBotNLAdjectives(MewBotBase):
	
	def run(self):
		if not self._list:
			self._list = self.getList(u'User:MewBot/feedme', u'Dutch adjectives')
		
		for entry in self._list:
			bot = NLAdjectiveFormBot(entry, 'Category:Requests for cleanup (MewBot)', self._simulation, self._force, self._verbose)
			bot.run()


class MewBotNLVerbs(MewBotBase):
	
	def run(self):
		if not self._list:
			self._list = self.getList(u'User:MewBot/feedme', u'Dutch verbs')
		
		for entry in self._list:
			bot = NLVerbFormBot(entry, 'Category:Requests for cleanup (MewBot)', self._simulation, self._force, self._verbose)
			bot.run()


def main():
	list = []
	
	simulation = False
	force = False
	verbose = False
	type = False
	
	for param in sys.argv[1:]:
		if param[0] == '-':
			options = param[1:]
			
			for opt in options:
				if opt == 'f':
					force = True
				elif opt == 's':
					simulation = True
				elif opt == 'v':
					verbose = True
				elif opt == 't':
					type = True
				else:
					wikipedia.output("Unknown option: {0}".format(opt))
					return
		elif type == True:
			type = param
			
			if type not in ['cav', 'eov', 'fiv', 'nla', 'nlv']:
				wikipedia.output("Unknown type: {0}".format(type))
				return
		else:
			try:
				param = unicode(param, 'utf-8')
			except UnicodeDecodeError:
				param = unicode(param, 'iso8859-1')
			list.append(param)
	
	if not type or type == 'cav':
		bot = MewBotCAVerbs(list, simulation, force, verbose)
		bot.run()
	
	if not type or type == 'eov':
		bot = MewBotEOVerbs(list, simulation, force, verbose)
		bot.run()
	
	if not type or type == 'fiv':
		bot = MewBotFIVerbs(list, simulation, force, verbose)
		bot.run()
	
	if not type or type == 'nla':
		bot = MewBotNLAdjectives(list, simulation, force, verbose)
		bot.run()
	
	if not type or type == 'nlv':
		bot = MewBotNLVerbs(list, simulation, force, verbose)
		bot.run()


if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()