| import subprocess |
| import os |
| import threading |
| import time |
| import yaml |
| from datetime import datetime |
| import signal |
| import psutil |
| import glob |
| import re |
| import pytz |
| import requests |
| import json |
| import random |
| import string |
| import urllib3 |
|
|
| |
| DATA_JSON =''' { |
| "BACKUP_TIME": "1200", |
| "HF_USER1": "qilanqi", |
| "HF_REPO": "ff2", |
| "HF_EMAIL": "HermanBrand@mffac.com", |
| "HF_TOKEN1": "hf_vFdlQdHyRONKbniDwsukVaTe", |
| "HF_USER2": "cdddwwer", |
| "HF_EMAIL2": "HermanNorris@mffac.com", |
| "HF_ID": "dasdw", |
| "HF_TOKON2": "hf_lSPmCUGQHIHYYcgF", |
| "UUID": "a488076d-0ced-4a83-91c4-c498fce00cff", |
| "N_SERVER": "z.282820.xyz:443", |
| "CHAT_ID": "-4829459058", |
| "BOT_TOKEN": "8259739796:AAGZY4tboUxJ3jnMi1GTpGtV3_-Tf2rMT7I" |
| } |
| ''' |
| ff_url = "https://ff1.vv9.dpdns.org/" |
| HF_SPACES_NAME = os.environ.get('HF_SPACES_NAME', '') |
| def assign_vars_from_json(config: dict): |
| for key, value in config.items(): |
| globals()[key] = value |
|
|
| if DATA_JSON: |
| try: |
| config_dict = json.loads(DATA_JSON) |
| assign_vars_from_json(config_dict) |
| except json.JSONDecodeError: |
| raise ValueError("DATA_JSON 环境变量不是合法的 JSON 字符串") |
| else: |
| BACKUP_TIME = os.environ.get('BACKUP_TIME', '1200') |
| HF_USER1 = os.environ.get('HF_USER1', '') |
| HF_REPO = os.environ.get('HF_REPO', '') |
| HF_EMAIL = os.environ.get('HF_EMAIL', '') |
| HF_TOKEN1 = os.environ.get('HF_TOKEN1', '') |
| |
| HF_USER2 = os.environ.get('HF_USER2', '') |
| HF_ID = os.environ.get('HF_ID', '') |
| HF_TOKON2 = os.environ.get('HF_TOKON2', '') |
| HF_EMAIL2 = os.environ.get('HF_EMAIL2', '') |
| |
| UUID = os.environ.get('UUID', 'a488076d-0ced-4a83-91c4-c498fce00cff') |
| N_SERVER = os.environ.get('N_SERVER', 'z.282820.xyz:443') |
|
|
| CHAT_ID = os.environ.get('CHAT_ID', '') |
| BOT_TOKEN = os.environ.get('BOT_TOKEN', '') |
|
|
| def check_url_status(url, timeout=5): |
| """ |
| 检查URL是否可以正常访问 |
| |
| 参数: |
| url (str): 要检查的URL地址 |
| timeout (int): 请求超时时间,默认为5秒 |
| |
| 返回: |
| bool: 网站是否可以正常访问 |
| """ |
| try: |
| |
| response = requests.get(url, timeout=timeout) |
| |
| |
| return 200 <= response.status_code < 300 |
| |
| except requests.RequestException: |
| |
| return False |
|
|
| |
|
|
|
|
| def random_name(length=8): |
| return ''.join(random.choices(string.ascii_lowercase, k=length)) |
|
|
| def update_value(data: dict, key: str, new_value: str) -> dict: |
| if key in data: |
| data[key] = new_value |
| else: |
| print(f"!!! Key '{key}' ON") |
| return data |
| def telegram_message(bot_token: str, chat_id: str, message: str): |
| try: |
| |
| def escape_markdown(text): |
| escape_chars = ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'] |
| return ''.join('\\' + char if char in escape_chars else char for char in text) |
| |
| |
| escaped_message = escape_markdown(message) |
| |
| url = f"https://tgbotapi.9.c.5.b.0.d.0.0.1.0.a.2.ip6.arpa/bot{bot_token}/sendMessage" |
| |
| |
| params = { |
| "chat_id": chat_id, |
| "text": escaped_message, |
| "parse_mode": "MarkdownV2" |
| } |
| |
| |
| urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| |
| |
| response = requests.post( |
| url, |
| params=params, |
| verify=False, |
| timeout=10 |
| ) |
| |
| |
| print(f"Response Status Code: {response.status_code}") |
| print(f"Response Content: {response.text}") |
| |
| |
| if response.status_code == 200: |
| return response.json() |
| else: |
| return { |
| "status_code": response.status_code, |
| "text": response.text |
| } |
| |
| except requests.exceptions.RequestException as e: |
| print(f"请求异常: {e}") |
| return {"error": str(e)} |
| except Exception as e: |
| print(f"未知异常: {e}") |
| return {"error": str(e)} |
|
|
| def create_space(type,key = "", value = "",name = ""): |
| if type == 1: |
| |
| url = "https://huggingface.co/api/repos/create" |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {HF_TOKON2}" |
| } |
| name = random_name() |
| payload = { |
| "name": name, |
| "type": "space", |
| "private": True, |
| "sleepTimeSeconds": 172800, |
| |
| "sdk": "docker" |
| } |
| |
| response = requests.post(url, headers=headers, json=payload) |
| return response.json() |
| if type == 2: |
| |
| url = "https://huggingface.co/api/repos/delete" |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {HF_TOKON2}" |
| } |
| payload = { |
| "organization": HF_USER2, |
| "name": name, |
| "type": "space" |
| } |
| response = requests.delete(url, headers=headers, json=payload) |
| return response.text |
| if type == 3: |
| |
| url = f"https://huggingface.co/api/spaces/{HF_USER2}/{name}/secrets" |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {HF_TOKON2}" |
| } |
| payload = { |
| "key": key, |
| "value": value, |
| "description": "" |
| } |
| response = requests.post(url, headers=headers, json=payload) |
| return response.text |
| if type == 4: |
| file_path = f"/workdir/{name}/Dockerfile" |
| os.system("rm -rf {HF_SPACES_NAME}") |
| git = f"git clone https://{HF_USER2}:{HF_TOKON2}@huggingface.co/spaces/{HF_USER2}/{name} /workdir/{name}" |
| url = "https://huggingface.co/datasets/Qilan2/ff/raw/main/Dockerfile" |
| print(git) |
| os.system(git) |
| os.system(f'git config --global user.email "{HF_EMAIL2}"') |
| os.system(f'git config --global user.name "{HF_USER2}"') |
| os.chdir(f'/workdir/{name}') |
| os.system(f"rm -rf {file_path}") |
| time.sleep(3) |
| |
| if os.path.exists(file_path): |
| print(f"文件已存在,删除: {file_path}") |
| os.remove(file_path) |
| print(f"正在下载 {url} 到 {file_path}") |
| response = requests.get(url) |
| if response.status_code == 200: |
| os.makedirs(os.path.dirname(file_path), exist_ok=True) |
| with open(file_path, "wb") as f: |
| f.write(response.content) |
| print("下载完成 ✅") |
| |
| else: |
| print(f"下载失败,状态码: {response.status_code}") |
| |
| |
| |
| repo_path = f"//workdir/{name}" |
| subprocess.run(["git", "-C", repo_path, "add", "."], check=True) |
| subprocess.run(["git", "-C", repo_path, "commit", "-m", "0"], check=True) |
| subprocess.run(["git", "-C", repo_path, "push", "origin", "main"], check=True) |
| |
| |
| |
| |
| |
| |
| |
| if type == 5: |
| |
| headers = { |
| "Authorization": f"Bearer {HF_TOKON2}" |
| } |
| |
| |
| response = requests.get(f"https://huggingface.co/api/spaces?author={HF_USER2}", headers=headers) |
| |
| if response.status_code != 200: |
| print(f"获取空间列表失败:{response.status_code}") |
| return |
| |
| spaces = response.json() |
| |
| |
| spaces_sorted = sorted(spaces, key=lambda x: x['createdAt'], reverse=True) |
| |
| |
| if len(spaces_sorted) > 1: |
| latest_space = spaces_sorted[0] |
| |
| |
| for space in spaces_sorted[1:]: |
| full_name = space['id'] |
| space_id = full_name.split("/")[-1] |
| |
| url = "https://huggingface.co/api/repos/delete" |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {HF_TOKON2}" |
| } |
| payload = { |
| "organization": HF_USER2, |
| "name": space_id, |
| "type": "space" |
| } |
| delete_response = requests.delete(url, headers=headers, json=payload) |
| if delete_response.status_code == 200: |
| print(f"成功删除空间:{space_id}") |
| telegram_message(BOT_TOKEN, CHAT_ID, f"delete={space_id}=OK") |
| else: |
| print(f"删除空间 {space_id} 失败:{delete_response.status_code}") |
| telegram_message(BOT_TOKEN, CHAT_ID, f"delete={space_id}=ON") |
| def _reconstruct_token(partial_token): |
| return partial_token.replace(" ", "") |
| def restart_huggingface_space(space_name, space_id, partial_token): |
| token = _reconstruct_token(partial_token) |
| url = f"https://huggingface.co/api/spaces/{space_name}/{space_id}/restart?factory=true" |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {token}", |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36" |
| } |
| try: |
| response = requests.post(url, headers=headers, json={}) |
| return { |
| "status_code": response.status_code, |
| "success": response.status_code == 200, |
| "message": response.text |
| } |
| except requests.RequestException as e: |
| return { |
| "status_code": None, |
| "success": False, |
| "message": str(e) |
| } |
|
|
| def run(): |
| response = create_space(1) |
| url = response["url"] |
| full_name = response["name"] |
| tmp_name = full_name.split("/")[-1] |
| log = f"“原始创建Space返回:{response} name字段:{full_name} Space 名称{tmp_name}" |
| |
| telegram_message(BOT_TOKEN, CHAT_ID, log) |
| print(log) |
| create_space(3,"BACKUP_TIME",BACKUP_TIME,tmp_name) |
| create_space(3,"HF_USER1",HF_USER1,tmp_name) |
| create_space(3,"HF_REPO",HF_REPO,tmp_name) |
| create_space(3,"HF_EMAIL",HF_EMAIL,tmp_name) |
| create_space(3,"HF_TOKEN1",HF_TOKEN1,tmp_name) |
| create_space(3,"HF_USER2",HF_USER2,tmp_name) |
| create_space(3,"HF_EMAIL2",HF_EMAIL2,tmp_name) |
| create_space(3,"HF_ID",tmp_name,tmp_name) |
| create_space(3,"HF_TOKON2",HF_TOKON2,tmp_name) |
| create_space(3,"UUID",UUID,tmp_name) |
| create_space(3,"N_SERVER","z.282820.xyz:443",tmp_name) |
| create_space(3,"CHAT_ID",CHAT_ID,tmp_name) |
| create_space(3,"BOT_TOKEN",BOT_TOKEN,tmp_name) |
| create_space(3,"HF_SPACES_NAME",HF_ID,tmp_name) |
| time.sleep(2) |
| create_space(4,"","",tmp_name) |
| time.sleep(2) |
| result = restart_huggingface_space(HF_USER2, tmp_name, HF_TOKON2) |
| print(f"{tmp_name}重启结果:",result) |
| telegram_message(BOT_TOKEN, CHAT_ID, f"{HF_ID} 重启结果:{result}") |
| os.system(f"rm -rf /workdir/{tmp_name}") |
| is_accessible = check_url_status(ff_url) |
| print(f"网站 {ff_url} 是否可访问: {is_accessible}") |
|
|
| while True: |
| time.sleep(1200) |
| telegram_message(BOT_TOKEN, CHAT_ID, f"网站 {url} 是否可访问: {is_accessible}") |
| is_accessible = check_url_status(ff_url) |
| print(f"网站 {url} 是否可访问: {is_accessible}") |
| if not is_accessible: |
| run() |