Browse Source

Add initial Pelit plugin

Toni Fadjukoff 7 years ago
parent
commit
57b2748043
5 changed files with 275 additions and 0 deletions
  1. 1 0
      Pelit/README.md
  2. 68 0
      Pelit/__init__.py
  3. 69 0
      Pelit/config.py
  4. 1 0
      Pelit/local/__init__.py
  5. 136 0
      Pelit/plugin.py

+ 1 - 0
Pelit/README.md View File

@@ -0,0 +1 @@
1
+Find text from Wiki and highlight people

+ 68 - 0
Pelit/__init__.py View File

@@ -0,0 +1,68 @@
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
+###
30
+
31
+"""
32
+Pelit: Find text from Wiki and highlight people
33
+"""
34
+
35
+import supybot
36
+import supybot.world as world
37
+
38
+# Use this for the version of this plugin.  You may wish to put a CVS keyword
39
+# in here if you're keeping the plugin in CVS or some similar system.
40
+__version__ = ""
41
+
42
+# XXX Replace this with an appropriate author or supybot.Author instance.
43
+__author__ = supybot.authors.unknown
44
+
45
+# This is a dictionary mapping supybot.Author instances to lists of
46
+# contributions.
47
+__contributors__ = {}
48
+
49
+# This is a url where the most recent plugin package can be downloaded.
50
+__url__ = ''
51
+
52
+from . import config
53
+from . import plugin
54
+from imp import reload
55
+# In case we're being reloaded.
56
+reload(config)
57
+reload(plugin)
58
+# Add more reloads here if you add third-party modules and want them to be
59
+# reloaded when this plugin is reloaded.  Don't forget to import them as well!
60
+
61
+if world.testing:
62
+    from . import test
63
+
64
+Class = plugin.Class
65
+configure = config.configure
66
+
67
+
68
+# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

+ 69 - 0
Pelit/config.py View File

@@ -0,0 +1,69 @@
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
+###
30
+
31
+import supybot.conf as conf
32
+import supybot.registry as registry
33
+try:
34
+    from supybot.i18n import PluginInternationalization
35
+    _ = PluginInternationalization('Pelit')
36
+except:
37
+    # Placeholder that allows to run the plugin on a bot
38
+    # without the i18n module
39
+    _ = lambda x: x
40
+
41
+
42
+def configure(advanced):
43
+    # This will be called by supybot to configure this module.  advanced is
44
+    # a bool that specifies whether the user identified themself as an advanced
45
+    # user or not.  You should effect your configuration by manipulating the
46
+    # registry as appropriate.
47
+    from supybot.questions import expect, anything, something, yn
48
+    conf.registerPlugin('Pelit', True)
49
+
50
+
51
+Pelit = conf.registerPlugin('Pelit')
52
+# This is where your configuration variables (if any) should go.  For example:
53
+# conf.registerGlobalValue(Pelit, 'someConfigVariableName',
54
+#     registry.Boolean(False, _("""Help for someConfigVariableName.""")))
55
+conf.registerGlobalValue(Pelit, 'wikiLogin',
56
+    registry.String('', _("The username for wiki")))
57
+conf.registerGlobalValue(Pelit, 'wikiPassword',
58
+    registry.String('', _("The password for wiki")))
59
+conf.registerGlobalValue(Pelit, 'wikiApi',
60
+    registry.String('', _("The URL for mediawiki api.php")))
61
+conf.registerGlobalValue(Pelit, 'wikiPage',
62
+    registry.String('', _("The path to mediawiki page to scrap")))
63
+conf.registerGlobalValue(Pelit, 'httpBasicAuthUser',
64
+    registry.String('', _("HTTP Basic Authentication username")))
65
+conf.registerGlobalValue(Pelit, 'httpBasicAuthPass',
66
+    registry.String('', _("HTTP Basic Authentication password")))
67
+
68
+
69
+# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

+ 1 - 0
Pelit/local/__init__.py View File

@@ -0,0 +1 @@
1
+# Stub so local is a module, used for third-party modules

+ 136 - 0
Pelit/plugin.py View File

