2424
2525import pyrogram
2626from pyrogram import filters , handlers , raw , types
27- from pyrogram .methods .messages .inline_session import get_session
27+ from pyrogram .session import Session
28+ from pyrogram .session .auth import Auth
2829
2930log = logging .getLogger (__name__ )
3031
32+
33+ async def get_session (client : "pyrogram.Client" , dc_id : int ) -> Session :
34+ if dc_id == await client .storage .dc_id ():
35+ return client .session
36+
37+ async with client .media_sessions_lock :
38+ if client .media_sessions .get (dc_id ):
39+ return client .media_sessions [dc_id ]
40+
41+ session = client .media_sessions [dc_id ] = Session (
42+ client ,
43+ dc_id ,
44+ await Auth (client , dc_id , await client .storage .test_mode ()).create (),
45+ await client .storage .test_mode (),
46+ is_media = False ,
47+ )
48+
49+ await session .start ()
50+
51+ return session
52+
53+
3154class QRLogin :
3255 def __init__ (self , client , except_ids : List [int ] = []):
33- self .client = client
34- self .request = raw .functions .auth .ExportLoginToken (
35- api_id = client .api_id ,
36- api_hash = client .api_hash ,
37- except_ids = except_ids
38- )
39- self .r = None
56+ self .client : "pyrogram.Client" = client
57+ self .except_ids : List [int ] = except_ids
58+ self .r : "raw.base.auth.LoginToken" = None
4059
4160 async def recreate (self ):
42- self .r = await self .client .invoke (self .request )
61+ self .r = await self .client .invoke (
62+ raw .functions .auth .ExportLoginToken (
63+ api_id = self .client .api_id ,
64+ api_hash = self .client .api_hash ,
65+ except_ids = self .except_ids ,
66+ )
67+ )
68+
69+ if isinstance (self .r , raw .types .auth .LoginTokenMigrateTo ):
70+ await self .client .storage .dc_id (self .r .dc_id )
71+ self .client .session = await get_session (self .client , self .r .dc_id )
72+ self .r = await self .client .invoke (
73+ raw .functions .auth .ImportLoginToken (token = self .r .token )
74+ )
4375
4476 return self .r
4577
@@ -58,9 +90,7 @@ async def raw_handler(client, update, users, chats):
5890 handler = self .client .add_handler (
5991 handlers .RawUpdateHandler (
6092 raw_handler ,
61- filters = filters .create (
62- lambda _ , __ , u : isinstance (u , raw .types .UpdateLoginToken )
63- )
93+ filters = filters .create (lambda _ , __ , u : isinstance (u , raw .types .UpdateLoginToken )),
6494 )
6595 )
6696
@@ -74,14 +104,6 @@ async def raw_handler(client, update, users, chats):
74104
75105 await self .recreate ()
76106
77- if isinstance (self .r , raw .types .auth .LoginTokenMigrateTo ):
78- session = await get_session (self .client , self .r .dc_id )
79- self .r = await session .invoke (
80- raw .functions .auth .ImportLoginToken (
81- token = self .token
82- )
83- )
84-
85107 if isinstance (self .r , raw .types .auth .LoginTokenSuccess ):
86108 user = types .User ._parse (self .client , self .r .authorization .user )
87109
@@ -90,7 +112,7 @@ async def raw_handler(client, update, users, chats):
90112
91113 return user
92114
93- raise TypeError (' Unexpected login token response: {}' .format (self .r ))
115+ raise TypeError (" Unexpected login token response: {}" .format (self .r ))
94116
95117 @property
96118 def url (self ) -> str :
0 commit comments