Passed
Push — master ( 9bfda5...dc2491 )
by Anas
02:12
created

modules.glitch.process_img()   B

Complexity

Conditions 6

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nop 2
dl 0
loc 13
rs 8.6666
c 0
b 0
f 0
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 modules.logging import log_command
6
from datetime import datetime
7
from telegram import ChatAction
8
from random import randint
9
import subprocess
10
import os
11
12
13
def module_init(gd):
14
    global path, extensions
15
    path = gd.config["path"]
16
    extensions = gd.config["extensions"]
17
    commands = gd.config["commands"]
18
    for command in commands:
19
        gd.dp.add_handler(MessageHandler(caption_filter(command), glitch))
20
        gd.dp.add_handler(CommandHandler(command, glitch))
21
22
23
# get image, then glitch
24
def glitch(bot, update):
25
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
26
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
27
    try:
28
        extension = get_image(bot, update, path, filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
29
    except:
30
        update.message.reply_text("I can't get the image! :(")
31
        return
32
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
33
    if extension not in extensions:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable extensions does not seem to be defined.
Loading history...
34
        update.message.reply_text("Unsupported file, onii-chan!")
35
        return False
36
    jpg = "convert " + path + filename + extension + " -resize 100% " + path + filename + ".jpg"
37
    subprocess.run(jpg, shell=True)
38
    process_img(update, filename)
39
    os.remove(path+filename+extension)
40
    os.remove(path+filename+"-glitched.jpg")
41
    print (current_time, ">", "/glitch", ">", update.message.from_user.username)
42
    log_command(bot, update, current_time, "glitch")
43
44
45
# glitch processing; deleting lines in .jpg file
46
def process_img(update, filename):
47
    with open(path + filename + ".jpg", "rb") as f:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable path does not seem to be defined.
Loading history...
48
        linelist = list(f)
49
        linecount = len(linelist) - 10
50
        for i in range(5):
51
            i = randint(1, linecount - 1)
52
            linecount = linecount - 1
53
            del linelist[i]
54
    with open(path + filename + "-glitched" + ".jpg", "wb") as f:
55
        for content in linelist:
56
            f.write(content)
57
    with open(path + filename + "-glitched" + ".jpg", "rb") as f:
58
        update.message.reply_photo(f)
59