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

modules.utils.get_param()   B

Complexity

Conditions 7

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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