MCPcopy Create free account
hub / github.com/TechifyBots/File-Stream-Bot / generate_media_session

Method generate_media_session

biisal/utils/custom_dl.py:61–115  ·  view source on GitHub ↗

Generates the media session for the DC that contains the media file. This is required for getting the bytes from Telegram servers.

(self, client: Client, file_id: FileId)

Source from the content-addressed store, hash-verified

59 return self.cached_file_ids[id]
60
61 async def generate_media_session(self, client: Client, file_id: FileId) -> Session:
62 """
63 Generates the media session for the DC that contains the media file.
64 This is required for getting the bytes from Telegram servers.
65 """
66
67 media_session = client.media_sessions.get(file_id.dc_id, None)
68
69 if media_session is None:
70 if file_id.dc_id != await client.storage.dc_id():
71 media_session = Session(
72 client,
73 file_id.dc_id,
74 await Auth(
75 client, file_id.dc_id, await client.storage.test_mode()
76 ).create(),
77 await client.storage.test_mode(),
78 is_media=True,
79 )
80 await media_session.start()
81
82 for _ in range(6):
83 exported_auth = await client.invoke(
84 raw.functions.auth.ExportAuthorization(dc_id=file_id.dc_id)
85 )
86
87 try:
88 await media_session.send(
89 raw.functions.auth.ImportAuthorization(
90 id=exported_auth.id, bytes=exported_auth.bytes
91 )
92 )
93 break
94 except AuthBytesInvalid:
95 logging.debug(
96 f"Invalid authorization bytes for DC {file_id.dc_id}"
97 )
98 continue
99 else:
100 await media_session.stop()
101 raise AuthBytesInvalid
102 else:
103 media_session = Session(
104 client,
105 file_id.dc_id,
106 await client.storage.auth_key(),
107 await client.storage.test_mode(),
108 is_media=True,
109 )
110 await media_session.start()
111 logging.debug(f"Created media session for DC {file_id.dc_id}")
112 client.media_sessions[file_id.dc_id] = media_session
113 else:
114 logging.debug(f"Using cached media session for DC {file_id.dc_id}")
115 return media_session
116
117
118 @staticmethod

Callers 1

yield_fileMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected