Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,27 +49,37 @@ def fetch_user_data(handle):
|
|
| 49 |
|
| 50 |
|
| 51 |
def fetch_and_save_data():
|
| 52 |
-
"""Fetch all kinds of data for every user and save to a file."""
|
| 53 |
users = fetch_all_users()
|
| 54 |
if not users:
|
| 55 |
return "Failed to fetch users. Check logs for errors."
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
for i, user in enumerate(users):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
user_data = fetch_user_data(user)
|
| 62 |
if user_data:
|
| 63 |
all_data[user] = user_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
time.sleep(0.5) # Respect API rate limit
|
| 65 |
except Exception as e:
|
| 66 |
print(f"Error fetching data for user {user}: {e}")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
| 70 |
-
json.dump(all_data, f, indent=4)
|
| 71 |
-
|
| 72 |
-
return f"Data fetching complete. Saved to {DATA_FILE}."
|
| 73 |
|
| 74 |
|
| 75 |
def serve_file():
|
|
@@ -88,15 +98,28 @@ def fetch_and_download():
|
|
| 88 |
return message, file_path
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
with gr.Blocks() as interface:
|
| 92 |
gr.Markdown("# Codeforces User Data Fetcher")
|
| 93 |
-
gr.Markdown("This tool fetches data for all users from Codeforces and provides a downloadable file.")
|
| 94 |
-
|
| 95 |
with gr.Row():
|
| 96 |
fetch_button = gr.Button("Fetch Data")
|
| 97 |
-
output_message = gr.Textbox(label="Status")
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
interface.launch(
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
def fetch_and_save_data():
|
| 52 |
+
"""Fetch all kinds of data for every user and save it to a file."""
|
| 53 |
users = fetch_all_users()
|
| 54 |
if not users:
|
| 55 |
return "Failed to fetch users. Check logs for errors."
|
| 56 |
|
| 57 |
+
if os.path.exists(DATA_FILE):
|
| 58 |
+
# Load existing data to resume
|
| 59 |
+
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
| 60 |
+
all_data = json.load(f)
|
| 61 |
+
else:
|
| 62 |
+
all_data = {}
|
| 63 |
|
| 64 |
for i, user in enumerate(users):
|
| 65 |
+
if user in all_data:
|
| 66 |
+
print(f"Skipping already fetched user: {user}")
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
try:
|
| 70 |
user_data = fetch_user_data(user)
|
| 71 |
if user_data:
|
| 72 |
all_data[user] = user_data
|
| 73 |
+
|
| 74 |
+
# Save progress to file after each user
|
| 75 |
+
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
| 76 |
+
json.dump(all_data, f, indent=4)
|
| 77 |
+
|
| 78 |
time.sleep(0.5) # Respect API rate limit
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error fetching data for user {user}: {e}")
|
| 81 |
|
| 82 |
+
return "Data fetching complete."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
def serve_file():
|
|
|
|
| 98 |
return message, file_path
|
| 99 |
|
| 100 |
|
| 101 |
+
def get_file_for_download():
|
| 102 |
+
"""Allow the user to download the current work anytime."""
|
| 103 |
+
file_path = serve_file()
|
| 104 |
+
if file_path:
|
| 105 |
+
return file_path
|
| 106 |
+
else:
|
| 107 |
+
return "No data file available."
|
| 108 |
+
|
| 109 |
+
|
| 110 |
with gr.Blocks() as interface:
|
| 111 |
gr.Markdown("# Codeforces User Data Fetcher")
|
| 112 |
+
gr.Markdown("This tool fetches data for all users from Codeforces and provides a downloadable file. You can fetch data and download progress anytime.")
|
| 113 |
+
|
| 114 |
with gr.Row():
|
| 115 |
fetch_button = gr.Button("Fetch Data")
|
| 116 |
+
output_message = gr.Textbox(label="Status", lines=2)
|
| 117 |
+
|
| 118 |
+
download_file_button = gr.Button("Download Current Data")
|
| 119 |
+
download_file_output = gr.File(label="Download Data", visible=False)
|
| 120 |
|
| 121 |
+
# Bind actions to buttons
|
| 122 |
+
fetch_button.click(fetch_and_download, inputs=[], outputs=[output_message, download_file_output])
|
| 123 |
+
download_file_button.click(get_file_for_download, inputs=[], outputs=download_file_output)
|
| 124 |
|
| 125 |
+
interface.launch()
|