Skip to content

Commit a7ac419

Browse files
committed
PEP8, lint and TelegramError class refactor
1 parent 354bfca commit a7ac419

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

telegram/bot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def decorator(self, *args, **kwargs):
139139
url, data = func(self, *args, **kwargs)
140140

141141
if not data.get('chat_id'):
142-
raise TelegramError('Invalid chat_id.')
142+
raise TelegramError('Invalid chat_id')
143143

144144
if kwargs.get('reply_to_message_id'):
145145
reply_to_message_id = kwargs.get('reply_to_message_id')
@@ -389,7 +389,7 @@ def sendDocument(self,
389389

390390
if filename:
391391
data['filename'] = filename
392-
392+
393393
return url, data
394394

395395
@log
@@ -422,7 +422,7 @@ def sendSticker(self,
422422

423423
data = {'chat_id': chat_id,
424424
'sticker': sticker}
425-
425+
426426
return url, data
427427

428428
@log

telegram/error.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
"""This module contains a object that represents a Telegram Error"""
2020

21+
import re
22+
2123

2224
class TelegramError(Exception):
2325
"""This object represents a Telegram Error."""
@@ -29,7 +31,11 @@ def __init__(self, message):
2931
"""
3032
super(TelegramError, self).__init__()
3133

32-
self.message = message.split(':')[-1].strip().capitalize()
34+
api_error = re.match(r'^Error: (?P<message>.*)', message)
35+
if api_error:
36+
self.message = api_error.group('message').capitalize()
37+
else:
38+
self.message = message
3339

3440
def __str__(self):
3541
return '%s' % (self.message)

telegram/inputfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def is_image(stream):
164164
if image:
165165
return 'image/%s' % image
166166

167-
raise TelegramError({'message': 'Could not parse file content'})
167+
raise TelegramError('Could not parse file content')
168168

169169
@staticmethod
170170
def is_inputfile(data):
@@ -177,7 +177,7 @@ def is_inputfile(data):
177177
bool
178178
"""
179179
if data:
180-
file_types = ['audio', 'document', 'photo', 'sticker', 'video',
180+
file_types = ['audio', 'document', 'photo', 'sticker', 'video',
181181
'voice', 'certificate']
182182
file_type = [i for i in list(data.keys()) if i in file_types]
183183

telegram/utils/request.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# pylint: disable=no-name-in-module
2+
# pylint: disable=no-name-in-module,unused-import
33
#
44
# A library that provides a Python interface to the Telegram Bot API
55
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
@@ -44,13 +44,10 @@ def _parse(json_data):
4444
Returns:
4545
A JSON parsed as Python dict with results.
4646
"""
47-
try:
48-
data = json.loads(json_data.decode())
47+
data = json.loads(json_data.decode())
4948

50-
if not data.get('ok') and data.get('description'):
51-
return data['description']
52-
except ValueError:
53-
raise TelegramError({'message': 'JSON decoding'})
49+
if not data.get('ok') and data.get('description'):
50+
return data['description']
5451

5552
return data['result']
5653

@@ -84,20 +81,21 @@ def post(url,
8481
try:
8582
if InputFile.is_inputfile(data):
8683
data = InputFile(data)
87-
request = Request(url, data=data.to_form(),
88-
headers=data.headers)
84+
request = Request(url,
85+
data=data.to_form(),
86+
headers=data.headers)
8987
else:
9088
data = json.dumps(data)
91-
request = Request(url, data=data.encode(),
92-
headers={'Content-Type': 'application/json'})
89+
request = Request(url,
90+
data=data.encode(),
91+
headers={'Content-Type': 'application/json'})
9392

9493
result = urlopen(request).read()
9594
except HTTPError as error:
95+
if error.getcode() == 403:
96+
raise TelegramError('Unauthorized')
97+
9698
message = _parse(error.read())
9799
raise TelegramError(message)
98-
except URLError as error:
99-
raise TelegramError(str(error))
100-
except IOError as error:
101-
raise TelegramError(str(error))
102100

103101
return _parse(result)

0 commit comments

Comments
 (0)