17
17
#
18
18
# You should have received a copy of the GNU Lesser Public License
19
19
# along with this program. If not, see [http://www.gnu.org/licenses/].
20
-
21
20
"""This module contains a object that represents a Telegram InputFile."""
22
21
23
22
try :
44
43
class InputFile (object ):
45
44
"""This object represents a Telegram InputFile."""
46
45
47
- def __init__ (self ,
48
- data ):
46
+ def __init__ (self , data ):
49
47
self .data = data
50
48
self .boundary = choose_boundary ()
51
49
@@ -87,25 +85,22 @@ def __init__(self,
87
85
# pylint: disable=E1101
88
86
self .filename = os .path .basename (self .input_file .name )
89
87
elif from_url :
90
- self .filename = os .path .basename (self .input_file .url ) \
91
- .split ('?' )[0 ].split ('&' )[0 ]
88
+ self .filename = os .path .basename (self .input_file .url ).split ('?' )[0 ].split ('&' )[0 ]
92
89
93
90
try :
94
91
self .mimetype = InputFile .is_image (self .input_file_content )
95
92
if not self .filename or '.' not in self .filename :
96
93
self .filename = self .mimetype .replace ('/' , '.' )
97
94
except TelegramError :
98
- self .mimetype = mimetypes .guess_type (self .filename )[0 ] or \
99
- DEFAULT_MIME_TYPE
95
+ self .mimetype = mimetypes .guess_type (self .filename )[0 ] or DEFAULT_MIME_TYPE
100
96
101
97
@property
102
98
def headers (self ):
103
99
"""
104
100
Returns:
105
101
str:
106
102
"""
107
- return {'User-agent' : USER_AGENT ,
108
- 'Content-type' : self .content_type }
103
+ return {'User-agent' : USER_AGENT , 'Content-type' : self .content_type }
109
104
110
105
@property
111
106
def content_type (self ):
@@ -126,21 +121,14 @@ def to_form(self):
126
121
# Add data fields
127
122
for name , value in self .data .items ():
128
123
form .extend ([
129
- form_boundary ,
130
- 'Content-Disposition: form-data; name="%s"' % name ,
131
- '' ,
132
- str (value )
124
+ form_boundary , 'Content-Disposition: form-data; name="%s"' % name , '' , str (value )
133
125
])
134
126
135
127
# Add input_file to upload
136
128
form .extend ([
137
- form_boundary ,
138
- 'Content-Disposition: form-data; name="%s"; filename="%s"' % (
129
+ form_boundary , 'Content-Disposition: form-data; name="%s"; filename="%s"' % (
139
130
self .input_name , self .filename
140
- ),
141
- 'Content-Type: %s' % self .mimetype ,
142
- '' ,
143
- self .input_file_content
131
+ ), 'Content-Type: %s' % self .mimetype , '' , self .input_file_content
144
132
])
145
133
146
134
form .append ('--' + self .boundary + '--' )
@@ -193,14 +181,12 @@ def is_inputfile(data):
193
181
bool
194
182
"""
195
183
if data :
196
- file_types = ['audio' , 'document' , 'photo' , 'sticker' , 'video' ,
197
- 'voice' , 'certificate' ]
184
+ file_types = ['audio' , 'document' , 'photo' , 'sticker' , 'video' , 'voice' , 'certificate' ]
198
185
file_type = [i for i in list (data .keys ()) if i in file_types ]
199
186
200
187
if file_type :
201
188
file_content = data [file_type [0 ]]
202
189
203
- return hasattr (file_content , 'read' ) or str (
204
- file_content ).startswith ('http' )
190
+ return hasattr (file_content , 'read' ) or str (file_content ).startswith ('http' )
205
191
206
192
return False
0 commit comments