1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from modules.utils import caption_filter, get_image, send_image, get_param |
4
|
|
|
from telegram.ext import CommandHandler, MessageHandler |
5
|
|
|
from telegram.ext.dispatcher import run_async |
6
|
|
|
from modules.logging import log_command |
7
|
|
|
from telegram import ChatAction |
8
|
|
|
from datetime import datetime |
9
|
|
|
import subprocess |
10
|
|
|
import legofy |
11
|
|
|
import os |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def module_init(gd): |
15
|
|
|
global path, extensions |
16
|
|
|
path = gd.config["path"] |
17
|
|
|
extensions = gd.config["extensions"] |
18
|
|
|
commands = gd.config["commands"] |
19
|
|
|
for command in commands: |
20
|
|
|
gd.dp.add_handler(MessageHandler(caption_filter("/"+command), lego)) |
21
|
|
|
gd.dp.add_handler(CommandHandler(command, lego)) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@run_async |
25
|
|
|
def lego(bot, update): |
26
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
27
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
28
|
|
|
size = get_param(update, 50, 1, 100) |
29
|
|
|
if size is None: |
30
|
|
|
return |
31
|
|
|
try: |
32
|
|
|
extension = get_image(bot, update, path, filename) |
|
|
|
|
33
|
|
|
except: |
34
|
|
|
update.message.reply_text("Can't get the image! :(") |
35
|
|
|
return |
36
|
|
|
if extension not in extensions: |
|
|
|
|
37
|
|
|
update.message.reply_text("Unsupported file, onii-chan!") |
38
|
|
|
return False |
39
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
40
|
|
|
if extension == ".webp" or ".png": |
41
|
|
|
stick = "convert " + path + filename + extension + " -background white -flatten " + path + filename + extension |
42
|
|
|
subprocess.run(stick, shell=True) |
43
|
|
|
legofy.main(image_path=path + filename + extension, |
44
|
|
|
output_path=path + filename + "-lego" + extension, |
45
|
|
|
size=size, palette_mode=None, dither=False) |
46
|
|
|
send_image(update, path, filename+"-lego", extension) |
47
|
|
|
os.remove(path+filename+extension) |
48
|
|
|
os.remove(path+filename+"-lego"+extension) |
49
|
|
|
print(current_time, ">", "/lego", ">", update.message.from_user.username) |
50
|
|
|
log_command(bot, update, current_time, "lego") |
51
|
|
|
|