Download app.py from Ahmad140/WinGo-Data-Collector: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/spaces/Ahmad140/WinGo-Data-Collector/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ahmad140/WinGo-Data-Collector/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ahmad140/WinGo-Data-Collector/resolve/main/app.py
2.73 kB
| import gradio as gr | |
| import threading | |
| import time | |
| import requests | |
| import pandas as pd | |
| import os | |
| import datetime | |
| HISTORY_URL = "https://draw.ar-lottery01.com/WinGo/WinGo_3M/GetHistoryIssuePage.json" | |
| CSV_PATH = "/data/wingo_data.csv" | |
| HEADERS = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', | |
| 'Referer': 'https://www.92pak8.com/', | |
| 'Origin': 'https://www.92pak8.com', | |
| } | |
| def fetch_and_save(): | |
| try: | |
| print(f"[{datetime.datetime.now()}] Fetching history...") | |
| resp = requests.get(HISTORY_URL, headers=HEADERS, timeout=15) | |
| if resp.status_code != 200: | |
| print(f"HTTP {resp.status_code}") | |
| return | |
| data = resp.json() | |
| items = data.get('data', {}).get('list', []) | |
| if not items: | |
| print("No items") | |
| return | |
| print(f"Fetched {len(items)} records") | |
| # Load existing | |
| if os.path.exists(CSV_PATH): | |
| df_old = pd.read_csv(CSV_PATH) | |
| else: | |
| df_old = pd.DataFrame(columns=['period','number','category','timestamp']) | |
| new_records = [] | |
| for it in items: | |
| period = it.get('issueNumber') | |
| num = it.get('number') | |
| if period and num is not None: | |
| num = int(num) | |
| cat = 0 if num <= 4 else 1 | |
| new_records.append({ | |
| 'period': str(period), | |
| 'number': num, | |
| 'category': cat, | |
| 'timestamp': datetime.datetime.now().isoformat() | |
| }) | |
| df_new = pd.DataFrame(new_records) | |
| df_combined = pd.concat([df_old, df_new], ignore_index=True) | |
| df_combined = df_combined.drop_duplicates(subset=['period'], keep='last') | |
| os.makedirs("/data", exist_ok=True) | |
| df_combined.to_csv(CSV_PATH, index=False) | |
| print(f"Saved {len(df_combined)} total records") | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| def background_collector(): | |
| fetch_and_save() # immediate on startup | |
| while True: | |
| time.sleep(3600) # every hour | |
| fetch_and_save() | |
| thread = threading.Thread(target=background_collector, daemon=True) | |
| thread.start() | |
| def show_data(): | |
| if not os.path.exists(CSV_PATH): | |
| return "No data yet. First fetch in progress..." | |
| df = pd.read_csv(CSV_PATH) | |
| return df.tail(50).to_html() | |
| with gr.Blocks(title="WinGo Data Collector") as demo: | |
| gr.Markdown("# WinGo 3‑Minute History Collector") | |
| gr.Markdown("Background thread fetches every hour. Data saved in /data/wingo_data.csv") | |
| refresh_btn = gr.Button("Refresh") | |
| output = gr.HTML(value=show_data()) | |
| refresh_btn.click(fn=show_data, outputs=output) | |
| demo.launch() |