5353log = logging .getLogger (__name__ )
5454
5555Config = namedtuple ("Config" , ["api_id" , "api_hash" ])
56+ Proxy = namedtuple ("Proxy" , ["enabled" , "hostname" , "port" , "username" , "password" ])
5657
5758
5859class Client :
@@ -90,6 +91,7 @@ def __init__(self, session_name: str, test_mode: bool = False):
9091 self .markdown = Markdown (self .peers_by_id )
9192
9293 self .config = None
94+ self .proxy = None
9395 self .session = None
9496
9597 self .update_handler = None
@@ -101,7 +103,7 @@ def start(self):
101103 self .load_config ()
102104 self .load_session (self .session_name )
103105
104- self .session = Session (self .dc_id , self .test_mode , self .auth_key , self .config .api_id )
106+ self .session = Session (self .dc_id , self .test_mode , self .proxy , self . auth_key , self .config .api_id )
105107
106108 terms = self .session .start ()
107109
@@ -191,9 +193,9 @@ def authorize(self):
191193 self .session .stop ()
192194
193195 self .dc_id = e .x
194- self .auth_key = Auth (self .dc_id , self .test_mode ).create ()
196+ self .auth_key = Auth (self .dc_id , self .test_mode , self . proxy ).create ()
195197
196- self .session = Session (self .dc_id , self .test_mode , self .auth_key , self .config .api_id )
198+ self .session = Session (self .dc_id , self .test_mode , self .proxy , self . auth_key , self .config .api_id )
197199 self .session .start ()
198200
199201 r = self .send (
@@ -290,21 +292,32 @@ def authorize(self):
290292 return r .user .id
291293
292294 def load_config (self ):
293- config = ConfigParser ()
294- config .read ("config.ini" )
295+ parser = ConfigParser ()
296+ parser .read ("config.ini" )
295297
296298 self .config = Config (
297- int ( config [ "pyrogram" ][ "api_id" ] ),
298- config [ "pyrogram" ][ "api_hash" ]
299+ api_id = parser . getint ( "pyrogram" , "api_id" ),
300+ api_hash = parser . get ( "pyrogram" , "api_hash" )
299301 )
300302
303+ if parser .has_section ("proxy" ):
304+ self .proxy = Proxy (
305+ enabled = parser .getboolean ("proxy" , "enabled" ),
306+ hostname = parser .get ("proxy" , "hostname" ),
307+ port = parser .getint ("proxy" , "port" ),
308+ username = parser .get ("proxy" , "username" , fallback = None ) or None ,
309+ password = parser .get ("proxy" , "password" , fallback = None ) or None
310+ )
311+
312+ print (self .proxy )
313+
301314 def load_session (self , session_name ):
302315 try :
303316 with open ("{}.session" .format (session_name )) as f :
304317 s = json .load (f )
305318 except FileNotFoundError :
306319 self .dc_id = 1
307- self .auth_key = Auth (self .dc_id , self .test_mode ).create ()
320+ self .auth_key = Auth (self .dc_id , self .test_mode , self . proxy ).create ()
308321 else :
309322 self .dc_id = s ["dc_id" ]
310323 self .test_mode = s ["test_mode" ]
@@ -1297,7 +1310,7 @@ def save_file(self, path: str, file_id: int = None, file_part: int = 0):
12971310 file_id = file_id or self .rnd_id ()
12981311 md5_sum = md5 () if not is_big and not is_missing_part else None
12991312
1300- session = Session (self .dc_id , self .test_mode , self .auth_key , self .config .api_id )
1313+ session = Session (self .dc_id , self .test_mode , self .proxy , self . auth_key , self .config .api_id )
13011314 session .start ()
13021315
13031316 try :
@@ -1362,7 +1375,8 @@ def get_file(self,
13621375 session = Session (
13631376 dc_id ,
13641377 self .test_mode ,
1365- Auth (dc_id , self .test_mode ).create (),
1378+ self .proxy ,
1379+ Auth (dc_id , self .test_mode , self .proxy ).create (),
13661380 self .config .api_id
13671381 )
13681382
@@ -1378,6 +1392,7 @@ def get_file(self,
13781392 session = Session (
13791393 dc_id ,
13801394 self .test_mode ,
1395+ self .proxy ,
13811396 self .auth_key ,
13821397 self .config .api_id
13831398 )
@@ -1433,7 +1448,8 @@ def get_file(self,
14331448 cdn_session = Session (
14341449 r .dc_id ,
14351450 self .test_mode ,
1436- Auth (r .dc_id , self .test_mode ).create (),
1451+ self .proxy ,
1452+ Auth (r .dc_id , self .test_mode , self .proxy ).create (),
14371453 self .config .api_id ,
14381454 is_cdn = True
14391455 )
0 commit comments