Skip to content

Commit a4bf0ee

Browse files
committed
Update
1 parent 842889c commit a4bf0ee

28 files changed

+397
-216
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1+
<img src="https://cdn4.iconfinder.com/data/icons/social-media-and-logos-12/32/Logo_telegram_Airplane_Air_plane_paper_airplane-33-256.png" align="right" width="131" />
12

3+
# TGConvertor
4+
5+
![PyPI](https://img.shields.io/pypi/v/TGSessionsConverter)
6+
![PyPI - License](https://img.shields.io/pypi/l/TGSessionsConverter)
7+
8+
This module is small util for easy converting Telegram sessions to various formats (Telethon, Pyrogram, Tdata)
9+
<hr/>
10+
11+
## Installation
12+
13+
```
14+
$ pip install TGConvertor
15+
```
16+
17+
## Quickstart
18+
19+
```python
20+
from TGConvertor import SessionManager
21+
22+
session = SessionManager.from_pyrogram_string('session_str')
23+
print(session.to_pyrogram_string())
24+
```
25+
26+
### How it works
27+
28+
> An authorization session consists of an authorization key and some additional data required to connect. The module
29+
> simply extracts this data and creates an instance of TelegramSession based on it, the methods of which are convenient to
30+
> use to convert to the format you need.

TDATA/tdata/D877F783D5D3EF8C/maps

-68 Bytes
Binary file not shown.

TDATA/tdata/D877F783D5D3EF8Cs

-348 Bytes
Binary file not shown.

TDATA/tdata/key_datas

-388 Bytes
Binary file not shown.

TGConvertor/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .manager import SessionManager
2+
from .manager import PyroSession, TeleSession, TDataSession
3+
4+
__all__ = ["SessionManager", "PyroSession", "TeleSession", "TDataSession"]

TGConvertor/__main__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def main():
2+
print('Soon...')
3+
4+
if __name__ == '__main__':
5+
main()
312 Bytes
Binary file not shown.
305 Bytes
Binary file not shown.
320 Bytes
Binary file not shown.
9.58 KB
Binary file not shown.
File renamed without changes.

TGConvertor/manager.py

+317
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
from pathlib import Path
2+
from typing import Type, Union
3+
from opentele.api import API, APIData
4+
from .sessions.pyro import PyroSession
5+
from .sessions.tele import TeleSession
6+
from .sessions.tdata import TDataSession
7+
from .exceptions import ValidationError
8+
9+
class SessionManager:
10+
def __init__(
11+
self,
12+
dc_id: int,
13+
auth_key: bytes,
14+
user_id: None | int = None,
15+
valid: None | bool = None,
16+
api: Type[APIData] = API.TelegramDesktop,
17+
):
18+
"""
19+
Initializes a SessionManager instance with the specified parameters.
20+
21+
Args:
22+
dc_id (int): Data center ID.
23+
auth_key (bytes): Authentication key.
24+
user_id (None|int, optional): User ID, default is None.
25+
valid (None|bool, optional): Validation status, default is None.
26+
api (Type[APIData], optional): API type, default is API.TelegramDesktop.
27+
"""
28+
29+
self.dc_id = dc_id
30+
self.auth_key = auth_key
31+
self.user_id = user_id
32+
self.valid = valid
33+
self.api = api.copy()
34+
self.user = None
35+
self.client = None
36+
37+
async def __aenter__(self):
38+
"""
39+
Asynchronous context manager entry method.
40+
Establishes a connection to the Telethon client.
41+
"""
42+
self.client = self.telethon_client()
43+
await self.client.connect()
44+
return self.client
45+
46+
async def __aexit__(self, exc_type, exc_val, exc_tb):
47+
"""
48+
Asynchronous context manager exit method.
49+
Disconnects the Telethon client.
50+
"""
51+
await self.client.disconnect()
52+
self.client = None
53+
54+
@property
55+
def auth_key_hex(self) -> str:
56+
"""
57+
Returns the hexadecimal representation of the authentication key.
58+
"""
59+
return self.auth_key.hex()
60+
61+
@classmethod
62+
async def from_telethon_file(cls, file: Union[Path, str], api=API.TelegramDesktop):
63+
"""
64+
Creates a SessionManager instance from a Telethon file.
65+
66+
Args:
67+
file (Union[Path, str]): Path to the Telethon file.
68+
api (Type[APIData], optional): API type, default is API.TelegramDesktop.
69+
70+
Returns:
71+
SessionManager: An instance initialized from the Telethon file.
72+
"""
73+
session = await TeleSession.from_file(file)
74+
return cls(
75+
dc_id=session.dc_id,
76+
auth_key=session.auth_key,
77+
api=api
78+
)
79+
80+
@classmethod
81+
def from_telethon_string(cls, string: str, api=API.TelegramDesktop):
82+
"""
83+
Creates a SessionManager instance from a Telethon string.
84+
85+
Args:
86+
string (str): Telethon session string.
87+
api (Type[APIData], optional): API type, default is API.TelegramDesktop.
88+
89+
Returns:
90+
SessionManager: An instance initialized from the Telethon string.
91+
"""
92+
session = TeleSession.from_string(string)
93+
return cls(
94+
dc_id=session.dc_id,
95+
auth_key=session.auth_key,
96+
api=api
97+
)
98+
99+
@classmethod
100+
async def from_pyrogram_file(cls, file: Union[Path, str], api=API.TelegramDesktop):
101+
"""
102+
Creates a SessionManager instance from a Pyrogram file.
103+
104+
Args:
105+
file (Union[Path, str]): Path to the Pyrogram file.
106+
api (Type[APIData], optional): API type, default is API.TelegramDesktop.
107+
108+
Returns:
109+
SessionManager: An instance initialized from the Pyrogram file.
110+
"""
111+
session = await PyroSession.from_file(file)
112+
return cls(
113+
auth_key=session.auth_key,
114+
dc_id=session.dc_id,
115+
api=api,
116+
user_id=session.user_id,
117+
)
118+
119+
@classmethod
120+
def from_pyrogram_string(cls, string: str, api=API.TelegramDesktop):
121+
"""
122+
Creates a SessionManager instance from a Pyrogram string.
123+
124+
Args:
125+
string (str): Pyrogram session string.
126+
api (Type[APIData], optional): API type, default is API.TelegramDesktop.
127+
128+
Returns:
129+
SessionManager: An instance initialized from the Pyrogram string.
130+
"""
131+
session = PyroSession.from_string(string)
132+
return cls(
133+
auth_key=session.auth_key,
134+
dc_id=session.dc_id,
135+
api=api,
136+
user_id=session.user_id,
137+
)
138+
139+
@classmethod
140+
def from_tdata_folder(cls, folder: Union[Path, str]):
141+
"""
142+
Creates a SessionManager instance from a TData session folder.
143+
144+
Args:
145+
folder (Union[Path, str]): Path to the TData session folder.
146+
147+
Returns:
148+
SessionManager: An instance initialized from the TData session folder.
149+
"""
150+
session = TDataSession.from_tdata(folder)
151+
return cls(
152+
auth_key=session.auth_key,
153+
dc_id=session.dc_id,
154+
api=session.api
155+
)
156+
157+
async def to_pyrogram_file(self, path: Union[Path, str]):
158+
"""
159+
Saves the current session as a Pyrogram file.
160+
161+
Args:
162+
path (Union[Path, str]): Path to save the Pyrogram file.
163+
"""
164+
await self.pyrogram.to_file(path)
165+
166+
def to_pyrogram_string(self) -> str:
167+
"""
168+
Converts the current session to a Pyrogram session string.
169+
170+
Returns:
171+
str: Pyrogram session string.
172+
"""
173+
return self.pyrogram.to_string()
174+
175+
async def to_telethon_file(self, path: Union[Path, str]):
176+
"""
177+
Saves the current session as a Telethon file.
178+
179+
Args:
180+
path (Union[Path, str]): Path to save the Telethon file.
181+
"""
182+
await self.telethon.to_file(path)
183+
184+
def to_telethon_string(self) -> str:
185+
"""
186+
Converts the current session to a Telethon session string.
187+
188+
Returns:
189+
str: Telethon session string.
190+
"""
191+
return self.telethon.to_string()
192+
193+
async def to_tdata_folder(self, path: Union[Path, str]):
194+
"""
195+
Saves the current session as a TData session folder.
196+
197+
Args:
198+
path (Union[Path, str]): Path to save the TData session folder.
199+
"""
200+
await self.get_user_id()
201+
self.tdata.to_folder(path)
202+
203+
@property
204+
def pyrogram(self) -> PyroSession:
205+
"""
206+
Returns a PyroSession instance representing the current session.
207+
"""
208+
return PyroSession(
209+
dc_id=self.dc_id,
210+
auth_key=self.auth_key,
211+
user_id=self.user_id,
212+
)
213+
214+
@property
215+
def telethon(self) -> TeleSession:
216+
"""
217+
Returns a TeleSession instance representing the current session.
218+
"""
219+
return TeleSession(
220+
dc_id=self.dc_id,
221+
auth_key=self.auth_key,
222+
)
223+
224+
@property
225+
def tdata(self) -> TDataSession:
226+
"""
227+
Returns a TDataSession instance representing the current session.
228+
"""
229+
return TDataSession(
230+
dc_id=self.dc_id,
231+
auth_key=self.auth_key,
232+
api=self.api,
233+
user_id=self.user_id,
234+
)
235+
236+
def pyrogram_client(self, proxy=None, no_updates=True):
237+
"""
238+
Returns a Pyrogram client for the current session.
239+
240+
Args:
241+
proxy: Proxy information for the client.
242+
no_updates (bool): Flag indicating whether to disable updates.
243+
244+
Returns:
245+
Pyrogram client instance.
246+
"""
247+
client = self.pyrogram.client(
248+
api=self.api,
249+
proxy=proxy,
250+
no_updates=no_updates,
251+
)
252+
return client
253+
254+
def telethon_client(self, proxy=None, no_updates=True):
255+
"""
256+
Returns a Telethon client for the current session.
257+
258+
Args:
259+
proxy: Proxy information for the client.
260+
no_updates (bool): Flag indicating whether to disable updates.
261+
262+
Returns:
263+
Telethon client instance.
264+
"""
265+
client = self.telethon.client(
266+
api=self.api,
267+
proxy=proxy,
268+
no_updates=no_updates,
269+
)
270+
return client
271+
272+
async def validate(self) -> bool:
273+
"""
274+
Validates the current session.
275+
276+
Returns:
277+
bool: Validation status (True if valid, False otherwise).
278+
"""
279+
user = await self.get_user()
280+
self.valid = bool(user)
281+
return self.valid
282+
283+
async def get_user_id(self) -> int:
284+
"""
285+
Gets the user ID associated with the current session.
286+
287+
Returns:
288+
int: User ID.
289+
290+
Raises:
291+
ValidationError: If the user is not available.
292+
"""
293+
if self.user_id:
294+
return self.user_id
295+
296+
user = await self.get_user()
297+
298+
if user is None:
299+
raise ValidationError()
300+
301+
return user.id
302+
303+
async def get_user(self):
304+
"""
305+
Gets the user information associated with the current session.
306+
307+
Returns:
308+
User: User information.
309+
310+
Raises:
311+
ValidationError: If the user is not available.
312+
"""
313+
async with self as client:
314+
self.user = await client.get_me()
315+
if self.user:
316+
self.user_id = self.user.id
317+
return self.user
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)