-
Notifications
You must be signed in to change notification settings - Fork 0
/
teledocs.py
175 lines (152 loc) · 6.62 KB
/
teledocs.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
# ██╗████████╗███████╗██╗░░░░░░█████╗░██╗░░░██╗███████╗
# ██║╚══██╔══╝╚════██║██║░░░░░██╔══██╗╚██╗░██╔╝╚════██║
# ██║░░░██║░░░░░███╔═╝██║░░░░░███████║░╚████╔╝░░░███╔═╝
# ██║░░░██║░░░██╔══╝░░██║░░░░░██╔══██║░░╚██╔╝░░██╔══╝░░
# ██║░░░██║░░░███████╗███████╗██║░░██║░░░██║░░░███████╗
# ╚═╝░░░╚═╝░░░╚══════╝╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚══════╝
# https://t.me/itzlayz
#
# 🔒 Licensed under the GNU AGPLv3
# https://www.gnu.org/licenses/agpl-3.0.html
# MAIN AUTHOR: hikariatama
# https://mods.hikariatama.ru/view/teledocs.py
import re
import requests as rq
from telethon.tl.types import Message
from aiogram.types import InlineQueryResultArticle, InputTextMessageContent
from .. import loader, utils
def get_message(i: dict) -> str:
return (
f"🔧 <a href=\"https://tl.telethon.dev/{i['link']}\">{i['result']}</a>\n\n"
"🍙 <b>Parameters:</b>\n\n"
f"ℹ️ <i>{utils.escape_html(re.sub(r'<.*?>', '', i['description'][0]))}</i>\n\n"
f"{i['description'][1]}\n\n"
"🦀 <b>Example:</b>\n\n"
f"<pre>{utils.escape_html(i['example'])}</pre>"
)
class TeledocsMod(loader.Module):
"""Telethon docs in your pocket"""
strings = {"name": "Teledocs"}
def __init__(self):
self._tl = None
@staticmethod
def _find(haystack: list, needle: str):
if needle in haystack:
return 0
haystack_index, needle_index, penalty, started = 0, 0, 0, False
while True:
while needle[needle_index] < "a" or needle[needle_index] > "z":
needle_index += 1
if needle_index == len(needle):
return penalty
while haystack[haystack_index] != needle[needle_index]:
haystack_index += 1
if started:
penalty += 1
if haystack_index == len(haystack):
return -1
haystack_index += 1
needle_index += 1
started = True
if needle_index == len(needle):
return penalty
if haystack_index == len(haystack):
return -1
def _get_search_array(self, original: list, original_urls: list, query: str):
destination, destination_urls = [], []
for i, (item, itemu) in enumerate(zip(original, original_urls)):
penalty = self._find(item.lower(), query)
if penalty > -1 and penalty < len(item) / 3:
destination += [[item, i]]
destination_urls += [itemu]
return destination, destination_urls
def _build_list(
self,
found_elements: list,
requests: bool = False,
constructors: bool = False,
) -> list:
return (
[
{
"link": link,
"result": item[0],
"description": self._tl[
"requests_desc" if requests else "constructors_desc"
][item[1]],
**(
{"example": self._tl["requests_ex"][item[1]]}
if requests
else {"example": ""}
),
}
for item, link in zip(*found_elements)
]
if requests or constructors
else [
{
"link": link,
"result": item[0],
"description": ["", ""],
"example": "",
}
for item, link in zip(*found_elements)
]
)
def search(self, query: str):
found_requests = self._get_search_array(
self._tl["requests"],
self._tl["requests_urls"],
query,
)
found_types = self._get_search_array(
self._tl["types"],
self._tl["types_urls"],
query,
)
found_constructors = self._get_search_array(
self._tl["constructors"],
self._tl["constructors_urls"],
query,
)
original = self._tl["requests"] + self._tl["constructors"]
original_urls = self._tl["requests_urls"] + self._tl["constructors_urls"]
destination = []
destination_urls = []
for item, link in zip(original, original_urls):
if item.lower().replace("request", "") == query:
destination += [item]
destination_urls += [link]
return (
self._build_list(found_requests, True)
+ self._build_list(found_types)
+ self._build_list(found_constructors, False, True)
)
def get_args_raw(self, message) -> str:
return utils.get_full_command(message)[-1]
async def on_load(self):
self._tl = (
await utils.run_sync(
rq.get,
"https://github.com/hikariatama/assets/raw/master/tl_docs.json",
)
).json()
@loader.inline_everyone
async def tl_inline_handler(self, query):
"""Shows 50 matches from telethon"""
await query.answer(
[
InlineQueryResultArticle(
id=utils.random_id(),
title=i['result'],
description=re.sub("<.*?>", "", i["description"][0]),
input_message_content=InputTextMessageContent(get_message(i))
) for i in self.search(query.args) if i["description"][0]
][:50]
)
async def tlcmd(self, message: Message):
"""<ref> - Return telethon reference"""
await utils.answer(
message,
get_message(self.search(self.get_args_raw(message))[0]),
)