@@ -0,0 +1,136 @@
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
+###
30
+
31
+import socket
32
+socket.setdefaulttimeout(15)
33
+
34
+import requests
35
+import json
36
+
37
+import supybot.utils as utils
38
+from supybot.commands import *
39
+import supybot.plugins as plugins
40
+import supybot.ircutils as ircutils
41
+import supybot.callbacks as callbacks
42
+try:
43
+    from supybot.i18n import PluginInternationalization
44
+    _ = PluginInternationalization('Pelit')
45
+except ImportError:
46
+    # Placeholder that allows to run the plugin on a bot
47
+    # without the i18n module
48
+    _ = lambda x: x
49
+
50
+
51
+class Pelit(callbacks.Plugin):
52
+    """Find text from Wiki and highlight people"""
53
+    pass
54
+
55
+    def doLogin(self):
56
+        wikiApi = self.registryValue('wikiApi')
57
+        httpAuth = self.createHttpAuth()
58
+
59
+        # Fetch token
60
+        tokenParams = self.createTokenParams()
61
+        resp = requests.post(wikiApi, auth=httpAuth, params=tokenParams)
62
+        data = json.loads(resp.text)
63
+        
64
+        # Do login
65
+        token = data["query"]["tokens"]["logintoken"]
66
+        loginParams = self.createLoginParams(token)
67
+        resp = requests.post(wikiApi, auth=httpAuth, params=loginParams, cookies=resp.cookies)
68
+        return resp.cookies
69
+
70
+    def fetchPageText(self, cookies):
71
+        wikiApi = self.registryValue('wikiApi')
72
+        pageParams = self.createPageParams()
73
+        resp = requests.get(wikiApi, auth=self.createHttpAuth(), params=pageParams, cookies=cookies)
74
+        data = json.loads(resp.text)
75
+        text = data["parse"]["text"]["*"]
76
+        return text
77
+
78
+    def createHttpAuth(self):
79
+        httpBasicAuthUser = self.registryValue('httpBasicAuthUser')
80
+        httpBasicAuthPass = self.registryValue('httpBasicAuthPass')
81
+        httpAuth = (httpBasicAuthUser, httpBasicAuthPass)
82
+        return httpAuth
83
+
84
+    def createTokenParams(self):
85
+        tokenParams = {"action":"query","meta":"tokens","type":"login","format":"json"}
86
+        return tokenParams
87
+
88
+    def createLoginParams(self, token):
89
+        wikiLogin = self.registryValue('wikiLogin')
90
+        wikiPassword = self.registryValue('wikiPassword')
91
+        loginParams = {"action":"login","lgname":wikiLogin,"lgpassword":wikiPassword,"lgtoken":token,"format":"json"}
92
+        return loginParams
93
+
94
+    def createPageParams(self):
95
+        wikiPage = self.registryValue('wikiPage')
96
+        params = {"action": "parse", "page": wikiPage, "format": "json"}
97
+        return params 
98
+
99
+    def processText(self, game, text):
100
+        state = False
101
+        nicks = []
102
+        for line in text.splitlines():
103
+            if "mw-headline" in line:
104
+                state = game.lower() in line.lower()
105
+            elif state:
106
+                if line.startswith("</li></ul>"):
107
+                    break
108
+                nick = utils.web.htmlToText(line).strip()
109
+                nicks.append(nick)
110
+        return nicks
111
+
112
+    def getPeopleForGame(self, game):
113
+
114
+        cookies = self.doLogin()
115
+        text = self.fetchPageText(cookies)
116
+        people = self.processText(game, text)
117
+
118
+        return people
119
+
120
+    def peli(self, irc, msg, args, game):
121
+        """<peli>
122
+
123
+        Etsii wikistä pelin ja huutaa kaikille että pelit alkamassa.
124
+        """
125
+        people = self.getPeopleForGame(game)
126
+        if not people:
127
+            return irc.reply("Eipä ole sellaista peliä wikissä")
128
+        else:
129
+            irc.reply(format("pelataanpa peliä %s! %s", game, " ".join(people)), prefixNick=False)
130
+    peli = wrap(peli, ['text'])
131
+
132
+
133
+Class = Pelit
134
+
135
+
136
+# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: