mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-02-16 18:31:45 +01:00
enable sending by URL for all sendX methods
This commit is contained in:
parent
5fd7a4fe0d
commit
ef1012b722
2 changed files with 22 additions and 14 deletions
|
@ -27,6 +27,7 @@ try:
|
|||
except ImportError:
|
||||
from mimetools import choose_boundary
|
||||
from urllib2 import urlopen
|
||||
|
||||
import mimetypes
|
||||
import os
|
||||
import sys
|
||||
|
@ -69,19 +70,29 @@ class InputFile(object):
|
|||
self.input_name = 'certificate'
|
||||
self.input_file = data.pop('certificate')
|
||||
|
||||
if isinstance(self.input_file, file):
|
||||
if str(self.input_file).startswith('http'):
|
||||
from_url = True
|
||||
self.input_file = urlopen(self.input_file)
|
||||
else:
|
||||
from_url = False
|
||||
|
||||
if isinstance(self.input_file, file) or from_url:
|
||||
self.input_file_content = self.input_file.read()
|
||||
if 'filename' in data:
|
||||
self.filename = self.data.pop('filename')
|
||||
else:
|
||||
elif isinstance(self.input_file, file):
|
||||
self.filename = os.path.basename(self.input_file.name)
|
||||
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
|
||||
DEFAULT_MIME_TYPE
|
||||
elif from_url:
|
||||
self.filename = self.input_file.url.split('/')[-1].split('?')[0].split('&')[0]
|
||||
|
||||
try:
|
||||
self.mimetype = InputFile.is_image(self.input_file_content)
|
||||
if 'filename' not in dir(self):
|
||||
self.filename = self.mimetype.replace('/', '.')
|
||||
except TelegramError:
|
||||
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
|
||||
DEFAULT_MIME_TYPE
|
||||
|
||||
if 'http' in self.input_file:
|
||||
self.input_file_content = urlopen(self.input_file).read()
|
||||
self.mimetype = InputFile.is_image(self.input_file_content)
|
||||
self.filename = self.mimetype.replace('/', '.')
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
|
@ -185,10 +196,7 @@ class InputFile(object):
|
|||
if file_type:
|
||||
file_content = data[file_type[0]]
|
||||
|
||||
if file_type[0] == 'photo' or file_type[0] == 'document':
|
||||
return isinstance(file_content, file) or \
|
||||
str(file_content).startswith('http')
|
||||
|
||||
return isinstance(file_content, file)
|
||||
return isinstance(file_content, file) or \
|
||||
str(file_content).startswith('http')
|
||||
|
||||
return False
|
||||
|
|
|
@ -96,7 +96,7 @@ class DocumentTest(BaseTest, unittest.TestCase):
|
|||
self.assertTrue(isinstance(document.file_id, str))
|
||||
self.assertNotEqual(document.file_id, '')
|
||||
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
|
||||
self.assertEqual(document.file_name, 'image.gif')
|
||||
self.assertEqual(document.file_name, 'fff.gif')
|
||||
self.assertEqual(document.mime_type, 'image/gif')
|
||||
self.assertEqual(document.file_size, 3878)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue