This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheatsh.py
52 lines (35 loc) · 1.47 KB
/
cheatsh.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
import re
from urllib.parse import quote_plus
import aiohttp
from ...common import *
from ... import module as mod
log = mod.get_logger()
escape_tt = str.maketrans({
'`': '\\`'
})
ansi_re = re.compile(r'\x1b\[.*?m')
class CheatShModule(mod.Module):
class Config(mod.Config):
max_length: int = 1000
async def on_load(self):
self.session = aiohttp.ClientSession()
log.info('Session opened')
async def on_unload(self):
if self.session:
log.info('Closing session...')
await self.session.close()
def result_fmt(self, url, language, body_text):
body_space = min(1992 - len(language) - len(url), self.conf.max_length)
if len(body_text) > body_space:
return f'```{language}\n{body_text[:body_space - 20]}\n[...]```\nFull results: {url}'
return f'```{language}\n{body_text}```\n{url}'
@mod.command(name='csh', usage='csh <language> <search terms>', description='Search cheat.sh')
async def cmd_csh(self, ctx, language: str, *search_terms: str):
url = f'https://cheat.sh/{quote_plus(language)}/{quote_plus(" ".join(search_terms))}'
async with self.session.get(
url,
headers={'User-Agent': 'curl/7.68.0'},
timeout=aiohttp.ClientTimeout(total=10)
) as response:
result = ansi_re.sub('', await response.text()).translate(escape_tt)
await ctx.send(self.result_fmt(url, language, result))