Passed
Push — master ( 26ab38...c6e319 )
by Anas
02:01
created

modules.utils.mp4_fix()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# get_image func courtesy of Slko
4
from wand.image import Image
5
import subprocess
6
import requests
7
import os.path
8
import io
9
10
11
def extract_url(entity, text):
12
    if entity["type"] == "text_link":
13
        return entity["url"]
14
    elif entity["type"] == "url":
15
        offset = entity["offset"]
16
        length = entity["length"]
17
        return text[offset:offset+length]
18
    else:
19
        return None
20
21
22
def is_image(path):
23
    ext = None
24
    image_extensions = (".jpg", ".jpeg", ".png", ".gif", ".svg", ".tif", ".bmp", ".mp4")
25
    if path is None:
26
        return False
27
    for i in image_extensions:
28
        if path.casefold().endswith(i):
29
            ext = i
30
            return ext
31
    if ext == None:
32
        ext = ".mp4"
33
        return ext
34
    return False
35
36
37
def get_image(bot, update, dl_path, filename):
38
    output = os.path.join(dl_path, filename)
39
    reply = update.message.reply_to_message
40
    if reply is None:
41
        extension = ".jpg"
42
        bot.getFile(update.message.photo[-1].file_id).download(output + extension)
43
        return extension
44
    # Entities; url, text_link
45
    if reply.entities is not None:
46
        urls = (extract_url(x, reply.text) for x in reply.entities)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
47
        images = [x for x in urls if is_image(x)]
48
        if len(images) > 0:
49
            extension = is_image(images[0])
50
            r = requests.get(images[0])  # use only first image url
51
            with open(output+extension, "wb") as f:
52
                f.write(r.content)
53
            return extension
54
    # Document
55
    if reply.document is not None and is_image(reply.document.file_name):
56
        extension = is_image(reply.document.file_name)
57
        bot.getFile(reply.document.file_id).download(output + extension)
58
        return extension
59
    # Sticker
60
    if reply.sticker is not None:
61
        extension = ".webp"
62
        bot.getFile(reply.sticker.file_id).download(output+extension)
63
        return extension
64
    # Photo in reply
65
    if reply.photo is not None:
66
        extension = ".jpg"
67
        bot.getFile(reply.photo[-1].file_id).download(output + extension)
68
        return extension
69
    return False
70
71
72
def send_image(update, filepath, name, extension):
73
    photo_extensions = (".jpg", ".jpeg")
74
    doc_extensions = (".png", ".svg", ".tif", ".bmp", ".gif", ".mp4")
75
    sticker_extension = ".webp"
76
    for i in photo_extensions:
77
        if extension.endswith(i):
78
            with open(filepath + name + extension, "rb") as f:
79
                update.message.reply_photo(f)
80
            return True  
81
    for i in doc_extensions:
82
        if extension.endswith(i):
83
            with open(filepath + name + extension, "rb") as f:
84
                update.message.reply_document(f, timeout=90)
85
            return True
86
    if extension.endswith(sticker_extension):
87
        with open(filepath + name + extension, "rb") as f:
88
            update.message.reply_sticker(f)
89
        return True
90
91
92
def get_param(update, defaultvalue, min_value, max_value):
93
    if update.message.reply_to_message is not None:
94
        parts = update.message.text.split(" ", 1)
95
    elif update.message.caption is not None:
96
        parts = update.message.caption.split(" ", 1)
97
    elif update.message.text is not None:
98
        parts = update.message.text.split(" ", 2)
99
    else:
100
        return defaultvalue
101
    if len(parts) == 1:
102
        parameter = defaultvalue
103
    else:
104
        try:
105
            parameter = int(parts[1])
106
        except:
107
            #update.message.reply_text("Paremeter needs to be a number!")
108
            return defaultvalue
109
        if  parameter < min_value or parameter > max_value:
110
            errtext = "Baka, make it from " + str(min_value) + " to " + str(max_value) + "!"
111
            update.message.reply_text(errtext)
112
            return None
113
    return parameter
114
    
115
def mp4_fix(path, filename):
116
    args = "ffmpeg -loglevel panic -i " + path + filename + ".mp4" + \
117
            " -an -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 \
118
            -pix_fmt yuv420p -c:v libx264 -profile:v high -level:v 2.0 " \
119
            + path + filename + "fixed.mp4 -y"
120
    subprocess.run(args, shell=True)
121
    os.remove(path+filename+".mp4")
122
    fixed_file_name = filename + "fixed"
123
    return fixed_file_name
124
125
126
# custom filters for message handler
127
# photo with caption
128
def caption_filter(text):
129
    return lambda msg: bool(msg.photo) and bool(msg.caption) and msg.caption.startswith(text)
130
131
132
# text of choice
133
def text_filter(text):
134
    return lambda msg: bool(text in msg.text)
135