Completed
Pull Request — master (#3)
by Anas
01:01
created

send_image()   C

Complexity

Conditions 9

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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