laichai commited on
Commit
5a7b0c6
·
verified ·
1 Parent(s): aaf169f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
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
- # Place this with your other Helper Functions
 
80
  def pull_from_github():
81
  """
82
- Downloads the latest 'topics' folder from GitHub
83
- and overwrites local files.
84
  """
85
  try:
 
 
 
 
 
 
 
 
86
  g = Github(st.secrets["github_token"])
87
  repo = g.get_repo(st.secrets["github_repo"])
88
 
89
- # Get all files in the topics directory from GitHub
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
- # It's a file, download and overwrite locally
99
- if file_content.path.endswith(".tex"):
100
- # Ensure local directory exists
101
- os.makedirs(os.path.dirname(file_content.path), exist_ok=True)
102
-
103
- # Write content
104
- with open(file_content.path, "wb") as f:
105
- f.write(file_content.decoded_content)
106
- count += 1
 
 
107
 
108
- return True, f"Successfully pulled {count} files from GitHub!"
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