Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import os | |
| import requests | |
| from datetime import datetime | |
| COOKIE_FILE = "cookies.json" | |
| def save_cookies(cookies): | |
| with open(COOKIE_FILE, "w", encoding="utf-8") as f: | |
| json.dump(cookies, f, indent=2) | |
| def load_cookies(): | |
| if os.path.exists(COOKIE_FILE): | |
| with open(COOKIE_FILE, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| return [] | |
| def upload_cookies(file_path): | |
| try: | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| new_cookies = json.load(f) | |
| save_cookies(new_cookies) | |
| return "β Cookies uploaded!" | |
| except Exception as e: | |
| return f"β Upload error: {e}" | |
| def refresh_cookies(): | |
| try: | |
| cookie_list = load_cookies() | |
| if not cookie_list: | |
| return "β οΈ No cookies loaded." | |
| # Convert list of cookies to dict for requests | |
| cookies = {c["name"]: c["value"] for c in cookie_list if "name" in c and "value" in c} | |
| response = requests.get( | |
| "https://chatglm.cn/video?lang=zh", | |
| cookies=cookies, | |
| headers={ | |
| "User-Agent": "Mozilla/5.0", | |
| "Accept": "text/html" | |
| }, | |
| timeout=15 | |
| ) | |
| # Simple response check | |
| if response.status_code == 200: | |
| now = datetime.utcnow().isoformat() | |
| return f"π Refreshed at {now}" | |
| else: | |
| return f"β οΈ Server responded with status: {response.status_code}" | |
| except Exception as e: | |
| return f"β Refresh error: {e}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## πͺ ChatGLM Cookie Refresher (Requests Only)") | |
| with gr.Row(): | |
| file_input = gr.File(label="Upload your `cookies.json`", type="filepath") | |
| upload_btn = gr.Button("Upload Cookies") | |
| upload_output = gr.Textbox(label="Upload Result") | |
| upload_btn.click(upload_cookies, inputs=file_input, outputs=upload_output) | |
| with gr.Row(): | |
| refresh_btn = gr.Button("Send Request with Cookies") | |
| refresh_output = gr.Textbox(label="Refresh Result") | |
| refresh_btn.click(refresh_cookies, outputs=refresh_output) | |
| demo.launch() | |