|
@@ -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:
|