-
Notifications
You must be signed in to change notification settings - Fork 1
/
Carbon.py
106 lines (97 loc) · 2.46 KB
/
Carbon.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
import random
from os.path import realpath
import aiohttp
from aiohttp import client_exceptions
class UnableToFetchCarbon(Exception):
pass
themes = [
"3024-night",
"a11y-dark",
"blackboard",
"base16-dark",
"base16-light",
"cobalt",
"duotone-dark",
"dracula-pro",
"hopscotch",
"lucario",
"material",
"monokai",
"nightowl",
"nord",
"oceanic-next",
"one-light",
"one-dark",
"panda-syntax",
"parasio-dark",
"seti",
"shades-of-purple",
"solarized+dark",
"solarized+light",
"synthwave-84",
"twilight",
"verminal",
"vscode",
"yeti",
"zenburn",
]
colour = [
"#FF0000",
"#FF5733",
"#FFFF00",
"#008000",
"#0000FF",
"#800080",
"#A52A2A",
"#FF00FF",
"#D2B48C",
"#00FFFF",
"#808000",
"#800000",
"#00FFFF",
"#30D5C8",
"#00FF00",
"#008080",
"#4B0082",
"#EE82EE",
"#FFC0CB",
"#000000",
"#FFFFFF",
"#808080",
]
class CarbonAPI:
def __init__(self):
self.language = "auto"
self.drop_shadow = True
self.drop_shadow_blur = "68px"
self.drop_shadow_offset = "20px"
self.font_family = "JetBrains Mono"
self.width_adjustment = True
self.watermark = False
async def generate(self, text: str, user_id):
async with aiohttp.ClientSession(
headers={"Content-Type": "application/json"},
) as ses:
params = {
"code": text,
}
params["backgroundColor"] = random.choice(colour)
params["theme"] = random.choice(themes)
params["dropShadow"] = self.drop_shadow
params["dropShadowOffsetY"] = self.drop_shadow_offset
params["dropShadowBlurRadius"] = self.drop_shadow_blur
params["fontFamily"] = self.font_family
params["language"] = self.language
params["watermark"] = self.watermark
params["widthAdjustment"] = self.width_adjustment
try:
request = await ses.post(
"https://carbonara.vercel.app/api/cook",
json=params,
)
except client_exceptions.ClientConnectorError:
raise UnableToFetchCarbon("Can not reach the Host!")
resp = await request.read()
with open(f"cache/carbon{user_id}.jpg", "wb") as f:
f.write(resp)
return realpath(f.name)