Skip to content

Commit

Permalink
增强下载能力,现在可以下载群组中指定用户的媒体了
Browse files Browse the repository at this point in the history
  • Loading branch information
holll committed Sep 15, 2023
1 parent 4c7d3cf commit b6e2348
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 45 deletions.
7 changes: 3 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
mutex_group.add_argument('-print', action='store_true', help='打印消息')
mutex_group.add_argument('-m', '--monit', action='store_true', help='监控频道')
parser.add_argument('-id', help='频道ID')
parser.add_argument('-user', help='指定下载用户', default=None)
parser.add_argument('--range', default='>0', help='下载范围')
parser.add_argument('-path', help='上传路径')
parser.add_argument('-dau', default='N', choices=['y', 'Y', 'n', 'N'], help='上传完成删除原文件')
Expand Down Expand Up @@ -53,11 +54,9 @@
client = TelegramClient(md5Token, api_id, api_hash, proxy=(socks.SOCKS5, proxy_ip, proxy_port))
else:
client = TelegramClient(md5Token, api_id, api_hash)
# 配置处理结束


# 接受监视的媒体格式(tg里面直接发送gif最后是mp4格式!),如果需要下载mp4内容可以添加"image/mp4"
accept_file_format = ["image/jpeg", "image/gif", "image/png", "image/webp", "video/mp4"]
# 配置处理结束


async def upDate_dialogs():
Expand Down Expand Up @@ -99,7 +98,7 @@ async def client_main():
channel_id = args.id
plus_func = args.range
for _id in channel_id.split('|'):
client.loop.run_until_complete(down_group(client, _id, plus_func))
client.loop.run_until_complete(down_group(client, _id, plus_func, args.user))
elif args.upload:
del_after_upload = True if args.dau.upper() == 'Y' else False
client.loop.run_until_complete(upload_file(client, args.id, args.path, del_after_upload))
Expand Down
14 changes: 2 additions & 12 deletions tools/down_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,9 @@ def GetFileSuffix(message) -> list:


async def download_file(client: TelegramClient, channel_title, channel_id, message, old=False):
media_type = GetFileSuffix(message)[0]
# 获取媒体类型
is_photo = media_type == 'image'
is_video = media_type == 'video'
is_audio = media_type == 'audio'
is_file = media_type == 'application'

message_time = message.date
formatted_time = datetime.strftime(message_time, '%Y_%m')

# 如果不是文件就放弃(可能是音频文字啥的)
if not (is_photo or is_video or is_audio or is_file):
return
file_name = GetFileName(message)
file_path = f'{os.environ["save_path"]}/{channel_title}-{channel_id}/{file_name}'
new_file_path = f'{os.environ["save_path"]}/{channel_title}-{channel_id}/{formatted_time}/{file_name}'
Expand Down Expand Up @@ -83,9 +73,9 @@ async def download_file(client: TelegramClient, channel_title, channel_id, messa
print(f"媒体已存在:{file_path}")


async def down_group(client: TelegramClient, chat_id, plus_func: str):
async def down_group(client: TelegramClient, chat_id, plus_func: str,from_user):
chat_id = await GetChatId(client, chat_id)
channel_title, messages = await getHistoryMessage(client, chat_id, plus_func) # messages是倒序的
channel_title, messages = await getHistoryMessage(client, chat_id, plus_func,from_user=from_user) # messages是倒序的
async for message in messages:
"""转发消息
await message.forward_to('me')
Expand Down
35 changes: 6 additions & 29 deletions tools/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def print_all_channel(client: TelegramClient):
print('全部输出完成')


async def getHistoryMessage(client: TelegramClient, chat_id: int, plus_func=None):
async def getHistoryMessage(client: TelegramClient, chat_id: int, plus_func=None, from_user=None):
channel_title = await GetChatTitle(client, chat_id)
messages = None
# Todo 根据plus_func获取指定消息区间
Expand All @@ -49,16 +49,16 @@ async def getHistoryMessage(client: TelegramClient, chat_id: int, plus_func=None
if filterFunc != 's':
specifyID = int(plus_func[1:])
if filterFunc == '=':
messages = client.iter_messages(chat_id, ids=specifyID)
messages = client.iter_messages(chat_id, ids=specifyID, from_user=from_user)
elif filterFunc == '>':
messages = client.iter_messages(chat_id, min_id=specifyID)
messages = client.iter_messages(chat_id, min_id=specifyID, from_user=from_user)
elif filterFunc == '<':
messages = client.iter_messages(chat_id, max_id=specifyID)
messages = client.iter_messages(chat_id, max_id=specifyID, from_user=from_user)
else:
tmpId = plus_func[1:].split('s')
messages = client.iter_messages(chat_id, max_id=int(tmpId[-1]), min_id=int(tmpId[0]))
messages = client.iter_messages(chat_id, max_id=int(tmpId[-1]), min_id=int(tmpId[0]), from_user=from_user)
else:
messages = client.iter_messages(chat_id, reverse=True, min_id=1, from_user='ggghh136')
messages = client.iter_messages(chat_id, reverse=True, min_id=1, from_user=from_user)
return channel_title, messages


Expand Down Expand Up @@ -216,26 +216,3 @@ def md5(string):

async def Hook(client: TelegramClient):
return
channel_title, messages = await getHistoryMessage(client, 1318204623)
# 统计每个人的发言次数
count_say = {}
async for message in messages:
if message.from_id is not None:
count_say[message.from_id.user_id] = count_say.get(message.from_id.user_id, 0) + 1
user_say = {}
for user_id in count_say.keys():
people = await client.get_entity(user_id)
user_say[people.username] = count_say[user_id]
print(user_say)

# async for message in client.iter_messages('@chengguangjiepai', ):
# if message.media is not None:
# file_name = GetFileName(message)
# file_path = f'{os.environ["save_path"]}/1-1/{file_name}'
# file_size = message.file.size
# print(f"开始下载:{file_name}")
# with TqdmUpTo(total=file_size, bar_format=TqdmUpTo.bar_format, desc=file_name[:10]) as bar:
# await message.download_media(file_path, progress_callback=bar.update_to)


pass

0 comments on commit b6e2348

Please sign in to comment.