Update ff2.py
Browse files
ff2.py
CHANGED
|
@@ -48,13 +48,13 @@ else: # 如果 DATA_JSON 为空,就从单独的环境变量里取
|
|
| 48 |
|
| 49 |
|
| 50 |
def cf():
|
| 51 |
-
os.system("rm -r /data/
|
| 52 |
-
os.system("wget -O '/data/
|
| 53 |
-
os.system("chmod +x /data/
|
| 54 |
# https://ff1.vv9.dpdns.org
|
| 55 |
-
# os.system('/data/
|
| 56 |
subprocess.Popen(
|
| 57 |
-
'/data/cf tunnel run --token
|
| 58 |
stdout=subprocess.DEVNULL,
|
| 59 |
stderr=subprocess.DEVNULL,
|
| 60 |
shell=True,
|
|
@@ -448,83 +448,47 @@ def compress_folder(folder_path, output_dir, keep_count=3):
|
|
| 448 |
# 设置中国时区
|
| 449 |
china_tz = pytz.timezone('Asia/Shanghai')
|
| 450 |
|
| 451 |
-
# 获取当前中国时间
|
| 452 |
-
|
| 453 |
-
timestamp = str(int(current_time.timestamp() * 1000))
|
| 454 |
-
current_date = current_time.strftime('%Y-%m-%d')
|
| 455 |
output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
|
| 456 |
|
| 457 |
# 获取所有压缩包
|
| 458 |
existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
|
| 459 |
|
| 460 |
-
# 安全地提取时间戳
|
| 461 |
def extract_timestamp(filename):
|
| 462 |
match = re.search(r'(\d+)\.tar\.gz$', filename)
|
| 463 |
return int(match.group(1)) if match else 0
|
| 464 |
-
|
| 465 |
-
def get_date_from_timestamp(ts):
|
| 466 |
-
dt = datetime.fromtimestamp(ts/1000, china_tz)
|
| 467 |
-
return dt.strftime('%Y-%m-%d')
|
| 468 |
-
|
| 469 |
-
# 按日期和时间戳整理已有的备份
|
| 470 |
-
date_archives = {}
|
| 471 |
-
for archive in existing_archives:
|
| 472 |
-
ts = extract_timestamp(archive)
|
| 473 |
-
if ts == 0:
|
| 474 |
-
continue
|
| 475 |
-
date = get_date_from_timestamp(ts)
|
| 476 |
-
if date not in date_archives:
|
| 477 |
-
date_archives[date] = []
|
| 478 |
-
date_archives[date].append((archive, ts))
|
| 479 |
|
| 480 |
files_to_cleanup = []
|
| 481 |
|
| 482 |
-
#
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
if date_archives[date]:
|
| 507 |
-
date_archives[date] = [date_archives[date][-1]]
|
| 508 |
-
|
| 509 |
-
# 保持最近三天的备份,删除更早的
|
| 510 |
-
sorted_dates = sorted(date_archives.keys(), reverse=True)
|
| 511 |
-
if len(sorted_dates) > keep_count:
|
| 512 |
-
for old_date in sorted_dates[keep_count:]:
|
| 513 |
-
for archive, _ in date_archives[old_date]:
|
| 514 |
-
oldest_filename = os.path.basename(archive)
|
| 515 |
-
try:
|
| 516 |
-
# 删除本地文件
|
| 517 |
-
os.remove(archive)
|
| 518 |
-
print(f"删除超过{keep_count}天的旧备份:{oldest_filename}")
|
| 519 |
-
|
| 520 |
-
# 记录需要清理的文件名
|
| 521 |
-
files_to_cleanup.append(oldest_filename)
|
| 522 |
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
delete_huggingface_lfs_file(oldest_filename, f"{HF_USER1}/{HF_REPO}", HF_TOKEN1)
|
| 526 |
-
except Exception as e:
|
| 527 |
-
print(f"删除失败 {archive}: {e}")
|
| 528 |
|
| 529 |
# 如果有文件需要从Git历史中清理,执行安全清理
|
| 530 |
if files_to_cleanup:
|
|
@@ -549,21 +513,7 @@ def compress_folder(folder_path, output_dir, keep_count=3):
|
|
| 549 |
print(f"压缩成功:{output_path}")
|
| 550 |
print(f"压缩大小:{file_size:.2f} MB")
|
| 551 |
print(f"压缩时间:{formatted_time}")
|
| 552 |
-
print(f"保留策略:
|
| 553 |
-
|
| 554 |
-
# 如果有当天的旧备份,删除
|
| 555 |
-
if current_date in date_archives and date_archives[current_date]:
|
| 556 |
-
for old_archive_info in date_archives[current_date]:
|
| 557 |
-
old_archive, old_ts = old_archive_info
|
| 558 |
-
old_filename = os.path.basename(old_archive)
|
| 559 |
-
if os.path.exists(old_archive) and old_filename != os.path.basename(output_path):
|
| 560 |
-
try:
|
| 561 |
-
os.remove(old_archive)
|
| 562 |
-
print(f"删除当天的旧备份:{old_filename}")
|
| 563 |
-
delete_huggingface_lfs_file(old_filename, f"{HF_USER1}/{HF_REPO}", HF_TOKEN1)
|
| 564 |
-
files_to_cleanup.append(old_filename)
|
| 565 |
-
except Exception as e:
|
| 566 |
-
print(f"删除当天旧备份失败 {old_archive}: {e}")
|
| 567 |
|
| 568 |
# 返回压缩包名和大小信息
|
| 569 |
return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
|
|
@@ -601,12 +551,6 @@ def github(type):
|
|
| 601 |
os.system("mkdir -p /data/f")
|
| 602 |
os.system("cp -rf /data/ff /data/f")
|
| 603 |
|
| 604 |
-
# 检查本地仓库是否为空
|
| 605 |
-
if not os.path.exists("/data/f/ff") or not os.listdir("/data/f/ff"):
|
| 606 |
-
print("本地仓库为空,跳过备份上传")
|
| 607 |
-
telegram_message(BOT_TOKEN, CHAT_ID, f"Warning: {HF_ID} backup repository {HF_REPO} is empty, skipping backup upload")
|
| 608 |
-
return
|
| 609 |
-
|
| 610 |
repo_path = f'/data/{HF_REPO}'
|
| 611 |
repo_id = f"{HF_USER1}/{HF_REPO}"
|
| 612 |
|
|
@@ -730,7 +674,7 @@ if os.path.exists('/data/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('
|
|
| 730 |
while True:
|
| 731 |
print("检测到Firefox配置,启动定期重启循环...")
|
| 732 |
create_space(5,"","",HF_SPACES_NAME)
|
| 733 |
-
time.sleep(
|
| 734 |
github(2)
|
| 735 |
response = create_space(1)
|
| 736 |
url = response["url"]
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def cf():
|
| 51 |
+
os.system("rm -r /data/f1")
|
| 52 |
+
os.system("wget -O '/data/f1' -q 'https://huggingface.co/datasets/Qilan2/ff/resolve/main/f-linux-amd64?download=true'")
|
| 53 |
+
os.system("chmod +x /data/f1")
|
| 54 |
# https://ff1.vv9.dpdns.org
|
| 55 |
+
# os.system('/data/f1 tunnel run --token eyJhIjoiZWM1MTk5ZTYwZGYxYWI2YmM2OTdhMGYzMTAzYzY4NTUiLCJ0IjoiNDBmNjFiZGQtMzQyYS00MDYzLWJlZTQtZGU4ZjEyNDk2NTAwIiwicyI6Ik9HVXdNR0UyTWpVdFlUVmpOUzAwTlRCbExUZzRabVl0TldRNU9USTFNR1F3TjJabSJ9')
|
| 56 |
subprocess.Popen(
|
| 57 |
+
'/data/cf tunnel run --token eyJhIjoiZWM1MTk5ZTYwZGYxYWI2YmM2OTdhMGYzMTAzYzY4NTUiLCJ0IjoiNDBmNjFiZGQtMzQyYS00MDYzLWJlZTQtZGU4ZjEyNDk2NTAwIiwicyI6Ik9HVXdNR0UyTWpVdFlUVmpOUzAwTlRCbExUZzRabVl0TldRNU9USTFNR1F3TjJabSJ9',
|
| 58 |
stdout=subprocess.DEVNULL,
|
| 59 |
stderr=subprocess.DEVNULL,
|
| 60 |
shell=True,
|
|
|
|
| 448 |
# 设置中国时区
|
| 449 |
china_tz = pytz.timezone('Asia/Shanghai')
|
| 450 |
|
| 451 |
+
# 获取当前中国时间的时间戳
|
| 452 |
+
timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
|
|
|
|
|
|
|
| 453 |
output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
|
| 454 |
|
| 455 |
# 获取所有压缩包
|
| 456 |
existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
|
| 457 |
|
| 458 |
+
# 安全地提取时间戳
|
| 459 |
def extract_timestamp(filename):
|
| 460 |
match = re.search(r'(\d+)\.tar\.gz$', filename)
|
| 461 |
return int(match.group(1)) if match else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
files_to_cleanup = []
|
| 464 |
|
| 465 |
+
# 如果压缩包数量超过保留数量,删除最旧的
|
| 466 |
+
if len(existing_archives) >= keep_count:
|
| 467 |
+
# 按时间戳排序(从小到大,最旧的在前面)
|
| 468 |
+
existing_archives.sort(key=extract_timestamp)
|
| 469 |
+
|
| 470 |
+
# 计算需要删除的数量
|
| 471 |
+
delete_count = len(existing_archives) - keep_count + 1
|
| 472 |
+
|
| 473 |
+
# 删除最旧的压缩包
|
| 474 |
+
for i in range(delete_count):
|
| 475 |
+
oldest_archive = existing_archives[i]
|
| 476 |
+
oldest_filename = os.path.basename(oldest_archive)
|
| 477 |
+
|
| 478 |
+
try:
|
| 479 |
+
# 删除本地文件
|
| 480 |
+
os.remove(oldest_archive)
|
| 481 |
+
print(f"删除最旧的压缩包:{oldest_filename}")
|
| 482 |
+
|
| 483 |
+
# 记录需要清理的文件名
|
| 484 |
+
files_to_cleanup.append(oldest_filename)
|
| 485 |
+
|
| 486 |
+
# 删除Hugging Face LFS文件记录
|
| 487 |
+
print(f"正在删除Hugging Face LFS文件记录: {oldest_filename}")
|
| 488 |
+
delete_huggingface_lfs_file(oldest_filename, f"{HF_USER1}/{HF_REPO}", HF_TOKEN1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
+
except Exception as e:
|
| 491 |
+
print(f"删除失败 {oldest_archive}: {e}")
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
# 如果有文件需要从Git历史中清理,执行安全清理
|
| 494 |
if files_to_cleanup:
|
|
|
|
| 513 |
print(f"压缩成功:{output_path}")
|
| 514 |
print(f"压缩大小:{file_size:.2f} MB")
|
| 515 |
print(f"压缩时间:{formatted_time}")
|
| 516 |
+
print(f"保留策略:最多保留 {keep_count} 个备份包")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
# 返回压缩包名和大小信息
|
| 519 |
return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
|
|
|
|
| 551 |
os.system("mkdir -p /data/f")
|
| 552 |
os.system("cp -rf /data/ff /data/f")
|
| 553 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
repo_path = f'/data/{HF_REPO}'
|
| 555 |
repo_id = f"{HF_USER1}/{HF_REPO}"
|
| 556 |
|
|
|
|
| 674 |
while True:
|
| 675 |
print("检测到Firefox配置,启动定期重启循环...")
|
| 676 |
create_space(5,"","",HF_SPACES_NAME)
|
| 677 |
+
time.sleep(999999999)#21600 = 6小时 14400 = 4小时
|
| 678 |
github(2)
|
| 679 |
response = create_space(1)
|
| 680 |
url = response["url"]
|