From a686db2c6fcd476b3daeb898cd298e6d6ae75920 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Tue, 26 Apr 2016 17:43:35 +0300 Subject: [PATCH 1/4] bot.Bot: fix class docstring --- telegram/bot.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 845fe885c..3073d8a22 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -43,11 +43,9 @@ class Bot(TelegramObject): Args: token (str): Bot's unique authentication. - **kwargs: Arbitrary keyword arguments. - - Keyword Args: base_url (Optional[str]): Telegram Bot API service URL. base_file_url (Optional[str]): Telegram Bot API file URL. + """ def __init__(self, From e160355190fed8f0d11561caf0d2754d37451940 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Mon, 25 Apr 2016 18:25:10 +0300 Subject: [PATCH 2/4] remove unused imports, use future for urllib imports --- telegram/inputfile.py | 10 +++++++--- tests/base.py | 1 - tests/test_bot.py | 1 - tests/test_chat.py | 1 - tests/test_contact.py | 1 - tests/test_emoji.py | 1 - tests/test_jobqueue.py | 5 ----- tests/test_location.py | 1 - tests/test_user.py | 1 - 9 files changed, 7 insertions(+), 15 deletions(-) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index f1d6b013d..55fce2b27 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -21,16 +21,18 @@ """This module contains a object that represents a Telegram InputFile.""" try: + # python 3 from email.generator import _make_boundary as choose_boundary - from urllib.request import urlopen except ImportError: + # python 2 from mimetools import choose_boundary - from urllib2 import urlopen +import imghdr import mimetypes import os import sys -import imghdr + +from future.moves.urllib.request import urlopen from telegram import TelegramError @@ -81,6 +83,8 @@ class InputFile(object): if 'filename' in data: self.filename = self.data.pop('filename') elif hasattr(self.input_file, 'name'): + # on py2.7, pylint fails to understand this properly + # pylint: disable=E1101 self.filename = os.path.basename(self.input_file.name) elif from_url: self.filename = os.path.basename(self.input_file.url) \ diff --git a/tests/base.py b/tests/base.py index 537a2d16f..8b21267a4 100644 --- a/tests/base.py +++ b/tests/base.py @@ -22,7 +22,6 @@ import os import sys import signal -import traceback from nose.tools import make_decorator diff --git a/tests/test_bot.py b/tests/test_bot.py index 20ef84204..a250af2a6 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -21,7 +21,6 @@ """This module contains a object that represents Tests for Telegram Bot""" import io -import os from datetime import datetime import sys from flaky import flaky diff --git a/tests/test_chat.py b/tests/test_chat.py index b5dd626b4..22d84cf49 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -19,7 +19,6 @@ """This module contains a object that represents Tests for Telegram Chat""" -import os import unittest import sys sys.path.append('.') diff --git a/tests/test_contact.py b/tests/test_contact.py index 38e6cdca7..c34cbcbb9 100644 --- a/tests/test_contact.py +++ b/tests/test_contact.py @@ -19,7 +19,6 @@ """This module contains a object that represents Tests for Telegram Contact""" -import os import unittest import sys sys.path.append('.') diff --git a/tests/test_emoji.py b/tests/test_emoji.py index d15bd2747..4df2b5999 100644 --- a/tests/test_emoji.py +++ b/tests/test_emoji.py @@ -19,7 +19,6 @@ """This module contains a object that represents Tests for Telegram Emoji""" -import os import unittest import sys sys.path.append('.') diff --git a/tests/test_jobqueue.py b/tests/test_jobqueue.py index 84bcc9573..7dfc3247e 100644 --- a/tests/test_jobqueue.py +++ b/tests/test_jobqueue.py @@ -30,11 +30,6 @@ if sys.version_info[0:2] == (2, 6): else: import unittest -try: - from urllib2 import urlopen, Request -except ImportError: - from urllib.request import Request, urlopen - sys.path.append('.') from telegram.ext import JobQueue, Updater diff --git a/tests/test_location.py b/tests/test_location.py index b219ed337..d11aa0efd 100644 --- a/tests/test_location.py +++ b/tests/test_location.py @@ -19,7 +19,6 @@ """This module contains a object that represents Tests for Telegram Location""" -import os import unittest import sys sys.path.append('.') diff --git a/tests/test_user.py b/tests/test_user.py index bbafe39a9..33ae93092 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -19,7 +19,6 @@ """This module contains a object that represents Tests for Telegram User""" -import os import unittest import sys sys.path.append('.') From 0ca3ef7a383aa9622d645c3e35a650f9ac0b337b Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Wed, 27 Apr 2016 23:55:00 +0300 Subject: [PATCH 3/4] utils.request: clean imports using feature.moves --- telegram/utils/request.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 55dfab019..28ff9c0ab 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# pylint: disable=no-name-in-module,unused-import # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2016 @@ -25,22 +24,9 @@ import json import socket from ssl import SSLError -try: - # python2 - from httplib import HTTPException -except ImportError: - # python3 - from http.client import HTTPException - -try: - # python3 - from urllib.request import urlopen, urlretrieve, Request - from urllib.error import HTTPError, URLError -except ImportError: - # python2 - from urllib import urlretrieve - from urllib2 import urlopen, Request, URLError - from urllib2 import HTTPError +from future.moves.http.client import HTTPException +from future.moves.urllib.error import HTTPError, URLError +from future.moves.urllib.request import urlopen, urlretrieve, Request from telegram import (InputFile, TelegramError) from telegram.error import Unauthorized, NetworkError, TimedOut From 0c74b3cfb91083df43516a5d04df3c12cb1ad012 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Thu, 28 Apr 2016 00:31:36 +0300 Subject: [PATCH 4/4] bot.py + request.py: network_delay is unique for getUpdates --- telegram/bot.py | 138 +++++++------------------------------- telegram/utils/request.py | 10 +-- 2 files changed, 24 insertions(+), 124 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 3073d8a22..8c3a52807 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -139,8 +139,7 @@ class Bot(TelegramObject): data['reply_markup'] = reply_markup result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) if result is True: return result @@ -206,10 +205,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, the sent message is @@ -256,10 +251,6 @@ class Bot(TelegramObject): receive a notification with no sound. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -315,10 +306,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -385,10 +372,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -449,10 +432,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -503,10 +482,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -560,10 +535,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -622,10 +593,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -675,10 +642,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -737,10 +700,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -798,10 +757,6 @@ class Bot(TelegramObject): keyboard or to force a reply from the user. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, instance representing the @@ -893,10 +848,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: bool: On success, `True` is returned. @@ -925,8 +876,7 @@ class Bot(TelegramObject): data['switch_pm_parameter'] = switch_pm_parameter result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return result @@ -951,10 +901,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: list[:class:`telegram.UserProfilePhotos`]: A list of @@ -975,8 +921,7 @@ class Bot(TelegramObject): data['limit'] = limit result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return UserProfilePhotos.de_json(result) @@ -995,10 +940,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.File`: On success, a :class:`telegram.File` @@ -1014,8 +955,7 @@ class Bot(TelegramObject): data = {'file_id': file_id} result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) if result.get('file_path'): result['file_path'] = '%s/%s' % (self.base_file_url, @@ -1043,10 +983,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: bool: On success, `True` is returned. @@ -1062,8 +998,7 @@ class Bot(TelegramObject): 'user_id': user_id} result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return result @@ -1087,10 +1022,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: bool: On success, `True` is returned. @@ -1106,8 +1037,7 @@ class Bot(TelegramObject): 'user_id': user_id} result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return result @@ -1133,10 +1063,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: bool: On success, `True` is returned. @@ -1156,8 +1082,7 @@ class Bot(TelegramObject): data['show_alert'] = show_alert result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return result @@ -1198,10 +1123,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, if edited message is sent by @@ -1234,8 +1155,7 @@ class Bot(TelegramObject): data['reply_markup'] = reply_markup result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return Message.de_json(result) @@ -1266,10 +1186,6 @@ class Bot(TelegramObject): A JSON-serialized object for an inline keyboard. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, if edited message is sent by @@ -1321,10 +1237,6 @@ class Bot(TelegramObject): A JSON-serialized object for an inline keyboard. timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: :class:`telegram.Message`: On success, if edited message is sent by @@ -1353,7 +1265,8 @@ class Bot(TelegramObject): def getUpdates(self, offset=None, limit=100, - **kwargs): + timeout=0, + network_delay=.2): """Use this method to receive incoming updates using long polling. Args: @@ -1366,14 +1279,14 @@ class Bot(TelegramObject): limit: Limits the number of updates to be retrieved. Values between 1-100 are accepted. Defaults to 100. - - Keyword Args: - timeout (Optional[float]): If this value is specified, use it as - the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. + timeout: + Timeout in seconds for long polling. Defaults to 0, i.e. usual + short polling. + network_delay: + Additional timeout in seconds to allow the response from Telegram + to take some time when using long polling. Defaults to 2, which + should be enough for most connections. Increase it if it takes very + long for data to be transmitted from and to the Telegram servers. Returns: list[:class:`telegram.Message`]: A list of :class:`telegram.Update` @@ -1386,16 +1299,16 @@ class Bot(TelegramObject): url = '{0}/getUpdates'.format(self.base_url) - data = {} + data = {'timeout': timeout} if offset: data['offset'] = offset if limit: data['limit'] = limit - result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + urlopen_timeout = timeout + network_delay + + result = request.post(url, data, timeout=urlopen_timeout) if result: self.logger.debug( @@ -1424,10 +1337,6 @@ class Bot(TelegramObject): Keyword Args: timeout (Optional[float]): If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. - network_delay (Optional[float]): If using the timeout (which is - a `timeout` for the Telegram servers operation), - then `network_delay` as an extra delay (in seconds) to - compensate for network latency. Defaults to 2. Returns: bool: On success, `True` is returned. @@ -1447,8 +1356,7 @@ class Bot(TelegramObject): data['certificate'] = certificate result = request.post(url, data, - timeout=kwargs.get('timeout'), - network_delay=kwargs.get('network_delay')) + timeout=kwargs.get('timeout')) return result diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 28ff9c0ab..ebbd9281c 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -116,8 +116,7 @@ def get(url): @_try_except_req def post(url, data, - timeout=None, - network_delay=2.): + timeout=None): """Request an URL. Args: url: @@ -127,11 +126,6 @@ def post(url, timeout: float. If this value is specified, use it as the definitive timeout (in seconds) for urlopen() operations. [Optional] - network_delay: - float. If using the timeout specified in `data` (which is a timeout for - the Telegram servers operation), then `network_delay` as an extra delay - (in seconds) to compensate for network latency. - default: 2 [Optional] Notes: If neither `timeout` nor `data['timeout']` is specified. The underlying @@ -145,8 +139,6 @@ def post(url, if timeout is not None: urlopen_kwargs['timeout'] = timeout - elif 'timeout' in data: - urlopen_kwargs['timeout'] = data['timeout'] + network_delay if InputFile.is_inputfile(data): data = InputFile(data)