|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from telegram.ext import CommandHandler, MessageHandler |
|
4
|
|
|
from modules.custom_filters import caption_filter |
|
5
|
|
|
from telegram.ext.dispatcher import run_async |
|
6
|
|
|
from modules.send_image import send_image |
|
7
|
|
|
from modules.get_image import get_image |
|
8
|
|
|
from telegram import ChatAction |
|
9
|
|
|
import subprocess |
|
10
|
|
|
import datetime |
|
11
|
|
|
import legofy |
|
12
|
|
|
import yaml |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def handler(dp): |
|
16
|
|
|
dp.add_handler(MessageHandler(caption_filter("/lego"), lego)) |
|
17
|
|
|
dp.add_handler(CommandHandler("lego", lego)) |
|
18
|
|
|
|
|
19
|
|
|
# import paths |
|
20
|
|
|
with open('config.yml', 'r') as f: |
|
21
|
|
|
path = yaml.load(f)["path"]["lego"] |
|
22
|
|
|
|
|
23
|
|
|
extensions = (".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp") |
|
24
|
|
|
name = "legofied" |
|
25
|
|
|
|
|
26
|
|
|
@run_async |
|
27
|
|
|
def lego(bot, update): |
|
28
|
|
|
if update.message.reply_to_message is not None: |
|
29
|
|
|
parts = update.message.text.split(" ", 1) |
|
30
|
|
|
else: |
|
31
|
|
|
parts = update.message.caption.split(" ", 1) |
|
32
|
|
|
if len(parts) == 1: |
|
33
|
|
|
size = 60 |
|
34
|
|
|
else: |
|
35
|
|
|
try: |
|
36
|
|
|
size = int(parts[1]) |
|
37
|
|
|
except: |
|
38
|
|
|
update.message.reply_text("Paremeter needs to be a number!") |
|
39
|
|
|
return |
|
40
|
|
|
if size > 100 or size < 1: |
|
41
|
|
|
update.message.reply_text("Baka, make it from 1 to 100!") |
|
42
|
|
|
return |
|
43
|
|
|
try: |
|
44
|
|
|
extension = get_image(bot, update, path) |
|
45
|
|
|
except: |
|
46
|
|
|
update.message.reply_text("Can't get the image! :(") |
|
47
|
|
|
return |
|
48
|
|
|
if extension not in extensions: |
|
49
|
|
|
update.message.reply_text("Unsupported file, onii-chan!") |
|
50
|
|
|
return False |
|
51
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
|
52
|
|
|
if extension == ".webp" or ".png": |
|
53
|
|
|
stick = "convert " + path + "original" + extension + " -background white -flatten " + path + "original" + extension |
|
54
|
|
|
subprocess.run(stick, shell=True) |
|
55
|
|
|
legofy.main(image_path=path + "original" + extension, |
|
56
|
|
|
output_path=path + name + extension, |
|
57
|
|
|
size=size, palette_mode=None, dither=False) |
|
58
|
|
|
send_image(bot, update, path, name, extension) |
|
59
|
|
|
print(datetime.datetime.now(), ">>>", "Done legofying", ">>>", update.message.from_user.username) |