Passed
Push — master ( 65d0c5...d05762 )
by Anas
01:59
created

modules.utils.extract_url()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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