-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurban.py
More file actions
108 lines (98 loc) · 3.59 KB
/
Copy pathurban.py
File metadata and controls
108 lines (98 loc) · 3.59 KB
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
from telegram import Bot, Update, InlineKeyboardMarkup, InlineKeyboardButton
from requests import get
import html
import core
apiurl = "http://api.urbandictionary.com/v0/define"
message = """
Definition for <b>%(word)s</b> by %(author)s:
%(definition)s
Examples:
<i>
%(example)s
</i>
<a href="%(permalink)s">Link to definition on Urban dictionary</a>
"""
PLUGINVERSION = 2
# Always name this variable as `plugin`
# If you dont, module loader will fail to load the plugin!
plugin = core.Plugin()
def get_definition(term, number):
definition = get(apiurl, params={
"term": term
}).json()
if definition["result_type"] == "exact":
deftxt = definition["list"][int(number) - 1]
deftxt = escape_definition(deftxt)
return message % deftxt, len(definition["list"])
else:
raise IndexError("Not found")
def escape_definition(definition):
for key, value in definition.items():
if isinstance(value, str):
definition[key] = html.escape(value)
return definition
@plugin.command(command="/ud",
description="Searches for definition of specfied word in urban dictionary",
inline_supported=True,
required_args=1,
hidden=False)
def urband(_: Bot, ___: Update, user, args):
"""
Example usage:
User:/ud test
Bot:
Definition for test by tester:
A process for testing things
Examples:
This is a test message
Link to definition on Urban dictionary (http://test.urbanup.com/708924)
"""
defnum = 1
term = " ".join(args)
try:
definition = get_definition(term, defnum)
except IndexError:
return core.message("Nothing found!", failed=True)
else:
kbd = InlineKeyboardMarkup([
[
InlineKeyboardButton(text="⬅️", callback_data="ud:bwd:" +
str(defnum) + ":" + term + ":" + str(definition[1])),
InlineKeyboardButton(
text="1/" + str(definition[1]), callback_data="none"),
InlineKeyboardButton(text="➡️", callback_data="ud:fwd:" +
str(defnum) + ":" + term + ":" + str(definition[1]))
]
])
return core.message(definition[0], parse_mode="HTML", inline_keyboard=kbd)
@plugin.inline_button("ud")
def urban_pswitch(bot, update, query):
data = query.data.split(":")
term = data[3]
defnum = None
if data[1] == "fwd":
if not int(data[2]) + 1 > int(data[-1]):
defnum = int(data[2])+1
elif data[1] == "bwd":
if not int(data[2]) - 1 <= 0:
defnum = int(data[2]) - 1
if (not data[2] == defnum) and defnum:
definition = get_definition(term, defnum)
query.edit_message_text(text=definition[0], parse_mode="HTML")
kbd = InlineKeyboardMarkup([
[
InlineKeyboardButton(text="⬅️", callback_data="ud:bwd:" +
str(defnum) + ":" + term + ":" + str(definition[1])),
InlineKeyboardButton(
text=str(defnum) + "/" + str(definition[1]), callback_data="none"),
InlineKeyboardButton(text="➡️", callback_data="ud:fwd:" +
str(defnum) + ":" + term + ":" + str(definition[1]))
]
])
query.edit_message_reply_markup(reply_markup=kbd)
if data[1] == "fwd":
query.answer("Switched to next entry")
else:
query.answer("Switched to previous entry")
else:
query.answer("This wont do anything.")