Completed
Branch external-img-dl (459aaf)
by Anas
01:37
created

get_image()   C

Complexity

Conditions 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 23
rs 5.5
1
import requests
2
import re
3
4
5
def get_image(bot, update, dl_path):
6
    if update.message.reply_to_message is not None:
7
        if "http" in update.message.reply_to_message.text:
8
                url = re.findall("http[s]?://\S+?\.(?:jpg|jpeg|png|gif|webp)", update.message.reply_to_message.text)
9
                r = requests.get(url[0])
10
                with open(dl_path + "original.jpg", "wb") as f:
11
                    f.write(r.content)
12
        elif update.message.reply_to_message.sticker is not None:
13
            sticker = update.message.reply_to_message.sticker.file_id
14
            bot.getFile(sticker).download(dl_path + "original.jpg")
15
        elif update.message.reply_to_message.document is not None:
16
            print(update.message.reply_to_message.document.file_name)
17
            if update.message.reply_to_message.document.file_name.endswith((".jpg", ".png")):
18
                document = update.message.reply_to_message.document.file_id
19
                bot.getFile(document).download(dl_path + "original.jpg")
20
            else:
21
                raise Exception("Not a valid image")
22
        else:
23
            photo = update.message.reply_to_message.photo[-1].file_id
24
            bot.getFile(photo).download(dl_path + "original.jpg")
25
    else:
26
        photo = update.message.photo[-1].file_id
27
        bot.getFile(photo).download(dl_path + "original.jpg")
28