python pyTelegrambotapi
Заранее спасибо за внимание
У меня такая задача спарсить все сообщения любых типов из чата в телеграмме, и послать их через бота но не все, а какие нужно, ну это не суть самое главное как их скачать?.
Вот я нашел парсер в инете и он работает
import configparser
import json
from telethon.sync import TelegramClient
from telethon import connection
# для корректного переноса времени сообщений в json
from datetime import date, datetime
# классы для работы с каналами
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
# класс для работы с сообщениями
from telethon.tl.functions.messages import GetHistoryRequest
# Присваиваем значения внутренним переменным
api_id = ''
api_hash = ''
username = ''
client = TelegramClient(username, api_id, api_hash)
client.start()
async def dump_all_participants(channel):
"""Записывает json-файл с информацией о всех участниках канала/чата"""
offset_user = 0 # номер участника, с которого начинается считывание
limit_user = 100 # максимальное число записей, передаваемых за один раз
all_participants = [] # список всех участников канала
filter_user = ChannelParticipantsSearch('')
while True:
participants = await client(GetParticipantsRequest(channel,
filter_user, offset_user, limit_user, hash=0))
if not participants.users:
break
all_participants.extend(participants.users)
offset_user += len(participants.users)
all_users_details = [] # список словарей с интересующими параметрами участников канала
for participant in all_participants:
all_users_details.append({"id": participant.id,
"first_name": participant.first_name,
"last_name": participant.last_name,
"user": participant.username,
"phone": participant.phone,
"is_bot": participant.bot})
#all_users_details.append({"user": participant.username})
with open('channel_users.json', 'w', encoding='utf8') as outfile:
json.dump(all_users_details, outfile, indent=4, ensure_ascii=False)
async def dump_all_messages(channel):
'''Записывает json-файл с информацией о всех сообщениях канала/чата'''
offset_msg = 0 # номер записи, с которой начинается считывание
limit_msg = 100 # максимальное число записей, передаваемых за один раз
all_messages = [] # список всех сообщений
total_messages = 0
total_count_limit = 0 # поменяйте это значение, если вам нужны не все сообщения
class DateTimeEncoder(json.JSONEncoder):
'''Класс для сериализации записи дат в JSON'''
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
if isinstance(o, bytes):
return list(o)
return json.JSONEncoder.default(self, o)
while True:
history = await client(GetHistoryRequest(
peer=channel,
offset_id=offset_msg,
offset_date=None, add_offset=0,
limit=limit_msg, max_id=0, min_id=0,
hash=0))
if not history.messages:
break
messages = history.messages
for message in messages:
all_messages.append(message.to_dict())
offset_msg = messages[len(messages) - 1].id
total_messages = len(all_messages)
if total_count_limit != 0 and total_messages >= total_count_limit:
break
with open('channel_messages.json', 'w', encoding='utf8') as outfile:
json.dump(all_messages, outfile, indent=4, ensure_ascii=False, cls=DateTimeEncoder)
async def main():
url = input("Введите ссылку на канал или чат: ")
channel = await client.get_entity(url)
await dump_all_participants(channel)
await dump_all_messages(channel)
with client:
client.loop.run_until_complete(main())
Этот парсер загружает файл json со всеми сообщениями выбранного чата\канала, вот что в этом файле, я для теста загрузил одно видео с текстом
[
{
"_": "Message",
"id": 13,
"peer_id": {
"_": "PeerChannel",
"channel_id": 1577193802
},
"date": "2022-02-22T11:35:32+00:00",
"message": "Видео для теста",
"out": true,
"mentioned": false,
"media_unread": false,
"silent": false,
"post": false,
"from_scheduled": false,
"legacy": false,
"edit_hide": false,
"pinned": false,
"from_id": {
"_": "PeerUser",
"user_id": 1309129529
},
"fwd_from": null,
"via_bot_id": null,
"reply_to": null,
"media": {
"_": "MessageMediaDocument",
"document": {
"_": "Document",
"id": 5235575184047478329,
"access_hash": -5721662063496490018,
"file_reference": [
4,
94,
2,
17,
74,
0,
0,
0,
13,
98,
20,
222,
72,
5,
105,
218,
45,
57,
53,
160,
76,
147,
153,
133,
165,
21,
105,
212,
172
],
"date": "2022-02-22T11:35:32+00:00",
"mime_type": "video/mp4",
"size": 10957628,
"dc_id": 2,
"attributes": [
{
"_": "DocumentAttributeVideo",
"duration": 27,
"w": 576,
"h": 1024,
"round_message": false,
"supports_streaming": true
},
{
"_": "DocumentAttributeFilename",
"file_name": "video_2022-02-19_13-43-35.mp4"
}
],
"thumbs": [
{
"_": "PhotoStrippedSize",
"type": "i",
"bytes": [
1,
40,
22,
147,
109,
27,
106,
72,
79,
154,
57,
192,
97,
218,
166,
242,
64,
228,
156,
10,
232,
231,
57,
108,
86,
9,
69,
74,
100,
69,
245,
52,
82,
231,
43,
149,
148,
212,
242,
49,
198,
42,
124,
157,
189,
73,
168,
210,
217,
243,
201,
2,
166,
48,
228,
1,
191,
24,
246,
172,
93,
88,
165,
160,
210,
111,
114,
18,
254,
212,
84,
194,
36,
29,
121,
62,
166,
138,
94,
214,
3,
179,
5,
147,
114,
150,
207,
122,
105,
151,
42,
66,
245,
20,
81,
92,
214,
212,
160,
77,
196,
96,
176,
207,
210,
138,
40,
160,
15
]
},
{
"_": "PhotoSize",
"type": "m",
"w": 180,
"h": 320,
"size": 12359
}
],
"video_thumbs": []
},
"ttl_seconds": null
},
"reply_markup": null,
"entities": [],
"views": null,
"forwards": null,
"replies": {
"_": "MessageReplies",
"replies": 0,
"replies_pts": 24,
"comments": false,
"recent_repliers": [],
"channel_id": null,
"max_id": null,
"read_max_id": null
},
"edit_date": null,
"post_author": null,
"grouped_id": null,
"restriction_reason": [],
"ttl_period": null
},
{
"_": "MessageService",
"id": 8,
"peer_id": {
"_": "PeerChannel",
"channel_id": 1577193802
},
"date": "2022-02-21T15:41:12+00:00",
"action": {
"_": "MessageActionChatAddUser",
"users": [
1037608882
]
},
"out": true,
"mentioned": false,
"media_unread": false,
"silent": false,
"post": false,
"legacy": false,
"from_id": {
"_": "PeerUser",
"user_id": 1309129529
},
"reply_to": null,
"ttl_period": null
},
{
"_": "MessageService",
"id": 1,
"peer_id": {
"_": "PeerChannel",
"channel_id": 1577193802
},
"date": "2022-02-21T15:38:21+00:00",
"action": {
"_": "MessageActionChannelMigrateFrom",
"title": "test",
"chat_id": 785314064
},
"out": true,
"mentioned": false,
"media_unread": false,
"silent": false,
"post": false,
"legacy": false,
"from_id": null,
"reply_to": null,
"ttl_period": null
}
]
И теперь мне нужно как то скачать это видео по айди или еще как то не знаю, бот у меня на PytelegramBotApi, надеюсь кто ни будь поможет)