LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!

Home Page:https://docs.telethon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

telethon.errors.rpcerrorlist.GraphInvalidReloadError Invalid graph token provided, please reload the stats and provide the updated token (caused by LoadAsyncGraphRequest)

mogilyoy opened this issue · comments

Code that causes the issue

async def get_stats(channel_id, client: TelegramClient):
    try:
        await client.connect()
        logger.write_log('INFO', f'Получение статистики для канала {channel_id}')
        result = await client(GetBroadcastStatsRequest(channel_id))
    except StatsMigrateError as e:
        sender = await client._borrow_exported_sender(e.dc)
        result = await client._call(sender, GetBroadcastStatsRequest(
            channel=channel_id
        ))

    growth_graph = result.growth_graph
    followers_graph = result.followers_graph
    mute_graph = result.mute_graph
    top_hours_graph = result.top_hours_graph

    interactions_graph = await load_graphs(result.interactions_graph, client, channel_id, 'interactions_graph')
    iv_interactions_graph = await load_graphs(result.iv_interactions_graph, client, channel_id, 'iv_interactions_graph')
    languages_graph = await load_graphs(result.languages_graph, client, channel_id, 'languages_graph')
    new_followers_by_source_graph = await load_graphs(result.new_followers_by_source_graph, client, channel_id, 'new_followers_by_source_graph')
    views_by_source_graph = await load_graphs(result.views_by_source_graph, client, channel_id, 'views_by_source_graph')

    return (growth_graph.json.data, followers_graph.json.data, mute_graph.json.data, top_hours_graph.json.data,
            interactions_graph.json.data, iv_interactions_graph.json.data, languages_graph.json.data,
            new_followers_by_source_graph.json.data, views_by_source_graph.json.data)


async def load_graphs(graph, client):
    result = await client(LoadAsyncGraphRequest(
        token=graph.token))
    return result.json.data


async def get_all_graphs_dict(channel_id):
    async with TelegramClient('sessions/statistic2', config.api_id, config.api_hash) as client:
        (growth_graph, followers_graph, mute_graph, top_hours_graph,
         interactions_graph, iv_interactions_graph, languages_graph,
         new_followers_by_source_graph, views_by_source_graph) = await get_stats(channel_id, client)

Expected behavior

I was trying to fetch all the graphs from the channel via GetBroadcastStatsRequest and LoadAsyncGraphRequest

Actual behavior

It works fine with a sync version of TelegramClient with some channels, but with the others it causes a StatsMigrateError and after changing the Data Center it freezes forever. I found some code on Github (handles the StatsMigrateError) that fixed my problem with freezing. But now the error says that token that I provide is expired

Traceback

Traceback (most recent call last):
  File "/Users/user/Documents/Python/tg_channel_stats/get_stats.py", line 172, in <module>
    df9 = asyncio.run(get_all_stat('ts'))  # DC 4
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/user/Documents/Python/tg_channel_stats/get_stats.py", line 160, in get_all_stat
    all_graphs_dict, top_hours_dict = await get_all_graphs_dict(channel_id=config.conf_dict[channel_name]['channel_id'], view=view)
  File "/Users/user/Documents/Python/tg_channel_stats/get_stats.py", line 118, in get_all_graphs_dict
    new_followers_by_source_graph, views_by_source_graph) = await get_stats(channel_id, client)
  File "/Users/user/Documents/Python/tg_channel_stats/get_stats.py", line 33, in get_stats
    interactions_graph = await load_graphs(result.interactions_graph, client, channel_id, 'interactions_graph')
  File "/Users/user/Documents/Python/tg_channel_stats/get_stats.py", line 77, in load_graphs
    result = await client(LoadAsyncGraphRequest(
  File "/Users/user/Library/Python/3.9/lib/python/site-packages/telethon/client/users.py", line 87, in _call
    result = await future
telethon.errors.rpcerrorlist.GraphInvalidReloadError: Invalid graph token provided, please reload the stats and provide the updated token (caused by LoadAsyncGraphRequest)

Telethon version

1.34.0

Python version

3.9.6

Operating system (including distribution name and version)

macOS 12.6.8

Other details

No response

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

I found the problem was that client in the second function is not using the migrated sender:

async def load_graphs(graph, client, sender):
    result = await client._call(sender, LoadAsyncGraphRequest(
        token=graph.token
    ))
    return result