Spaces:
Sleeping
Sleeping
| import requests | |
| import gradio as gr | |
| from datetime import datetime | |
| # Reliable public API (when active; community-hosted) | |
| API_URL = "https://gagstock.gleeze.com/grow-a-garden" | |
| def fetch_stock(): | |
| try: | |
| response = requests.get(API_URL, timeout=15) | |
| response.raise_for_status() | |
| data = response.json() | |
| if data.get("status") != "success": | |
| return {"error": "API returned non-success status."} | |
| seeds_data = data.get("data", {}).get("seed", {}).get("items", []) | |
| if not seeds_data: | |
| return {"error": "No seed data found in API response."} | |
| return seeds_data | |
| except requests.exceptions.ConnectionError: | |
| return {"error": "Cannot connect to API (server down). Use live trackers like growagardenstock.org or growagardenpro.com."} | |
| except Exception as e: | |
| return {"error": f"API error: {str(e)}. Check growagardenstock.org or vulcanvalues.com/grow-a-garden/stock for live data."} | |
| def check_seeds(desired_seeds): | |
| stock_data = fetch_stock() | |
| if isinstance(stock_data, dict) and "error" in stock_data: | |
| return stock_data["error"], "" | |
| desired_list = [seed.strip().lower() for seed in desired_seeds.split(",") if seed.strip()] | |
| table = "| Seed Name | Stock |\n|-----------|-------|\n" | |
| matches = [] | |
| for item in stock_data: | |
| seed_name = item.get('name', 'Unknown') | |
| stock = item.get('quantity', 0) | |
| table += f"| {seed_name} | {stock} |\n" | |
| if seed_name.lower() in desired_list and stock > 0: | |
| matches.append(f"π **{seed_name} is IN STOCK! ({stock} available)** π") | |
| notification = "\n".join(matches) if matches else "No matching seeds in stock right now. Check back soon!" | |
| notification += f"\n\nLast updated: {datetime.now().strftime('%H:%M:%S')} UTC" | |
| return table, notification | |
| with gr.Blocks(title="Grow A Garden Seed Notifier π±", js=""" | |
| () => { | |
| setInterval(() => { | |
| document.querySelector('button#refresh_button').click(); | |
| }, 60000); // Auto-refresh every 60 seconds | |
| } | |
| """) as demo: | |
| gr.Markdown("# Grow A Garden Seed Notifier π±") | |
| gr.Markdown("Enter seeds you're hunting (comma-separated). Auto-refreshes **every 60 seconds**!") | |
| desired_input = gr.Textbox( | |
| label="Desired Seeds", | |
| placeholder="e.g. Cocoa, Dragon Fruit, Moon Blossom, Sugar Apple", | |
| value="Cocoa, Dragon Fruit, Moon Blossom" | |
| ) | |
| stock_output = gr.Markdown(label="Current Seed Stock Table") | |
| notification_output = gr.Markdown(label="π Alerts") | |
| refresh_button = gr.Button("Manual Refresh Now", elem_id="refresh_button") | |
| refresh_button.click( | |
| fn=check_seeds, | |
| inputs=desired_input, | |
| outputs=[stock_output, notification_output] | |
| ) | |
| demo.load( | |
| fn=check_seeds, | |
| inputs=desired_input, | |
| outputs=[stock_output, notification_output] | |
| ) | |
| demo.launch() |