Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,49 @@ import shutil # Make sure this is imported at the top
|
|
| 9 |
TOPICS_DIR = "topics"
|
| 10 |
TEMP_DIR = "temp_build"
|
| 11 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Auto-load files if the container restarted
|
| 13 |
if not os.path.exists(TOPICS_DIR) or not os.listdir(TOPICS_DIR):
|
| 14 |
print("System restart detected. Auto-pulling files from GitHub...")
|
|
@@ -83,47 +126,7 @@ def push_to_github(local_path, content, commit_message):
|
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
-
def pull_from_github():
|
| 87 |
-
"""
|
| 88 |
-
1. Wipes the local 'topics' folder.
|
| 89 |
-
2. Downloads a fresh copy from GitHub.
|
| 90 |
-
"""
|
| 91 |
-
try:
|
| 92 |
-
# 1. WIPE LOCAL FOLDER CLEAN
|
| 93 |
-
if os.path.exists(TOPICS_DIR):
|
| 94 |
-
shutil.rmtree(TOPICS_DIR) # Deletes the folder and everything inside
|
| 95 |
-
|
| 96 |
-
# Re-create the empty folder
|
| 97 |
-
os.makedirs(TOPICS_DIR, exist_ok=True)
|
| 98 |
|
| 99 |
-
# 2. CONNECT TO GITHUB
|
| 100 |
-
g = Github(st.secrets["github_token"])
|
| 101 |
-
repo = g.get_repo(st.secrets["github_repo"])
|
| 102 |
-
|
| 103 |
-
# 3. DOWNLOAD EVERYTHING
|
| 104 |
-
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
| 105 |
-
count = 0
|
| 106 |
-
|
| 107 |
-
while contents:
|
| 108 |
-
file_content = contents.pop(0)
|
| 109 |
-
if file_content.type == "dir":
|
| 110 |
-
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 111 |
-
else:
|
| 112 |
-
# Calculate where to save it locally
|
| 113 |
-
# We strip the TOPICS_DIR prefix from the GitHub path to ensure correct nesting
|
| 114 |
-
local_path = file_content.path
|
| 115 |
-
|
| 116 |
-
# Ensure the subfolder exists
|
| 117 |
-
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 118 |
-
|
| 119 |
-
# Write the file
|
| 120 |
-
with open(local_path, "wb") as f:
|
| 121 |
-
f.write(file_content.decoded_content)
|
| 122 |
-
count += 1
|
| 123 |
-
|
| 124 |
-
return True, f"Clean sync complete! Downloaded {count} files."
|
| 125 |
-
except Exception as e:
|
| 126 |
-
return False, str(e)
|
| 127 |
|
| 128 |
# ==========================================
|
| 129 |
# 🚀 MAIN APP LOGIC
|
|
|
|
| 9 |
TOPICS_DIR = "topics"
|
| 10 |
TEMP_DIR = "temp_build"
|
| 11 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
def pull_from_github():
|
| 14 |
+
"""
|
| 15 |
+
1. Wipes the local 'topics' folder.
|
| 16 |
+
2. Downloads a fresh copy from GitHub.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# 1. WIPE LOCAL FOLDER CLEAN
|
| 20 |
+
if os.path.exists(TOPICS_DIR):
|
| 21 |
+
shutil.rmtree(TOPICS_DIR) # Deletes the folder and everything inside
|
| 22 |
+
|
| 23 |
+
# Re-create the empty folder
|
| 24 |
+
os.makedirs(TOPICS_DIR, exist_ok=True)
|
| 25 |
+
|
| 26 |
+
# 2. CONNECT TO GITHUB
|
| 27 |
+
g = Github(st.secrets["github_token"])
|
| 28 |
+
repo = g.get_repo(st.secrets["github_repo"])
|
| 29 |
+
|
| 30 |
+
# 3. DOWNLOAD EVERYTHING
|
| 31 |
+
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
| 32 |
+
count = 0
|
| 33 |
+
|
| 34 |
+
while contents:
|
| 35 |
+
file_content = contents.pop(0)
|
| 36 |
+
if file_content.type == "dir":
|
| 37 |
+
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 38 |
+
else:
|
| 39 |
+
# Calculate where to save it locally
|
| 40 |
+
# We strip the TOPICS_DIR prefix from the GitHub path to ensure correct nesting
|
| 41 |
+
local_path = file_content.path
|
| 42 |
+
|
| 43 |
+
# Ensure the subfolder exists
|
| 44 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 45 |
+
|
| 46 |
+
# Write the file
|
| 47 |
+
with open(local_path, "wb") as f:
|
| 48 |
+
f.write(file_content.decoded_content)
|
| 49 |
+
count += 1
|
| 50 |
+
|
| 51 |
+
return True, f"Clean sync complete! Downloaded {count} files."
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return False, str(e)
|
| 54 |
+
|
| 55 |
# Auto-load files if the container restarted
|
| 56 |
if not os.path.exists(TOPICS_DIR) or not os.listdir(TOPICS_DIR):
|
| 57 |
print("System restart detected. Auto-pulling files from GitHub...")
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# ==========================================
|
| 132 |
# 🚀 MAIN APP LOGIC
|