Completed
Branch external-img-dl (74a600)
by Anas
01:17
created

glitch()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.get_image import get_image
4
from random import randint
5
import yaml
6
import datetime
7
8
# import path
9
with open("config.yml", "r") as f:
10
    glitch_folder = yaml.load(f)["path"]["glitch"]
11
12
13
# get image, then glitch
14
def glitch(bot, update):
15
    try:
16
        get_image(bot, update, glitch_folder)
17
    except:
18
        update.message.reply_text("I can't get the image! :(")
19
        return
20
    process_img(update)
21
22
23
# glitch processing; deleting lines in .jpg file
24
def process_img(update):
25
    with open(glitch_folder + "original.jpg", "rb") as f:
26
        linelist = list(f)
27
        linecount = len(linelist) - 10
28
        for i in range(5):
29
            i = randint(1, linecount - 1)
30
            linecount = linecount - 1
31
            del linelist[i]
32
    with open(glitch_folder + "result.jpg", "wb") as f:
33
        for content in linelist:
34
            f.write(content)
35
    with open(glitch_folder + "result.jpg", "rb") as f:
36
        update.message.reply_photo(f)
37
    print (datetime.datetime.now(), ">>>", "Done glitching", ">>>",
38
           update.message.from_user.username)
39