This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
233 lines (199 loc) · 8.83 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
###
# Copyright (c) 2020, Brian McCord
# 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 requests
import re
from supybot import utils, plugins, ircutils, callbacks
from supybot.commands import *
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('BeestLex')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x: x
pink = "\x0313"
green = "\x0303"
nulattr = "\x0F"
bullet = " \x0303•\x0f "
class BeestLex(callbacks.Plugin):
"""IRC-legible dictionary"""
pass
def ety(self, irc, msg, args, input_word):
"""[<input>]
Get etymologies for <input>.
"""
mw_api = self.registryValue("MWKey")
dict_url = "https://www.dictionaryapi.com/api/v3/references/collegiate/json/"
payload = {'key': mw_api}
dict_d = (requests.get(dict_url + input_word, params=payload)).json()
try:
ety_base = dict_d[0]['et'][0][1]
except KeyError:
irc.reply("I can't find etymology for " + pink +
input_word + nulattr + ".")
return
ety_str = (ety_base.strip().replace('{it}', '\x0303\x1D')
.replace('{/it}', '\x0F')
.replace('{ma}{mat|', '(see \x0303')
.replace('|}{/ma}', '\x0F)'))
regex = r"{et_link\|(.*?):*\d*\|.*?}"
subst = "🐻\\1🐶"
ety_str = re.sub(regex, subst, ety_str, 0)
ety_str = ety_str.replace('🐻', '\x0303').replace('🐶', '\x0F')
hword = (pink + '\x02' + dict_d[0]['hwi']['hw'].replace("*", "")
+ nulattr)
reply_str = (green + "▶" + hword + ' ' + ety_str)
irc.reply(reply_str, prefixNick=False)
ety = wrap(ety, ['text'])
def syn(self, irc, msg, args, input_word):
"""[<input>]
Get English synonyms for <input>.
"""
if input_word == "":
irc.error('Received null input')
return
mw_api = self.registryValue("MWThesKey")
syn_url = "https://www.dictionaryapi.com/api/v3/references/thesaurus/json/"
payload = {'key': mw_api}
syn_d = (requests.get(syn_url + input_word, params=payload)).json()
try:
syn_test = syn_d[0]['meta']['syns']
except TypeError:
syn_cog = syn_d[0]
syn_d = (requests.get(syn_url + syn_cog, params=payload)).json()
except IndexError:
irc.reply("I can't find synonyms for " + pink +
input_word + nulattr + ".")
return
def syn_bld(in_word):
syn_out = ''
for idx in range(0, 10):
try:
hword = (pink + '\x02' + syn_d[idx]['hwi']['hw'] + nulattr)
syn_def = syn_d[idx]['shortdef'][0]
syn_lst = syn_d[idx]['meta']['syns'][0]
syn_lst = ', '.join(syn_lst)
syn_out = (syn_out + green + '▶' + hword + green +
" \x1D" + syn_def + " " + nulattr + syn_lst + " ")
except IndexError:
pass
return syn_out
syn_print = syn_bld(syn_d)
irc.reply(syn_print, prefixNick=False)
syn = wrap(syn, ['text'])
def lex(self, irc, msg, args, input_word):
"""[<input>]
Get English definitions for <input>.
"""
if input_word == "":
irc.error('Received null input')
return
mw_api = self.registryValue("MWKey")
dict_url = "https://www.dictionaryapi.com/api/v3/references/collegiate/json/"
payload = {'key': mw_api}
dict_d = (requests.get(dict_url + input_word, params=payload)).json()
reply_build = ''
# automatic redirection
try:
cognate = dict_d[0]['cxs'][0]
reply_build = (pink + (dict_d[0]['hwi']['hw']).replace("*", "") +
nulattr + ": \x1D" + cognate['cxl'] + nulattr + " ")
dict_d = (requests.get(dict_url + cognate['cxtis'][0]['cxt'],
params=payload)).json()
except (KeyError, IndexError, TypeError):
pass
# okay we'll try to get some definitions
try:
for i in range(0, 20):
headword = (pink + "\x02" +
(dict_d[i]['hwi']['hw']).replace("*", "") + nulattr)
homo_def = (dict_d[i]['shortdef'])
# main entry, status labels
homo_sls1 = homo_sls2 = ''
try:
homo_sls1 = ", " + (dict_d[i]['def'][0]['sseq'][0][0][1]
['sls'][0])
homo_sls2 = ", " + (dict_d[i]['def'][0]['sseq'][0][0][1]
['sls'][1])
except (KeyError, TypeError, IndexError):
pass
func_lab = (green + " \x1D" + str(dict_d[i]['fl']) +
homo_sls1 + homo_sls2 + nulattr + green + " " +
str(i + 1))
# B status labels
sls_b1 = sls_b2 = ''
try:
sls_b1 = "\x1D " + (dict_d[i]['def'][0]['sseq'][1][0][1]
['sls'][0])
sls_b2 = ", " + (dict_d[i]['def'][0]['sseq'][1][0][1]
['sls'][1])
except:
pass
# C status labels
sls_c1 = sls_c2 = ''
try:
sls_c1 = "\x1D " + (dict_d[i]['def'][0]['sseq'][2][0][1]
['sls'][0])
sls_c2 = ", " + (dict_d[i]['def'][0]['sseq'][2][0][1]
['sls'][1])
except:
pass
# build an entry set
def_1 = def_2 = def_3 = ''
try:
def_1 = ": " + nulattr + homo_def[0]
def_2 = ("; " + green + str(i + 1) + "b:" + sls_b1 +
sls_b2 + " " + nulattr + homo_def[1])
def_3 = ("; " + green + str(i + 1) + "c:" + sls_c1 +
sls_c2 + " " + nulattr + homo_def[2])
except IndexError:
pass
reply_build = (reply_build + green + "▶" + headword +
func_lab + def_1 + def_2 + def_3 + " ")
except (KeyError, IndexError):
pass
# halp cannot speel, maek suggest
except TypeError:
irc.reply(green + 'Did you mean: ' + pink +
((str(dict_d)).translate
(str.maketrans({',': '', '[': '', ']': ''}))))
return
# can't do anything with silly user's input
if reply_build == '':
irc.reply("I can't find a word or suggestion for " + pink +
input_word + nulattr + ".")
# if there's a definition, show it
else:
regex = r"\{.+?\|(.*?)\|.*?\}" # MW tags gotta go
subst = "\\1"
reply_final = re.sub(regex, subst, reply_build, 0)
irc.reply(reply_final, prefixNick=False)
lex = wrap(lex, ['text'])
Class = BeestLex
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: