### # Copyright (c) 2016, Toni Fadjukoff # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions, and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions, and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author of this software nor the name of # contributors to this software may be used to endorse or promote products # derived from this software without specific prior written consent. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. ### import socket socket.setdefaulttimeout(15) import requests import json import supybot.utils as utils from supybot.commands import * import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.callbacks as callbacks try: from supybot.i18n import PluginInternationalization _ = PluginInternationalization('Pelit') except ImportError: # Placeholder that allows to run the plugin on a bot # without the i18n module _ = lambda x: x class Pelit(callbacks.Plugin): """Find text from Wiki and highlight people""" pass def doLogin(self): wikiApi = self.registryValue('wikiApi') httpAuth = self.createHttpAuth() # Fetch token tokenParams = self.createTokenParams() resp = requests.post(wikiApi, auth=httpAuth, params=tokenParams) data = json.loads(resp.text) # Do login token = data["query"]["tokens"]["logintoken"] loginParams = self.createLoginParams(token) resp = requests.post(wikiApi, auth=httpAuth, params=loginParams, cookies=resp.cookies) return resp.cookies def fetchPageText(self, cookies): wikiApi = self.registryValue('wikiApi') pageParams = self.createPageParams() resp = requests.get(wikiApi, auth=self.createHttpAuth(), params=pageParams, cookies=cookies) data = json.loads(resp.text) text = data["parse"]["text"]["*"] return text def createHttpAuth(self): httpBasicAuthUser = self.registryValue('httpBasicAuthUser') httpBasicAuthPass = self.registryValue('httpBasicAuthPass') httpAuth = (httpBasicAuthUser, httpBasicAuthPass) return httpAuth def createTokenParams(self): tokenParams = {"action":"query","meta":"tokens","type":"login","format":"json"} return tokenParams def createLoginParams(self, token): wikiLogin = self.registryValue('wikiLogin') wikiPassword = self.registryValue('wikiPassword') loginParams = {"action":"login","lgname":wikiLogin,"lgpassword":wikiPassword,"lgtoken":token,"format":"json"} return loginParams def createPageParams(self): wikiPage = self.registryValue('wikiPage') params = {"action": "parse", "page": wikiPage, "format": "json"} return params def processText(self, game, text): state = False nicks = [] for line in text.splitlines(): if "mw-headline" in line: state = game.lower() in line.lower() elif state: if line.startswith(""): break nick = utils.web.htmlToText(line).strip() nicks.append(nick) return nicks def getPeopleForGame(self, game): cookies = self.doLogin() text = self.fetchPageText(cookies) people = self.processText(game, text) return people def peli(self, irc, msg, args, game): """ Etsii wikistä pelin ja huutaa kaikille että pelit alkamassa. """ people = self.getPeopleForGame(game) if not people: return irc.reply("Eipä ole sellaista peliä wikissä") else: irc.reply(format("pelataanpa peliä %s! %s", game, " ".join(people)), prefixNick=False) peli = wrap(peli, ['text']) Class = Pelit # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: