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
|
import json import os import shutil import time
import sys from pyrogram import Client, filters
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler() ch.setLevel(logging.INFO)
fh = logging.FileHandler('log.txt', encoding='utf-8') fh.setLevel(logging.ERROR)
fh_all = logging.FileHandler('log_all.txt', encoding='utf-8') fh_all.setLevel(logging.INFO)
formatter = logging.Formatter( '%(asctime)s-%(name)s-%(filename)s-[line:%(lineno)d]''-%(levelname)s-[日志信息]: %(message)s') ch.setFormatter(formatter)
logger.addHandler(ch) logger.addHandler(fh) logger.addHandler(fh_all) api_id = 11 api_hash = '11' phone_number = '11111' chat_id = 1111 proxy = { "scheme": "socks5", "hostname": "127.0.0.1", "port": 6153 } download_file = '/root/download/download_file'
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): 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 with open('numbers.txt', 'w') as f: f.write(str(message.id)) f.write('\n') f.write(str(number)) logger.info("已经下载了{}个文件".format(number)) f.close() if __name__ == '__main__': app.run(main1())
|