Skip to content

Commit

Permalink
add carbon module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeepeo committed May 16, 2019
1 parent 01c5e84 commit 15a392e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Aptfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
neofetch
chromium-chromedriver
chromium-browser
4 changes: 3 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@
"url": "https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest"
}, {
"url": "heroku/python"
}]
},{
"url": "heroku-community/apt"
}]
}
72 changes: 71 additions & 1 deletion userbot/modules/extras.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio, subprocess
import time, re, io
import time, re, io, os
from userbot import bot, LOGGER, LOGGER_GROUP, HELPER
from telethon import events, functions, types
from telethon.events import StopPropagation
Expand All @@ -11,6 +11,11 @@
from telethon.tl.functions.users import GetFullUserRequest
from userbot.events import register
from userbot.modules.rextester.api import UnknownLanguage, Rextester
from time import sleep
from selenium import webdriver
from urllib.parse import quote_plus
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options

@register(outgoing=True, pattern="^.leave$")
async def leave(e):
Expand Down Expand Up @@ -163,6 +168,71 @@ async def rextestercli(e):
await e.edit(output)


@register(outgoing=True, pattern="^.setlang")
async def setlang(prog):
if not prog.text[0].isalpha() and prog.text[0] not in ("/", "#", "@", "!"):
global LANG
LANG = prog.text.split()[1]
await prog.edit(f"language set to {LANG}")


@register(outgoing=True, pattern="^.carbon")
async def carbon_api(e):
if not e.text[0].isalpha() and e.text[0] not in ("/", "#", "@", "!"):
await e.edit("Processing...")
CARBON = 'https://carbon.now.sh/?l={lang}&code={code}'
global LANG
textx = await e.get_reply_message()
pcode = e.text
if pcode[8:]:
pcode = str(pcode[8:])
elif textx:
pcode = str(textx.message) # Importing message to module
code = quote_plus(pcode) # Converting to urlencoded
url = CARBON.format(code=code, lang=LANG)
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-gpu')
prefs = {'download.default_directory': '/'}
chrome_options.add_experimental_option('prefs', prefs)
await e.edit("Processing 30%")

driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
download_path = '/home/'
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_path}}
command_result = driver.execute("send_command", params)

driver.find_element_by_xpath("//button[contains(text(),'Export')]").click()
sleep(3) # this might take a bit.
await e.edit("Processing 50%")
driver.find_element_by_xpath("//button[contains(text(),'PNG')]").click()
sleep(3) # Waiting for downloading

await e.edit("Processing 90%")
file = '/home/carbon.png'
await e.edit("Done!!")
await bot.send_file(
e.chat_id,
file,
reply_to=e.message.reply_to_msg_id,
)

os.remove('/home/carbon.png')
# Removing carbon.png after uploading
await e.delete() # Deleting msg

HELPER.update({
"carbon":".carbon <text> \n Beautify your code"
})
HELPER.update({
'setlang': ".setlang <Lang> \
\nUsage: It will set language for you carbon module "
})
HELPER.update({
"leave": "Leave a Chat"
})
Expand Down

0 comments on commit 15a392e

Please sign in to comment.