简体   繁体   English

如何将 tdata 转换为电视节目 session?

[英]How to convert tdata to telethon session?

I need to convert telegram data to telethon session.我需要将电报数据转换为电视节目 session。 How can i do this?我怎样才能做到这一点? I tried to find a solution, spent three days, but I didn't find anything我试图找到解决方案,花了三天时间,但我没有找到任何东西

It's been nearly two years, but there's now a Python library called opentele created to do this.已经快两年了,但现在创建了一个名为opentele的 Python 库来执行此操作。

Assuming you already have Python knowledge, please follow the steps below.假设您已经具备 Python 知识,请按照以下步骤操作。

First you need to install opentele from PyPi:首先你需要从 PyPi 安装 opentele:

pip install opentele pip 安装opentele

Preparing:准备:

  • You need to have a tdata folder that is already authorized (have at least one logged-in account).您需要有一个已授权的tdata文件夹(至少有一个登录帐户)。
  • The default path to tdata folder on Windows is located at %appdata%\Telegram Desktop\tdata . Windows 上tdata文件夹的默认路径位于%appdata%\Telegram Desktop\tdata
  • Create a python (.py) file and import these things:创建一个 python (.py) 文件并导入以下内容:
     from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession, CreateNewSession import asyncio
  • And you also need to put the main code inside an async function:您还需要将主代码放在异步 function 中:
     async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main())

Initialize TDesktop from tdata folder从 tdata 文件夹初始化TDesktop

  • Load the tdata folder into a TDesktop object:tdata文件夹加载到TDesktop object 中:
     # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\<username>\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded()

Converting TDesktop to TelegramClientTDesktop转换为TelegramClient

There are two ways you can do this, either by using the current session or creating (login to) a new session.有两种方法可以做到这一点,要么使用当前的 session,要么创建(登录)新的 session。

  • Use the current session:使用当前的 session:
     # flag=UseCurrentSession # # Convert TDesktop to Telethon using the current session. client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession)
  • Create a new session:创建一个新的 session:
     # flag=CreateNewSession # # Convert TDesktop to Telethon by creating a new session. # CreateNewSession will use the current session (in tdata folder) to authorize a new session using QR Login. # If 2FA is enabled for this account, you must specify the password via the password argument. # This is of course slower than UseCurrentSession. client = await tdesk.ToTelethon(session="telethon.session", flag=CreateNewSession)

Connect and print all logged-in sessions.连接并打印所有登录的会话。

# Connect and print all logged-in sessions of this client.
# Telethon will save the session to telethon.session on creation.
await client.connect()
await client.PrintSessions()

Final result example最终结果示例

from opentele.td import TDesktop
from opentele.tl import TelegramClient
from opentele.api import API, UseCurrentSession
import asyncio

async def main():

    # Load TDesktop client from tdata folder
    tdataFolder = r"C:\Users\<username>\AppData\Roaming\Telegram Desktop\tdata"
    tdesk = TDesktop(tdataFolder)
    
    # Check if we have loaded any accounts
    assert tdesk.isLoaded()

    # flag=UseCurrentSession
    #
    # Convert TDesktop to Telethon using the current session.
    client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession)
    
    # Connect and print all logged-in sessions of this client.
    # Telethon will save the session to telethon.session on creation.
    await client.connect()
    await client.PrintSessions()

asyncio.run(main())

The result结果

  • The session will be saved to telethon.session file. session 将保存到telethon.session文件。
  • You can use this telethon.session with telethon, or use it directly with opentele - which is recommended because you don't need your own API_ID and API_HASH , the library by default will use Official Telegram API.您可以将此telethon.session与 telethon 一起使用,或直接与opentele一起使用 - 建议您这样做,因为您不需要自己的API_IDAPI_HASH ,该库默认使用官方 Telegram API。

Remark评论

  • opentele is a very well documented library, you can find its documentation here . opentele是一个文档齐全的库,您可以在此处找到它的文档。
  • It can also convert telethon sessions back to tdata and it's usable with Telegram Desktop.它还可以将 Telethon 会话转换回 tdata,并且可以与 Telegram Desktop 一起使用。

Warning警告

  • According to its docs, you must continue using the associated API which you've had used to create that session whenever connecting to the server, or else you're at risk of losing that account.根据其文档,您必须继续使用关联的API ,您在连接到服务器时用于创建 session,否则您有丢失该帐户的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM