-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfdip.py
executable file
·212 lines (182 loc) · 9.12 KB
/
fdip.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import re
import logging
import shutil # 用于删除非空文件夹
import subprocess # 新增导入
from glob import glob
from dotenv import load_dotenv
from datetime import datetime
from telethon.sync import TelegramClient
# 加载环境变量
load_dotenv()
# --------------------------
# 配置区
# --------------------------
API_ID = int(os.getenv('API_ID'))
API_HASH = os.getenv('API_HASH')
SESSION_NAME = os.getenv('SESSION_NAME', 'default_session') # 默认值为 default_session
CHANNEL = '@Marisa_kristi'
DOWNLOAD_DIR = 'txt/fdip'
OUTPUT_FILE = 'proxy.txt'
LOG_DIR = 'logs'
# 配置日志系统
os.makedirs(LOG_DIR, exist_ok=True)
# 删除旧日志文件
for old_log in glob(os.path.join(LOG_DIR, 'fdip_*.log')):
try:
os.remove(old_log)
except Exception as e:
pass # 初始化阶段日志系统尚未就绪
# 创建新日志文件
log_filename = datetime.now().strftime("fdip_%Y%m%d_%H%M%S.log")
log_path = os.path.join(LOG_DIR, log_filename)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_path, encoding='utf-8'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def sanitize_filename(filename):
"""清理文件名中的特殊字符"""
return re.sub(r'[\\/*?:"<>|]', "", filename).strip()
async def main():
async with TelegramClient(SESSION_NAME, API_ID, API_HASH) as client:
try:
logger.info("=== 程序启动 ===")
# --------------------------
# 新增文件清理模块
# --------------------------
# 删除旧下载目录及内容
try:
shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True) # [1,3](@ref)
logger.info(f"已清空下载目录: {DOWNLOAD_DIR}")
except Exception as e:
logger.error(f"删除下载目录失败: {str(e)}")
# 删除旧输出文件
try:
os.remove(OUTPUT_FILE) # [1,3](@ref)
logger.info(f"已删除旧输出文件: {OUTPUT_FILE}")
except FileNotFoundError:
pass # 文件不存在无需处理
except Exception as e:
logger.error(f"删除输出文件失败: {str(e)}")
# 重新创建下载目录
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
# 获取群组实体
try:
group = await client.get_entity(CHANNEL)
logger.debug(f"成功连接频道: {CHANNEL}")
except Exception as e:
logger.error(f"获取群组实体失败: {str(e)}")
return
region_files = {}
message_count = 0
logger.info("开始扫描频道消息...")
async for message in client.iter_messages(group, limit=100):
message_count += 1
if message.document:
try:
file_name = getattr(message.document.attributes[0], 'file_name', '')
file_name = sanitize_filename(file_name)
# 使用改进正则表达式匹配文件名
match = re.match(r'^(.+?)(\d{8})ip.*?\.txt$', file_name)
if not match:
continue
region, date_str = match.groups()
try:
file_date = datetime.strptime(date_str, "%Y%m%d").date()
except ValueError as e:
logger.warning(f"无效日期格式: {file_name} - {str(e)}")
continue
# 更新区域最新文件
if region not in region_files or \
file_date > region_files[region]['date'] or \
(file_date == region_files[region]['date'] and
message.date > region_files[region]['msg_time']):
region_files[region] = {
'date': file_date,
'msg_time': message.date,
'document': message.document,
'file_name': file_name
}
logger.info(f"发现新版本文件: {file_name}")
except Exception as e:
logger.error(f"处理消息异常: {str(e)}", exc_info=True)
logger.info(f"共处理 {message_count} 条消息,找到 {len(region_files)} 个区域的最新文件")
# 下载文件
downloaded_files = []
for region, info in region_files.items():
safe_filename = sanitize_filename(info['file_name'])
file_path = os.path.join(DOWNLOAD_DIR, safe_filename)
try:
logger.info(f"开始下载: {safe_filename}")
await client.download_media(info['document'], file_path)
if os.path.exists(file_path):
if os.path.getsize(file_path) > 0:
downloaded_files.append(file_path)
logger.info(f"下载成功: {safe_filename} ({os.path.getsize(file_path)} bytes)")
else:
os.remove(file_path)
logger.warning(f"空文件已删除: {safe_filename}")
else:
logger.error(f"文件未找到: {safe_filename}")
except Exception as e:
logger.error(f"下载失败: {safe_filename} - {str(e)}", exc_info=True)
# 修改后的文件合并模块(增加正则过滤)
if downloaded_files:
logger.info(f"开始合并 {len(downloaded_files)} 个文件")
with open(OUTPUT_FILE, 'w', encoding='utf-8') as outfile:
for file_path in downloaded_files:
try:
with open(file_path, 'r', encoding='utf-8') as infile:
content = infile.read().strip()
if content:
content = re.sub(r':\d+', '', content)# 移除端口部分(如:443)
content = re.sub(r'#.*', '', content)# 移除注释部分(如#美国1)
content = re.sub(r'\n+', '\n', content) # 合并多个空行
outfile.write(content + '\n\n') # 保持原有格式
else:
logger.warning(f"空内容跳过: {os.path.basename(file_path)}")
except Exception as e:
logger.error(f"文件读取失败: {file_path} - {str(e)}")
logger.info(f"合并完成 → {OUTPUT_FILE}")
# ==========================
# 新增的Git提交模块
# ==========================
if not os.getenv('GITHUB_ACTIONS'):
logger.info("检测到本地运行环境,尝试提交更改...")
try:
# 添加文件到暂存区
logging.info("执行 git add...")
subprocess.run(["git", "add", "."], check=True)
# 提交更改
commit_message = f"cfst: Update proxy.txt on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
logging.info(f"执行 git commit: {commit_message}")
subprocess.run(["git", "commit", "-m", commit_message], check=True)
# 推送更改到远程仓库
logging.info("执行 git push...")
subprocess.run(["git", "push", "-f", "origin", "main"], check=True)
logger.info("文件已成功推送至GitHub仓库")
except subprocess.CalledProcessError as e:
logger.error(f"Git操作失败: {e.stderr.decode().strip()}")
except Exception as e:
logger.error(f"提交过程中发生意外错误: {str(e)}")
else:
logger.info("检测到GitHub Actions环境,跳过提交步骤")
else:
logger.warning("没有找到可合并的文件")
except Exception as e:
logger.error(f"主程序异常: {str(e)}", exc_info=True)
finally:
logger.info("=== 程序结束 ===")
if __name__ == '__main__':
import asyncio
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("用户中断操作")
except Exception as e:
logger.error(f"运行时异常: {str(e)}", exc_info=True)