-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
.idea/**/dictionaries | ||
.idea/**/shelf | ||
|
||
test_sessions/* | ||
sessions/* | ||
test.py | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import struct | ||
import base64 | ||
import sqlite3 | ||
from pathlib import Path | ||
from typing import Union, Dict, Tuple, Coroutine, Any | ||
|
||
from pyrogram import Client | ||
from pyrogram.storage import FileStorage, Storage | ||
from telethon import TelegramClient | ||
from telethon.sessions import StringSession | ||
from telethon.tl.types import User | ||
|
||
|
||
class SessionConvertor: | ||
def __init__(self, session_path: Path, config: Dict, work_dir: Path): | ||
self.session_path = session_path | ||
self.inappropriate_sessions_path = work_dir.joinpath('unnecessary_sessions') | ||
self.app_id = config['app_id'] | ||
self.app_hash = config['app_hash'] | ||
self.work_dir = work_dir | ||
|
||
async def convert(self) -> None: | ||
"""Main func""" | ||
user_data, session_data = await self.__get_data_telethon_session() | ||
converted_sting_session = await self.__get_converted_sting_session(session_data, user_data) | ||
await self.move_file_to_unnecessary(self.session_path) | ||
await self.__save_pyrogram_session_file(converted_sting_session, session_data) | ||
|
||
async def move_file_to_unnecessary(self, file_path: Path): | ||
"""Move the unnecessary Telethon session file to the directory with the unnecessary sessions""" | ||
if file_path.exists(): | ||
file_path.rename(self.inappropriate_sessions_path.joinpath(file_path.name)) | ||
|
||
async def __get_data_telethon_session(self) -> Tuple[User, StringSession]: | ||
"""Get User and StringSession""" | ||
async with TelegramClient(self.session_path.with_suffix('').__str__(), self.app_id, self.app_hash) as client: | ||
user_data = await client.get_me() | ||
string_session = StringSession.save(client.session) | ||
session_data = StringSession(string_session) | ||
return user_data, session_data | ||
|
||
async def __save_pyrogram_session_file(self, session_string: Union[str, Coroutine[Any, Any, str]], | ||
session_data: StringSession): | ||
"""Create session file for pyrogram""" | ||
async with Client(self.session_path.stem, session_string=session_string, api_id=self.app_id, | ||
api_hash=self.app_hash, workdir=self.work_dir.__str__()) as client: | ||
user_data = await client.get_me() | ||
client.storage = FileStorage(self.session_path.stem, self.work_dir) | ||
client.storage.conn = sqlite3.Connection(self.session_path) | ||
client.storage.create() | ||
await client.storage.dc_id(session_data.dc_id) | ||
await client.storage.test_mode(False) | ||
await client.storage.auth_key(session_data.auth_key.key) | ||
await client.storage.user_id(user_data.id) | ||
await client.storage.date(0) | ||
await client.storage.is_bot(False) | ||
await client.storage.save() | ||
|
||
@staticmethod | ||
async def __get_converted_sting_session(session_data: StringSession, user_data: User) -> str: | ||
"""Convert to sting session""" | ||
pack = [ | ||
Storage.SESSION_STRING_FORMAT, | ||
session_data.dc_id, | ||
None, | ||
session_data.auth_key.key, | ||
user_data.id, | ||
user_data.bot | ||
] | ||
try: | ||
bytes_pack = struct.pack(*pack) | ||
except struct.error: | ||
pack[0] = Storage.OLD_SESSION_STRING_FORMAT_64 | ||
bytes_pack = struct.pack(*pack) | ||
|
||
encode_pack = base64.urlsafe_b64encode(bytes_pack) | ||
decode_pack = encode_pack.decode() | ||
sting_session = decode_pack.rstrip("=") | ||
return sting_session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
pyaes==1.6.1 | ||
pyasn1==0.4.8 | ||
Pyrogram==2.0.57 | ||
PySocks==1.7.1 | ||
rsa==4.9 | ||
Telethon==1.25.4 | ||
TgCrypto==1.2.4 | ||
uvloop==0.17.0 |