Plugins build on top of Supybot to be used with LamperiBot

plugin.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ###
  2. # Copyright (c) 2016, Toni Fadjukoff
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions, and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions, and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the author of this software nor the name of
  14. # contributors to this software may be used to endorse or promote products
  15. # derived from this software without specific prior written consent.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. ###
  29. import socket
  30. socket.setdefaulttimeout(15)
  31. import requests
  32. import json
  33. import supybot.utils as utils
  34. from supybot.commands import *
  35. import supybot.plugins as plugins
  36. import supybot.ircutils as ircutils
  37. import supybot.callbacks as callbacks
  38. try:
  39. from supybot.i18n import PluginInternationalization
  40. _ = PluginInternationalization('Pelit')
  41. except ImportError:
  42. # Placeholder that allows to run the plugin on a bot
  43. # without the i18n module
  44. _ = lambda x: x
  45. class Pelit(callbacks.Plugin):
  46. """Find text from Wiki and highlight people"""
  47. pass
  48. def doLogin(self):
  49. wikiApi = self.registryValue('wikiApi')
  50. httpAuth = self.createHttpAuth()
  51. # Fetch token
  52. tokenParams = self.createTokenParams()
  53. resp = requests.post(wikiApi, auth=httpAuth, params=tokenParams)
  54. data = json.loads(resp.text)
  55. # Do login
  56. token = data["query"]["tokens"]["logintoken"]
  57. loginParams = self.createLoginParams(token)
  58. resp = requests.post(wikiApi, auth=httpAuth, params=loginParams, cookies=resp.cookies)
  59. return resp.cookies
  60. def fetchPageText(self, cookies):
  61. wikiApi = self.registryValue('wikiApi')
  62. pageParams = self.createPageParams()
  63. resp = requests.get(wikiApi, auth=self.createHttpAuth(), params=pageParams, cookies=cookies)
  64. data = json.loads(resp.text)
  65. text = data["parse"]["text"]["*"]
  66. return text
  67. def createHttpAuth(self):
  68. httpBasicAuthUser = self.registryValue('httpBasicAuthUser')
  69. httpBasicAuthPass = self.registryValue('httpBasicAuthPass')
  70. httpAuth = (httpBasicAuthUser, httpBasicAuthPass)
  71. return httpAuth
  72. def createTokenParams(self):
  73. tokenParams = {"action":"query","meta":"tokens","type":"login","format":"json"}
  74. return tokenParams
  75. def createLoginParams(self, token):
  76. wikiLogin = self.registryValue('wikiLogin')
  77. wikiPassword = self.registryValue('wikiPassword')
  78. loginParams = {"action":"login","lgname":wikiLogin,"lgpassword":wikiPassword,"lgtoken":token,"format":"json"}
  79. return loginParams
  80. def createPageParams(self):
  81. wikiPage = self.registryValue('wikiPage')
  82. params = {"action": "parse", "page": wikiPage, "format": "json"}
  83. return params
  84. def processText(self, game, text):
  85. state = False
  86. nicks = []
  87. for line in text.splitlines():
  88. if "mw-headline" in line:
  89. state = game.lower() in line.lower()
  90. elif state:
  91. if line.startswith("</li></ul>"):
  92. break
  93. nick = utils.web.htmlToText(line).strip()
  94. nicks.append(nick)
  95. return nicks
  96. def getPeopleForGame(self, game):
  97. cookies = self.doLogin()
  98. text = self.fetchPageText(cookies)
  99. people = self.processText(game, text)
  100. return people
  101. def peli(self, irc, msg, args, game):
  102. """<peli>
  103. Etsii wikistä pelin ja huutaa kaikille että pelit alkamassa.
  104. """
  105. people = self.getPeopleForGame(game)
  106. if not people:
  107. return irc.reply("Eipä ole sellaista peliä wikissä")
  108. else:
  109. irc.reply(format("pelataanpa peliä %s! %s", game, " ".join(people)), prefixNick=False)
  110. peli = wrap(peli, ['text'])
  111. Class = Pelit
  112. # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: