TG 频道/群组文件批量下载 无视分享限制

前两行为依赖 42-53为配置注意修改,55-56是使用代理的配置 ,58-59是不用代理的配置,按照需求注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

# pip install cryptg
# pip install pyrogram
import json
import os
import shutil
import time
# import File_move
import sys
from pyrogram import Client, filters
# 导入logging模块
import logging
# 创建一个loggger,并设置日志级别
logger = logging.getLogger()
# logger 存储到文件
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于将日志输出到控制台,并设置日志级别
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# 创建一个handler,用于将日志输出到文件,并设置日志级别
fh = logging.FileHandler('log.txt', encoding='utf-8')
fh.setLevel(logging.ERROR)
# 创建一个handler,用于将日志输出到文件,并设置日志级别
fh_all = logging.FileHandler('log_all.txt', encoding='utf-8')
fh_all.setLevel(logging.INFO)
# 定义handler的输出格式
formatter = logging.Formatter(
'%(asctime)s-%(name)s-%(filename)s-[line:%(lineno)d]''-%(levelname)s-[日志信息]: %(message)s')
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(ch)
logger.addHandler(fh)
logger.addHandler(fh_all)
api_id = 11
api_hash = '11' # https://my.telegram.org/auth 登陆获取
phone_number = '11111' # 手机号
chat_id = 1111 # 群组id
proxy = {
"scheme": "socks5",
"hostname": "127.0.0.1",
"port": 6153
}
download_file = '/root/download/download_file' # 下载地址
# app = Client("my_account", api_id, api_hash, proxy=proxy, phone_number=phone_number, device_model='PC',
# system_version='Linux', max_concurrent_transmissions=16)
app = Client("my_account", api_id, api_hash, phone_number=phone_number, device_model='PC',
system_version='Linux', max_concurrent_transmissions=32)
async def progress(current, total):
print(f"当前已下载:{current * 100 / total:.1f}%")
async def main1():
await app.start()
all_msg_num = await app.get_chat_history_count(chat_id)
logger.info("当前群组消息总数为:" + str(all_msg_num))
number = 0
# for i in range(all_msg_num, 0, -1):
for i in range(all_msg_num, 0, -1):
# 输出当前时间
logger.info("当前时间:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
message = await app.get_messages(chat_id, i)
logger.info("目前第{}条信息".format(message.id))
if message.photo:
if message.photo.file_id:
filename = message.photo.file_id + '.jpg'
final_filename = download_file + '/' + filename
if os.path.exists(final_filename):
logger.info("文件存在")
continue
else:
logger.info("开始下载")
logger.info("消息ID :{}".format(message.id))
await app.download_media(message, file_name=final_filename, progress=progress)
if message.video:
if message.video.file_id:
filename = message.video.file_id + '.mp4'
final_filename = download_file + '/video/' + filename
if os.path.exists(final_filename):
logger.info("文件存在")
continue
else:
logger.info("开始下载")
logger.info("消息ID:{}".format(message.id))
await app.download_media(message, file_name=final_filename, progress=progress)
number += 1
# starr_msg 存入到numbers.txt,覆盖原来的内容
with open('numbers.txt', 'w') as f:
f.write(str(message.id)) # 消息id
f.write('\n')
f.write(str(number)) # 下载文件数量
# if message.caption:
# f.write('消息标签:\n')
# f.write(str(message.caption[message.caption.find('#') + 6:message.caption.find('\n')]))
logger.info("已经下载了{}个文件".format(number))
f.close()
if __name__ == '__main__':
app.run(main1())

🔰本文标题: TG 频道/群组文件批量下载 无视分享限制

🔞本文链接: https://193.gs/tg-load/index.html

🌡️本文总热度