Completed
Push — master ( 82b9d3...7dbca0 )
by Anas
01:07
created

glitch()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 3
c 4
b 0
f 1
dl 0
loc 13
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler, MessageHandler
4
from modules.utils import get_image, caption_filter
5
from telegram import ChatAction
6
from random import randint
7
import subprocess
8
import datetime
9
import yaml
10
11
12
def handler(dp):
13
    dp.add_handler(MessageHandler(caption_filter("/glitch"), glitch))
14
    dp.add_handler(CommandHandler("glitch", glitch))
15
16
# import path
17
with open("config.yml", "r") as f:
18
    path = yaml.load(f)["path"]["glitch"]
19
20
name = "glitch"
21
extensions = (".jpg", ".jpeg", ".png", ".bmp", ".webp")
22
23
# get image, then glitch
24
def glitch(bot, update):
25
    try:
26
        extension = get_image(bot, update, path)
27
    except:
28
        update.message.reply_text("I can't get the image! :(")
29
        return
30
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
31
    if extension not in extensions:
32
        update.message.reply_text("Unsupported file, onii-chan!")
33
        return False
34
    jpg = "convert " + path + "original" + extension + " -resize 100% " + path + "original.jpg"
35
    subprocess.run(jpg, shell=True)
36
    process_img(update)
37
38
39
# glitch processing; deleting lines in .jpg file
40
def process_img(update):
41
    with open(path + "original.jpg", "rb") as f:
42
        linelist = list(f)
43
        linecount = len(linelist) - 10
44
        for i in range(5):
45
            i = randint(1, linecount - 1)
46
            linecount = linecount - 1
47
            del linelist[i]
48
    with open(path + name + ".jpg", "wb") as f:
49
        for content in linelist:
50
            f.write(content)
51
    with open(path + name + ".jpg", "rb") as f:
52
        update.message.reply_photo(f)
53
    print (datetime.datetime.now(), ">>>", "Done glitching", ">>>",
54
           update.message.from_user.username)