From 8ca257ca612addd6cbe7cbf227c9f5811a859431 Mon Sep 17 00:00:00 2001 From: JASON0916 Date: Sat, 11 Jul 2015 14:38:43 +0800 Subject: [PATCH 1/3] simplify _requestUrl --- .gitignore | 2 + .idea/scopes/scope_settings.xml | 5 + .idea/workspace.xml | 471 ++++++++++++++++++++++++++++++++ telegram/bot.py | 8 +- 4 files changed, 482 insertions(+), 4 deletions(-) create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 242e8f95d..84868c99b 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,5 @@ docs/_build/ # PyBuilder target/ +.idea/ +.gitignore diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 000000000..922003b84 --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000..988f066d9 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,471 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + 1436540850238 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/telegram/bot.py b/telegram/bot.py index 12e333c79..29705730f 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -602,10 +602,10 @@ class Bot(object): """ if method == 'POST': - if 'audio' in data and (isinstance(data['audio'], file) or 'http' in data['audio']) or \ - 'document' in data and (isinstance(data['document'], file) or 'http' in data['document']) or \ - 'photo' in data and (isinstance(data['photo'], file) or 'http' in data['photo']) or \ - 'video' in data and (isinstance(data['video'], file) or 'http' in data['video']): + 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')): try: data = InputFile(data) request = urllib2.Request( From 4c306de3a7e68561e3d61397b994387336242df9 Mon Sep 17 00:00:00 2001 From: JASON0916 Date: Sat, 11 Jul 2015 14:44:54 +0800 Subject: [PATCH 2/3] delete .idea/ --- .idea/scopes/scope_settings.xml | 5 - .idea/workspace.xml | 471 -------------------------------- 2 files changed, 476 deletions(-) delete mode 100644 .idea/scopes/scope_settings.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml deleted file mode 100644 index 922003b84..000000000 --- a/.idea/scopes/scope_settings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 988f066d9..000000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,471 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - - - - - - - - - - - - - - - - - - - 1436540850238 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From f0a5b756cfa4a7192cef378a896a1a3d2fa3efab Mon Sep 17 00:00:00 2001 From: JASON0916 Date: Sat, 11 Jul 2015 22:52:28 +0800 Subject: [PATCH 3/3] create _isFileRequest to simplify _requestUrl --- telegram/bot.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 29705730f..b21278773 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -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(