2023-08-06 19:30:45 +08:00
|
|
|
|
import argparse
|
2022-10-23 10:35:37 +08:00
|
|
|
|
import json
|
2022-10-30 15:56:47 +08:00
|
|
|
|
import os
|
2022-10-23 10:35:37 +08:00
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from telethon import TelegramClient
|
2022-10-30 15:56:47 +08:00
|
|
|
|
|
|
|
|
|
from tools.down_file import down_group
|
2023-05-18 19:08:32 +08:00
|
|
|
|
from tools.monit import StartMonit
|
2023-06-19 21:24:07 +08:00
|
|
|
|
from tools.tool import print_all_channel, Hook, print_group, initDb, md5
|
2023-04-10 11:41:55 +08:00
|
|
|
|
from tools.upload_file import upload_file
|
2022-10-23 10:35:37 +08:00
|
|
|
|
|
2023-08-13 09:05:02 +08:00
|
|
|
|
# 1.创建解释器
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
# 2.创建一个互斥参数组
|
|
|
|
|
mutex_group = parser.add_mutually_exclusive_group()
|
|
|
|
|
# 3.添加需要的参数
|
|
|
|
|
parser.add_argument('-c', '--config', default='config.json', help='配置文件')
|
|
|
|
|
parser.add_argument('-re', '--refresh', action='store_true', help='刷新缓存')
|
|
|
|
|
mutex_group.add_argument('-up', '--upload', action='store_true', help='上传文件')
|
|
|
|
|
mutex_group.add_argument('-down', '--download', action='store_true', help='下载文件')
|
|
|
|
|
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')
|
2023-09-15 22:05:55 +08:00
|
|
|
|
parser.add_argument('-user', help='指定下载用户', default=None)
|
2023-08-13 09:05:02 +08:00
|
|
|
|
parser.add_argument('--range', default='>0', help='下载范围')
|
|
|
|
|
parser.add_argument('-path', help='上传路径')
|
|
|
|
|
parser.add_argument('-dau', default='N', choices=['y', 'Y', 'n', 'N'], help='上传完成删除原文件')
|
2023-10-31 22:17:42 +08:00
|
|
|
|
parser.add_argument('-at', '--addtag', help='增加tag')
|
2023-08-13 09:05:02 +08:00
|
|
|
|
# 3.进行参数解析
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
config_path = args.config
|
2022-10-23 10:35:37 +08:00
|
|
|
|
# 配置处理开始
|
|
|
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
config = json.load(f)
|
|
|
|
|
api_id = config.get('api_id')
|
|
|
|
|
api_hash = config.get('api_hash')
|
2023-06-19 21:24:07 +08:00
|
|
|
|
phone = config.get('phone')
|
|
|
|
|
bot_token = config.get('bot_token')
|
2023-10-31 16:06:32 +08:00
|
|
|
|
alias = config.get('alias')
|
2023-06-19 21:24:07 +08:00
|
|
|
|
if (phone is not None and bot_token is not None) or (phone is None and bot_token is None):
|
|
|
|
|
print('请确认使用机器人登录还是电话号码登录')
|
|
|
|
|
exit()
|
|
|
|
|
if phone:
|
|
|
|
|
md5Token = md5(phone)
|
|
|
|
|
else:
|
|
|
|
|
md5Token = md5(bot_token)
|
2023-10-31 16:06:32 +08:00
|
|
|
|
if alias:
|
|
|
|
|
for _id in alias:
|
|
|
|
|
os.environ[_id] = alias[_id]
|
2023-06-19 23:31:53 +08:00
|
|
|
|
initDb(md5Token)
|
2022-10-30 15:56:47 +08:00
|
|
|
|
os.environ['save_path'] = save_path = config.get('save_path')
|
2024-03-01 15:04:01 +08:00
|
|
|
|
proxy = config.get('proxy')
|
|
|
|
|
if proxy is not None:
|
|
|
|
|
username = None
|
|
|
|
|
password = None
|
|
|
|
|
if '@' in proxy:
|
|
|
|
|
username = proxy.split('@')[0].split(':')[0]
|
|
|
|
|
password = proxy.split('@')[0].split(':')[1]
|
|
|
|
|
addr = proxy.split('@')[1].split(':')[0]
|
|
|
|
|
port = proxy.split('@')[1].split(':')[1]
|
|
|
|
|
else:
|
|
|
|
|
addr = proxy.split(':')[0]
|
|
|
|
|
port = proxy.split(':')[1]
|
|
|
|
|
proxy = {
|
|
|
|
|
'proxy_type': 'socks5', # (mandatory) protocol to use (see above)
|
|
|
|
|
'addr': addr, # (mandatory) proxy IP address
|
|
|
|
|
'port': int(port), # (mandatory) proxy port number
|
|
|
|
|
'username': username, # (optional) username if the proxy requires auth
|
|
|
|
|
'password': password, # (optional) password if the proxy requires auth
|
|
|
|
|
'rdns': True # (optional) whether to use remote or local resolve, default remote
|
|
|
|
|
}
|
|
|
|
|
client = TelegramClient(md5Token, api_id, api_hash, proxy=proxy)
|
2022-10-23 10:35:37 +08:00
|
|
|
|
else:
|
2023-06-19 21:24:07 +08:00
|
|
|
|
client = TelegramClient(md5Token, api_id, api_hash)
|
2022-10-23 10:35:37 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-15 22:05:55 +08:00
|
|
|
|
# 配置处理结束
|
2022-10-23 10:35:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def upDate_dialogs():
|
|
|
|
|
await client.get_dialogs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 展示登陆的信息
|
|
|
|
|
def show_my_inf(me):
|
|
|
|
|
print("-----****************-----")
|
|
|
|
|
print("Name:", me.username)
|
|
|
|
|
print("ID:", me.id)
|
|
|
|
|
print("-----login successful-----")
|
|
|
|
|
|
|
|
|
|
|
2022-10-30 15:56:47 +08:00
|
|
|
|
async def client_main():
|
2022-10-23 10:35:37 +08:00
|
|
|
|
print("-client-main-")
|
|
|
|
|
me = await client.get_me()
|
|
|
|
|
show_my_inf(me)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-08-06 19:30:45 +08:00
|
|
|
|
# 除了刷新缓存,都需要频道ID
|
|
|
|
|
if not args.refresh and args.id is None:
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
if args.upload and args.path is None:
|
|
|
|
|
print('缺失上传路径')
|
|
|
|
|
sys.exit(1)
|
2023-03-05 09:26:14 +08:00
|
|
|
|
with client.start(phone=phone, bot_token=bot_token):
|
2022-10-30 15:56:47 +08:00
|
|
|
|
client.loop.run_until_complete(client_main())
|
2023-04-06 11:03:59 +08:00
|
|
|
|
client.loop.run_until_complete(Hook(client))
|
2023-08-06 19:30:45 +08:00
|
|
|
|
if args.refresh:
|
|
|
|
|
print_all_channel(client=client)
|
2023-10-31 22:17:42 +08:00
|
|
|
|
if args.download:
|
2023-08-06 19:30:45 +08:00
|
|
|
|
if 't.me' in args.id:
|
|
|
|
|
tmpList = args.id.split('/')
|
2022-10-24 22:41:12 +08:00
|
|
|
|
channel_id = tmpList[-2]
|
2024-03-01 15:04:01 +08:00
|
|
|
|
plus_func = '=' + tmpList[-1]
|
2022-10-23 10:35:37 +08:00
|
|
|
|
else:
|
2023-08-06 19:30:45 +08:00
|
|
|
|
channel_id = args.id
|
|
|
|
|
plus_func = args.range
|
2023-08-18 22:25:08 +08:00
|
|
|
|
for _id in channel_id.split('|'):
|
2023-09-15 22:05:55 +08:00
|
|
|
|
client.loop.run_until_complete(down_group(client, _id, plus_func, args.user))
|
2023-08-06 19:30:45 +08:00
|
|
|
|
elif args.upload:
|
2023-08-06 22:07:06 +08:00
|
|
|
|
del_after_upload = True if args.dau.upper() == 'Y' else False
|
2023-10-31 22:17:42 +08:00
|
|
|
|
client.loop.run_until_complete(upload_file(client, args.id, args.path, del_after_upload, args.addtag))
|
2023-08-06 19:30:45 +08:00
|
|
|
|
elif args.print:
|
|
|
|
|
client.loop.run_until_complete(print_group(client, args.id))
|
|
|
|
|
elif args.monit:
|
|
|
|
|
channel_ids = args.id.split(',')
|
2023-05-18 19:08:32 +08:00
|
|
|
|
client.loop.run_until_complete(StartMonit(client, channel_ids))
|
|
|
|
|
client.run_until_disconnected()
|