estebanpdl/telegram-tracker

GitHub: estebanpdl/telegram-tracker

一个基于Python的Telegram数据收集工具,连接API批量抓取频道信息与帖子以支持分析研究。

Stars: 379 | Forks: 61

# **Telegram-API**: `一个基于Python的Telegram开源工具` [![GitHub forks](https://img.shields.io/github/forks/estebanpdl/telegram-tracker.svg?style=social&label=Fork&maxAge=2592000)](https://GitHub.com/estebanpdl/telegram-tracker/network/) [![GitHub stars](https://img.shields.io/github/stars/estebanpdl/telegram-tracker?style=social)](https://github.com/estebanpdl/telegram-tracker/stargazers) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/estebanpdl/telegram-tracker/blob/main/LICENCE) [![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://twitter.com/estebanpdl) [![Made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![Twitter estebanpdl](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/estebanpdl)
## 概述 该工具连接到 Telegram API。它生成包含频道数据的 JSON 文件,包括频道信息和帖子。你可以搜索特定频道,或一组在文本文件中提供的频道(每行一个频道)。 文件默认保存在名为 *output/data* 的文件夹中。该文件夹由脚本自动创建。你也可以指定一个输出目录来存储收集的数据。 **所需软件** * [Python 3.x](https://www.python.org/) * [Telegram API 凭证](https://my.telegram.org/auth?to=apps) + Telegram 账户 + App `api_id` + App `api_hash` **所需 Python 库** * [Telethon](https://docs.telethon.dev/en/stable/) * [Pandas](https://pandas.pydata.org/) * [Openpyxl](https://openpyxl.readthedocs.io/en/stable/) * [tqdm](https://tqdm.github.io/) * [Networkx](https://networkx.org/) * [Matplotlib](https://matplotlib.org/) * [Louvain Community Detection](https://github.com/taynaud/python-louvain) ## 安装 - **通过 git clone** ``` git clone https://github.com/estebanpdl/telegram-api.git ``` 这将创建一个名为 `telegram-tracker` 的目录,其中包含 Python 脚本。克隆可以让你轻松升级并切换可用版本。 - **从 GitHub 下载按钮** 从 GitHub 下载 ZIP 文件并使用你喜欢的解压工具将文件 `telegram-tracker.zip` 解压到你的首选位置。 **克隆或下载仓库后,从 `requirements.txt` 安装库。** ``` pip install -r requirements.txt ``` 或 ``` pip3 install -r requirements.txt ``` **一旦你在 my.telegram.org 上获得 API ID 和 API Hash,用描述的值填充 `config/config.ini` 文件。** ``` [Telegram API credentials] api_id = api_id api_hash = api_hash phone = phone ``` *注意:你的手机必须用于首次认证。使用格式 +\\(例如,+19876543210)。Telegram API 将通过 Telegram 应用向你发送一个验证码,你需要将其包含在内。*
# 示例用法 ## `main.py` 此 Python 脚本将连接到 Telegram API 并处理你的 API 请求。 ### 选项 * `--telegram-channel` 指定要下载数据的 Telegram 频道。 * `--batch-file` 包含要下载数据的 Telegram 频道的文本文件,每行一个频道。 * `--limit-download-to-channel-metadata` 仅收集频道元数据,不收集频道消息。(默认 = False) * `--output, -o` 指定保存收集数据的文件夹。如果未提供,脚本将生成默认文件夹 `./output/data` * `--min-id` 指定偏移 ID。这将更新 Telegram 数据中的新帖子。
### 输出数据结构 ``` ├──🗂 output | └──🗂 data | └──🗂 | └──.json | └──_messages.json | └──chats.txt // TM channels, groups, or users' IDs found in data. | └──collected_chats.csv // TM channels or groups found in data (e.g., forwards) | └──collected_chats.xlsx // TM channels or groups found in data (e.g., forwards) | └──counter.csv // TM channels, groups or users found in data (e.g., forwards) | └──user_exceptions.txt // From collected_chats, these are mostly TM users' which | metadata was not possible to retrieve via the API | └──msgs_dataset.csv // Posts and messages from the requested channels ```
## **示例**
### **基本请求** ``` python main.py --telegram-channel channel_name` ``` **预期输出** - 收集的频道文件: - chats.txt - collected_chats.csv - user_exceptions.txt - counter.csv - 一个新文件夹:** 包含 - 一个包含频道配置文件的 JSON 文件 - 一个包含请求频道帖子的 JSON 文件
### **使用包含一组频道的文本文件请求** ``` python main.py --batch-file './path/to/channels_text_file.txt' ``` **预期输出** - 收集的频道文件: - chats.txt - collected_chats.csv - user_exceptions.txt - counter.csv - 基于请求频道数量的新文件夹:** 包含 - 一个包含频道配置文件的 JSON 文件 - 一个包含请求频道帖子的 JSON 文件 这些示例将从 API 获取请求频道的所有可用帖子。如果只想收集频道信息而不包含帖子,可以运行:
### **限制下载仅到频道元数据** ``` python main.py --telegram-channel channelname --limit-download-to-channel-metadata ``` 或通过文本文件使用一组 Telegram 频道: ``` python main.py --batch-file './path/to/channels_text_file.txt' --limit-download-to-channel-metadata ```
### **更新频道数据** 如果你想收集某个频道的新消息,需要先识别最后一条帖子的消息 ID。识别 ID 后,运行: ``` python main.py --telegram-channel channelname --min-id 12345 ``` **预期输出** - 收集的频道文件: - chats.txt - collected_chats.csv - user_exceptions.txt - counter.csv - 一个新文件夹:** 包含 - 一个包含频道配置文件的 JSON 文件 - 一个包含请求频道帖子的 JSON 文件
### **指定输出文件夹** 脚本允许你指定一个特定的输出目录来保存收集的数据。如果目录不存在,脚本会自动创建这些文件夹。 ``` python main.py --telegram-channel channelname --output ./path/to/chosen/directory` ``` 预期输出与上述描述相同,但数据将保存在所选的目录中。
## `build-datasets.py` 此 Python 脚本读取收集的文件并创建一个包含请求频道消息的新数据集。默认情况下,创建的数据集将位于 `output` 文件夹中。 如果你在 `main.py` 中指定了保存收集数据的特定目录,则需要提供相同的路径来使用此脚本。 ### 选项 * `--data-path` 数据所在路径。如果未提供,将使用 `./output/data`。 如果在 `main.py` 中未提供特定目录,运行: ``` python build-datasets.py ``` 如果你在 `main.py` 中使用 `--output` 选项提供了特定目录,运行: ``` python build-datasets.py --data-path ./path/to/chosen/directory ``` 这些选项将创建上述数据集:`msgs_dataset.csv`,一个包含请求频道帖子和消息的 CSV 文件。
## `channels-to-network.py` 此 Python 脚本构建一个网络图。默认情况下,文件将位于 `output` 文件夹中。该脚本还会保存一个初步的图形:`network.png`,使用 matplotlib、networkx 和 python-louvain 模块实现社区检测。你可以使用不同的软件(包括 Gephi)导入 GEFX 图形文件。 ### 选项 * `--data-path` 数据所在路径。如果未提供,将使用 `./output/data`。 如果在 `main.py` 中未提供特定目录,运行: ``` python channels-to-network.py ``` 如果你在 `main.py` 中使用 `--output` 选项提供了特定目录,运行: ``` python channels-to-network.py --data-path ./path/to/chosen/directory ```
标签:API, JSON, Louvain, Matplotlib, Networkx, Openpyxl, Python, SEO, Telegram, Telethon, tqdm, 代码示例, 信息搜集, 开源, 数据分析, 文件输入, 无后门, 渠道搜索, 特权检测, 社交媒体, 社交网络分析, 社区检测, 输出目录, 逆向工具, 频道