|
27 | 27 | except ImportError: |
28 | 28 | from mimetools import choose_boundary |
29 | 29 | from urllib2 import urlopen |
| 30 | + |
30 | 31 | import mimetypes |
31 | 32 | import os |
32 | 33 | import sys |
@@ -69,19 +70,29 @@ def __init__(self, |
69 | 70 | self.input_name = 'certificate' |
70 | 71 | self.input_file = data.pop('certificate') |
71 | 72 |
|
72 | | - if isinstance(self.input_file, file): |
| 73 | + if str(self.input_file).startswith('http'): |
| 74 | + from_url = True |
| 75 | + self.input_file = urlopen(self.input_file) |
| 76 | + else: |
| 77 | + from_url = False |
| 78 | + |
| 79 | + if isinstance(self.input_file, file) or from_url: |
73 | 80 | self.input_file_content = self.input_file.read() |
74 | 81 | if 'filename' in data: |
75 | 82 | self.filename = self.data.pop('filename') |
76 | | - else: |
| 83 | + elif isinstance(self.input_file, file): |
77 | 84 | self.filename = os.path.basename(self.input_file.name) |
78 | | - self.mimetype = mimetypes.guess_type(self.filename)[0] or \ |
79 | | - DEFAULT_MIME_TYPE |
| 85 | + elif from_url: |
| 86 | + self.filename = self.input_file.url.split('/')[-1].split('?')[0].split('&')[0] |
| 87 | + |
| 88 | + try: |
| 89 | + self.mimetype = InputFile.is_image(self.input_file_content) |
| 90 | + if 'filename' not in dir(self): |
| 91 | + self.filename = self.mimetype.replace('/', '.') |
| 92 | + except TelegramError: |
| 93 | + self.mimetype = mimetypes.guess_type(self.filename)[0] or \ |
| 94 | + DEFAULT_MIME_TYPE |
80 | 95 |
|
81 | | - if 'http' in self.input_file: |
82 | | - self.input_file_content = urlopen(self.input_file).read() |
83 | | - self.mimetype = InputFile.is_image(self.input_file_content) |
84 | | - self.filename = self.mimetype.replace('/', '.') |
85 | 96 |
|
86 | 97 | @property |
87 | 98 | def headers(self): |
@@ -185,10 +196,7 @@ def is_inputfile(data): |
185 | 196 | if file_type: |
186 | 197 | file_content = data[file_type[0]] |
187 | 198 |
|
188 | | - if file_type[0] == 'photo' or file_type[0] == 'document': |
189 | | - return isinstance(file_content, file) or \ |
190 | | - str(file_content).startswith('http') |
191 | | - |
192 | | - return isinstance(file_content, file) |
| 199 | + return isinstance(file_content, file) or \ |
| 200 | + str(file_content).startswith('http') |
193 | 201 |
|
194 | 202 | return False |
0 commit comments