Skip to content

Commit 59ebbea

Browse files
author
GodSaveTheDoge
committed
Added /carbon
1 parent 805be6d commit 59ebbea

8 files changed

Lines changed: 158 additions & 5 deletions

File tree

config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[pyrogram]
22
api_id = 123456
3-
api_hash = abcdef1234
3+
api_hash = d5b9b8991879f8cd91291d3d9ed821ff
44
[plugins]
55
root = plugins
66
[prefixes]

main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import configparser
1212
import shutil
13+
import logging
1314

1415
ubot = Client(
1516
"MultiUserbot",
@@ -21,11 +22,15 @@
2122
# This is an easy way to have the prefixes in the config file
2223
# Later prefixes will be imported in the plugins
2324

24-
if not os.path.exists("MultiUserbot.session"):
25-
print("Write /commands in a chat to see the commands avaiable!")
26-
2725
if os.path.exists("tmp"):
2826
shutil.rmtree("tmp", ignore_errors=True)
2927

3028
if __name__ == "__main__":
29+
logging.basicConfig(
30+
format="[%(levelname)s %(asctime)s] In module %(module)s, function %(funcName)s at line %(lineno)d -> %("
31+
"message)s",
32+
datefmt="%d/%m/%Y %H:%M:%S %p", level=logging.WARN)
3133
ubot.start()
34+
35+
if not os.path.exists("MultiUserbot.session"):
36+
logging.warning("Write /commands in a chat to see the commands available!")

methods/ArgumentOrReply.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Optional
2+
3+
from pyrogram import Message
4+
5+
6+
def ArgumentOrReply(msg: Message, offset: int = 0) -> Optional[str]:
7+
if len(msg.command) > 1:
8+
return " ".join(msg.command[1:]) if not offset else msg.text[offset:]
9+
elif msg.reply_to_message:
10+
return msg.reply_to_message.text or msg.reply_to_message.caption
11+
else:
12+
return None # this final else is not really needed but I prefer to make it explicit
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# _Selenium
2+
This is not a "plugin", it does not add any command to the userbot.
3+
4+
It's needed for other plugins, read the README.md files inside plugins directories and check if any other plugin is needed for them to work
5+
6+
[How to install geckodriver](https://selenium-python.readthedocs.io/installation.html#drivers)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
from selenium import webdriver
4+
5+
6+
class driverWrapper(webdriver.Firefox): # wrapper for the selenium webdriver
7+
def __getattr__(self, attribute: str): # credits to nocturn9x https://github.com/nocturn9x/BotBase
8+
if attribute in self.__dict__:
9+
return self.__dict__[attribute]
10+
else:
11+
def wrapper(*args, **kwargs):
12+
if hasattr(self.instance, attribute):
13+
try:
14+
return getattr(self.instance, attribute)(*args, **kwargs)
15+
except Exception as e:
16+
logging.error(f"An exception occurred -> {type(e).__name__}: {e}")
17+
return e
18+
else:
19+
raise AttributeError(self.instance, attribute)
20+
21+
return wrapper
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import importlib.util
2+
import logging
3+
import os
4+
import random
5+
import string
6+
import urllib.parse
7+
8+
from pyrogram import Client, Filters, Message
9+
from selenium.webdriver import Firefox
10+
from selenium.webdriver.firefox.options import Options
11+
12+
from main import prefixes
13+
from methods.ArgumentOrReply import ArgumentOrReply
14+
15+
_path = os.path.dirname(__file__)
16+
_selenium_path = os.path.join(_path, "../_selenium/_selenium.py")
17+
18+
if not os.path.exists(_selenium_path):
19+
raise Exception("The _selenium plugin needs to be enabled to work")
20+
21+
spec = importlib.util.spec_from_file_location("driverWrapper", _selenium_path)
22+
_selenium = importlib.util.module_from_spec(spec)
23+
spec.loader.exec_module(_selenium)
24+
driverWrapper = getattr(_selenium, "driverWrapper")
25+
26+
CSS_SELECTOR = ".bg"
27+
OPTIONS = Options()
28+
OPTIONS.headless = True
29+
30+
31+
@Client.on_message(Filters.me & Filters.command("carbon", prefixes=prefixes))
32+
def carbon(c: Client, msg: Message):
33+
code = ArgumentOrReply(msg, len("/carbon "))
34+
if not code:
35+
msg.edit_text("Please reply to a message or use <code>/carbon My Text</code>")
36+
37+
_params = {
38+
"bg": "rgba(57, 70, 79, 1)",
39+
"t": "seti",
40+
"wt": "none",
41+
"l": "auto",
42+
"ds": "true",
43+
"dsyoff": "20px",
44+
"dsblur": "68px",
45+
"wc": "true",
46+
"wa": "true",
47+
"pv": "56px",
48+
"ph": "56px",
49+
"ln": "false",
50+
"fl": "1",
51+
"fm": "Hack",
52+
"fs": "14px",
53+
"lh": "133%",
54+
"si": "false",
55+
"es": "2x",
56+
"wm": "false",
57+
"code": code
58+
}
59+
60+
params = "&".join(["{}={}".format(i, urllib.parse.quote(j)) for i, j in _params.items()])
61+
62+
msg.edit_text("Initializing driver...")
63+
64+
try:
65+
driver: Firefox = driverWrapper(options=OPTIONS)
66+
except Exception as e:
67+
logging.error(f"An exception occurred -> {type(e).__name__}: {e}")
68+
msg.edit_text("Something went wrong!")
69+
return 1
70+
71+
msg.edit_text("Reaching https://carbon.now.sh/...", disable_web_page_preview=True)
72+
73+
driver.get("https://carbon.now.sh/?" + params)
74+
75+
element = driver.find_element_by_css_selector(CSS_SELECTOR)
76+
77+
if not element:
78+
msg.edit_text("Something went wrong!")
79+
return 1
80+
81+
if not os.path.exists("tmp"):
82+
os.mkdir("tmp")
83+
84+
path = "tmp/" + "".join(random.choices(string.ascii_letters, k=40)) + ".png"
85+
open(path, "wb").write(element.screenshot_as_png)
86+
87+
driver.quit()
88+
msg.edit_text("Sending photo...")
89+
90+
if msg.reply_to_message:
91+
c.send_photo(
92+
msg.chat.id,
93+
path,
94+
reply_to_message_id=msg.reply_to_message.message_id
95+
)
96+
else:
97+
c.send_photo(
98+
msg.chat.id,
99+
path
100+
)
101+
102+
msg.delete()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"command": "carbon",
4+
"description": "In reply, it shows the code on carbon.now.sh (you can specify the code with /carbon Your Text) [/carbon]"
5+
}
6+
]

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ TgCrypto~=1.2.1
66
pathlib~=1.0.1
77
beautifulsoup4~=4.9.1
88
Pillow~=7.2.0
9-
telegraph~=1.4.1
9+
telegraph~=1.4.1
10+
selenium~=3.141.0

0 commit comments

Comments
 (0)