2015-09-05 16:56:06 +02:00
|
|
|
#!/usr/bin/env python
|
2015-09-11 01:08:24 +02:00
|
|
|
# pylint: disable=no-name-in-module,unused-import
|
2015-09-05 16:56:06 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
|
|
|
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
|
|
|
|
"""This module contains methods to make POST and GET requests"""
|
|
|
|
|
|
|
|
import json
|
2015-11-11 14:05:57 +01:00
|
|
|
import socket
|
2015-11-02 20:30:37 +01:00
|
|
|
from ssl import SSLError
|
2015-09-05 16:56:06 +02:00
|
|
|
|
|
|
|
try:
|
2015-09-20 17:28:10 +02:00
|
|
|
from urllib.request import urlopen, urlretrieve, Request
|
2015-11-10 15:10:50 +01:00
|
|
|
from urllib.error import HTTPError
|
2015-09-05 16:56:06 +02:00
|
|
|
except ImportError:
|
2015-11-10 15:10:50 +01:00
|
|
|
from urllib import urlretrieve
|
2015-09-05 16:56:06 +02:00
|
|
|
from urllib2 import urlopen, Request
|
2015-11-10 15:10:50 +01:00
|
|
|
from urllib2 import HTTPError
|
2015-09-14 18:13:22 +02:00
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
from telegram import (InputFile, TelegramError)
|
|
|
|
|
|
|
|
|
|
|
|
def _parse(json_data):
|
|
|
|
"""Try and parse the JSON returned from Telegram and return an empty
|
|
|
|
dictionary if there is any error.
|
|
|
|
|
|
|
|
Args:
|
2015-09-07 20:54:12 +02:00
|
|
|
url:
|
|
|
|
urllib.urlopen object
|
2015-09-05 16:56:06 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A JSON parsed as Python dict with results.
|
|
|
|
"""
|
2015-09-11 01:08:24 +02:00
|
|
|
data = json.loads(json_data.decode())
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2015-09-11 01:08:24 +02:00
|
|
|
if not data.get('ok') and data.get('description'):
|
|
|
|
return data['description']
|
2015-09-05 16:56:06 +02:00
|
|
|
|
|
|
|
return data['result']
|
|
|
|
|
2015-09-16 05:21:45 +02:00
|
|
|
|
|
|
|
def get(url):
|
2015-09-05 16:56:06 +02:00
|
|
|
"""Request an URL.
|
|
|
|
Args:
|
|
|
|
url:
|
|
|
|
The web location we want to retrieve.
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
Returns:
|
|
|
|
A JSON object.
|
|
|
|
"""
|
2015-09-16 05:21:45 +02:00
|
|
|
result = urlopen(url).read()
|
2015-09-07 20:54:12 +02:00
|
|
|
|
|
|
|
return _parse(result)
|
2015-09-05 16:56:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def post(url,
|
2015-11-10 22:40:19 +01:00
|
|
|
data,
|
|
|
|
network_delay=2.):
|
2015-09-05 16:56:06 +02:00
|
|
|
"""Request an URL.
|
|
|
|
Args:
|
|
|
|
url:
|
|
|
|
The web location we want to retrieve.
|
|
|
|
data:
|
|
|
|
A dict of (str, unicode) key/value pairs.
|
2015-11-10 22:40:19 +01:00
|
|
|
network_delay:
|
|
|
|
Additional timeout in seconds to allow the response from Telegram to
|
|
|
|
take some time.
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
Returns:
|
|
|
|
A JSON object.
|
|
|
|
"""
|
2015-11-10 05:16:16 +01:00
|
|
|
|
2015-11-10 19:47:21 +01:00
|
|
|
# Add time to the timeout of urlopen to allow data to be transferred over
|
|
|
|
# the network.
|
2015-11-10 15:10:50 +01:00
|
|
|
if 'timeout' in data:
|
2015-11-10 23:12:20 +01:00
|
|
|
timeout = data['timeout'] + network_delay
|
2015-11-02 20:30:37 +01:00
|
|
|
else:
|
|
|
|
timeout = None
|
|
|
|
|
2015-09-05 16:56:06 +02:00
|
|
|
try:
|
|
|
|
if InputFile.is_inputfile(data):
|
|
|
|
data = InputFile(data)
|
2015-09-16 05:21:45 +02:00
|
|
|
request = Request(url,
|
|
|
|
data=data.to_form(),
|
|
|
|
headers=data.headers)
|
2015-09-05 16:56:06 +02:00
|
|
|
else:
|
2015-09-09 14:29:23 +02:00
|
|
|
data = json.dumps(data)
|
2015-09-16 05:21:45 +02:00
|
|
|
request = Request(url,
|
|
|
|
data=data.encode(),
|
|
|
|
headers={'Content-Type': 'application/json'})
|
|
|
|
|
2015-11-02 20:30:37 +01:00
|
|
|
result = urlopen(request, timeout=timeout).read()
|
2015-09-05 16:56:06 +02:00
|
|
|
except HTTPError as error:
|
2015-09-11 01:08:24 +02:00
|
|
|
if error.getcode() == 403:
|
|
|
|
raise TelegramError('Unauthorized')
|
2015-09-24 14:17:50 +02:00
|
|
|
if error.getcode() == 502:
|
|
|
|
raise TelegramError('Bad Gateway')
|
2015-09-11 01:08:24 +02:00
|
|
|
|
2015-09-07 20:54:12 +02:00
|
|
|
message = _parse(error.read())
|
|
|
|
raise TelegramError(message)
|
2015-11-11 14:05:57 +01:00
|
|
|
except (SSLError, socket.timeout) as error:
|
|
|
|
if "operation timed out" in str(error):
|
2015-11-02 20:30:37 +01:00
|
|
|
raise TelegramError("Timed out")
|
2015-09-07 20:54:12 +02:00
|
|
|
|
2015-11-11 14:05:57 +01:00
|
|
|
raise TelegramError(str(error))
|
2015-09-07 20:54:12 +02:00
|
|
|
return _parse(result)
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def download(url,
|
|
|
|
filename):
|
|
|
|
"""Download a file by its URL.
|
|
|
|
Args:
|
|
|
|
url:
|
|
|
|
The web location we want to retrieve.
|
|
|
|
|
|
|
|
filename:
|
|
|
|
The filename wihtin the path to download the file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
urlretrieve(url, filename)
|