Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import subprocess
|
| 4 |
from streamlit_pdf_viewer import pdf_viewer
|
| 5 |
from github import Github
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
TOPICS_DIR = "topics"
|
|
@@ -76,36 +77,47 @@ def push_to_github(local_path, content, commit_message):
|
|
| 76 |
except Exception as e:
|
| 77 |
return False, f"GitHub Error: {str(e)}"
|
| 78 |
|
| 79 |
-
|
|
|
|
| 80 |
def pull_from_github():
|
| 81 |
"""
|
| 82 |
-
|
| 83 |
-
|
| 84 |
"""
|
| 85 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
g = Github(st.secrets["github_token"])
|
| 87 |
repo = g.get_repo(st.secrets["github_repo"])
|
| 88 |
|
| 89 |
-
#
|
| 90 |
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
| 91 |
-
|
| 92 |
count = 0
|
|
|
|
| 93 |
while contents:
|
| 94 |
file_content = contents.pop(0)
|
| 95 |
if file_content.type == "dir":
|
| 96 |
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 97 |
else:
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
return True, f"
|
| 109 |
except Exception as e:
|
| 110 |
return False, str(e)
|
| 111 |
|
|
|
|
| 3 |
import subprocess
|
| 4 |
from streamlit_pdf_viewer import pdf_viewer
|
| 5 |
from github import Github
|
| 6 |
+
import shutil # Make sure this is imported at the top
|
| 7 |
|
| 8 |
# --- Configuration ---
|
| 9 |
TOPICS_DIR = "topics"
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return False, f"GitHub Error: {str(e)}"
|
| 79 |
|
| 80 |
+
|
| 81 |
+
|
| 82 |
def pull_from_github():
|
| 83 |
"""
|
| 84 |
+
1. Wipes the local 'topics' folder.
|
| 85 |
+
2. Downloads a fresh copy from GitHub.
|
| 86 |
"""
|
| 87 |
try:
|
| 88 |
+
# 1. WIPE LOCAL FOLDER CLEAN
|
| 89 |
+
if os.path.exists(TOPICS_DIR):
|
| 90 |
+
shutil.rmtree(TOPICS_DIR) # Deletes the folder and everything inside
|
| 91 |
+
|
| 92 |
+
# Re-create the empty folder
|
| 93 |
+
os.makedirs(TOPICS_DIR, exist_ok=True)
|
| 94 |
+
|
| 95 |
+
# 2. CONNECT TO GITHUB
|
| 96 |
g = Github(st.secrets["github_token"])
|
| 97 |
repo = g.get_repo(st.secrets["github_repo"])
|
| 98 |
|
| 99 |
+
# 3. DOWNLOAD EVERYTHING
|
| 100 |
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
|
|
|
| 101 |
count = 0
|
| 102 |
+
|
| 103 |
while contents:
|
| 104 |
file_content = contents.pop(0)
|
| 105 |
if file_content.type == "dir":
|
| 106 |
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 107 |
else:
|
| 108 |
+
# Calculate where to save it locally
|
| 109 |
+
# We strip the TOPICS_DIR prefix from the GitHub path to ensure correct nesting
|
| 110 |
+
local_path = file_content.path
|
| 111 |
+
|
| 112 |
+
# Ensure the subfolder exists
|
| 113 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 114 |
+
|
| 115 |
+
# Write the file
|
| 116 |
+
with open(local_path, "wb") as f:
|
| 117 |
+
f.write(file_content.decoded_content)
|
| 118 |
+
count += 1
|
| 119 |
|
| 120 |
+
return True, f"Clean sync complete! Downloaded {count} files."
|
| 121 |
except Exception as e:
|
| 122 |
return False, str(e)
|
| 123 |
|