create _isFileRequest to simplify _requestUrl

This commit is contained in:
JASON0916 2015-07-11 22:52:28 +08:00
parent 4c306de3a7
commit f0a5b756cf

View file

@ -583,6 +583,24 @@ class Bot(object):
return True
def _isFileRequest(self,
data=None):
"""Check if the request is a file request
Args:
data:
A dict od (str, unicode) key/value pairs
Returns:
bool
"""
if data:
file_types = ['audio', 'document', 'photo', 'video']
file_type = [i for i in data.keys() if i in file_types]
if file_type:
file_content = data[file_type[0]]
return isinstance(file_content, file) or str(file_content).startswith('http')
return False
def _requestUrl(self,
url,
method,
@ -602,10 +620,7 @@ class Bot(object):
"""
if method == 'POST':
file_type = ['audio', 'document', 'photo', 'video']
# len(dict{key1: val1, key2: val2}) == 2, it is used in case data is None
if len(data) >= 2 and data.keys()[1] in file_type and \
(isinstance(data.items()[1], file) or str(data.items()[1]).startswith('http')):
if self._isFileRequest(data):
try:
data = InputFile(data)
request = urllib2.Request(