Loading all sessions from folder

This commit is contained in:
BarsTiger
2023-05-12 17:36:09 +03:00
parent d80a247cf1
commit b355ed5c07
4 changed files with 42 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ async def create_session_callback():
await (client := GeneratedClient(name=session_name)).start()
sessions[session_name] = SessionConfig(
id=client.me.id,
phone=client.phone_number,
phone=client.me.phone_number,
profile_name=client.me.first_name + (f' {client.me.last_name}' if client.me.last_name else ''),
username=client.me.username
).json()
@@ -27,8 +27,3 @@ async def create_session_callback():
except OperationalError:
print('[red]Cannot create session file.[/] Try using different name...')
input()
except Exception as e:
print(f'[red]Error:[/] {e}...')
input()
return

View File

@@ -0,0 +1 @@
from .callbacks import load_sessions_callback

View File

@@ -0,0 +1,37 @@
from modules.decorators.callback import async_callback
from modules.config import sessions
from modules.client import GeneratedClient
from modules.config.models import SessionConfig
from rich import print
import os
@async_callback
async def load_sessions_callback():
if input('Do you really want to load all .session files that are not in database? '
'It may cause bans of your accounts, because logging to many sessions from one IP at once may seem'
'suspicious for Telegram (y/N) ') not in ['y', 'Y']:
return
print('Loading...')
loaded = 0
for file in os.listdir():
if not file.endswith('.session'):
continue
file = file.replace('.session', '')
if file not in sessions.keys():
try:
await (client := GeneratedClient(name=file)).start()
sessions[file] = SessionConfig(
id=client.me.id,
phone=client.me.phone_number,
profile_name=client.me.first_name + (f' {client.me.last_name}' if client.me.last_name else ''),
username=client.me.username
).json()
print(f'[green]Created[/] session {file}...')
await client.stop()
loaded += 1
except Exception as e:
print(f'[red]Error[/] loading {file} ({e}), skipping...')
print(f'[green]Created[/] {loaded} records...')
input()

View File

@@ -1,6 +1,7 @@
from cursesmenu import CursesMenu
from cursesmenu.items import FunctionItem
from ...callbacks.overkill import overkill_callback
from ...callbacks.load_sessions import load_sessions_callback
from ...dynamic import update
from ...dynamic import delete
@@ -9,6 +10,7 @@ def get_items_list(menu: CursesMenu):
_ = [
update.get_submenu_item,
delete.get_submenu_item,
lambda _: FunctionItem('Delete overkill sessions', function=overkill_callback)
lambda _: FunctionItem('Delete overkill sessions', function=overkill_callback),
lambda _: FunctionItem('Load new sessions in folder', function=load_sessions_callback)
]
return [x(menu) for x in _]