trojvn 5ed6f960d5 feat(logging): improve error handling and logging for file movement
Add retry logic to the `move` function to handle cases where files are locked or in use. Implement comprehensive error handling and logging to provide detailed feedback about file movement operations. Update the `Thon` class to include more informative logging messages for banned account handling and client disconnection. Improve the `ProcessThon` class with proper resource cleanup during disconnection.
2026-03-07 16:07:04 +03:00
2026-01-25 22:48:13 +03:00
2026-01-25 23:02:19 +03:00
2026-01-25 23:02:19 +03:00
2026-01-25 23:02:19 +03:00
2026-01-25 22:48:13 +03:00

Thon

Thon is a lightweight, extensible Python library designed for processing Telegram sessions. It provides a clean, asynchronous interface to handle session conversion and automated processing tasks.


📥 Installation

Install the library directly from the repository:

pip install git+https://github.com/your-username/thon.git

🛠 Usage Examples

1. Basic Session Iteration

Iterate through your .session files automatically. Thon converts them on the fly and provides an authorized telethon client for each.

import asyncio
from pathlib import Path
from thon import Thon
from thon.models.options import ThonOptions

async def main():
    # Folder containing your .session files
    sessions_path = Path("my_sessions")
    
    # Basic options
    options = ThonOptions(retries=5, timeout=15)
    
    async for thon_session in Thon(options, sessions_folder=sessions_path):
        # thon_session.thon is an active ProcessThon instance
        # thon_session.session contains session metadata
        
        me = thon_session.thon.me
        print(f"Logged in as: {me.first_name} (@{me.username})")
        
        # Access the underlying Telethon client
        client = thon_session.thon.client
        await client.send_message('me', 'Hello from Thon!')

asyncio.run(main())

2. Searching for Verification Codes

A common task is waiting for a verification code (e.g., from Telegram or a bot). Thon has built-in support for this.

from thon.models.misc import ThonSearchCodeOptions

async def wait_for_code(thon_instance):
    search_options = ThonSearchCodeOptions(
        wait_time=60,    # How long to wait for the message (seconds)
        date_delta=300,  # Only look at messages from the last 5 minutes
        regexp=r'\d{5}'  # Custom regex for the code
    )
    
    code = await thon_instance.search_code(search_options)
    if code:
        print(f"Found code: {code}")
    else:
        print("Code not found.")

3. Handling Banned Sessions and Errors

You can provide a callback to handle errors and automatically move banned sessions to a separate folder.

def on_error():
    print("An error occurred with a session!")

async def process_with_safety():
    options = ThonOptions()
    bot = Thon(
        options,
        sessions_folder=Path("sessions"),
        move_banned_folder=True,  # Automatically moves banned sessions to sessions/banned/
        callback_error=on_error
    )
    
    async for thon_session in bot:
        # Only valid, unbanned sessions reach here
        pass

📁 Project Structure

thon/
├── thon/               # Core package directory
│   ├── __init__.py     # Library entry point (Thon class)
│   ├── converter.py    # Session conversion logic
│   ├── process.py      # Main processing logic (ProcessThon)
│   ├── models/         # Data models and options
│   └── utils.py        # Utility functions
├── pyproject.toml      # Build system and metadata
└── README.md           # Documentation

📜 License

This project is licensed under the MIT License.

Description
Thon helper
Readme 50 KiB
Languages
Python 100%