awacke1 commited on
Commit
acbd0bd
·
1 Parent(s): 9e2fd8c

Update backup.py

Browse files
Files changed (1) hide show
  1. backup.py +29 -22
backup.py CHANGED
@@ -8,8 +8,12 @@ import hashlib
8
  import json
9
 
10
  EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
 
 
 
 
 
11
 
12
- # Create a history.json file if it doesn't exist yet
13
  if not os.path.exists("history.json"):
14
  with open("history.json", "w") as f:
15
  json.dump({}, f)
@@ -35,12 +39,10 @@ def download_html_and_files(url, subdir):
35
  file_url = urllib.parse.urljoin(base_url, link.get('href'))
36
  local_filename = os.path.join(subdir, urllib.parse.urlparse(file_url).path.split('/')[-1])
37
 
38
- # Skip if the local filename is a directory
39
  if not local_filename.endswith('/') and local_filename != subdir:
40
  link['href'] = local_filename
41
  download_file(file_url, local_filename)
42
 
43
- # Save the modified HTML content
44
  with open(os.path.join(subdir, "index.html"), "w") as file:
45
  file.write(str(soup))
46
 
@@ -48,24 +50,28 @@ def list_files(directory_path='.'):
48
  files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
49
  return [f for f in files if f not in EXCLUDED_FILES]
50
 
51
-
52
  def show_file_operations(file_path):
53
  st.write(f"File: {os.path.basename(file_path)}")
54
-
55
- # Edit button
56
- if st.button(f"✏️ Edit {os.path.basename(file_path)}"):
57
- with open(file_path, "r") as f:
58
- file_content = f.read()
59
- file_content = st.text_area("Edit the file content:", value=file_content, height=250)
60
- if st.button(f"💾 Save {os.path.basename(file_path)}"):
 
 
 
 
 
61
  with open(file_path, "w") as f:
62
  f.write(file_content)
63
- st.success(f"File {os.path.basename(file_path)} saved!")
64
 
65
- # Delete button
66
- if st.button(f"🗑️ Delete {os.path.basename(file_path)}"):
67
- os.remove(file_path)
68
- st.markdown(f"🎉 File {os.path.basename(file_path)} deleted!")
69
 
70
  def show_download_links(subdir):
71
  st.write(f'Files for {subdir}:')
@@ -77,7 +83,6 @@ def show_download_links(subdir):
77
  else:
78
  st.write(f"File not found: {file}")
79
 
80
-
81
  def get_download_link(file):
82
  with open(file, "rb") as f:
83
  bytes = f.read()
@@ -85,16 +90,19 @@ def get_download_link(file):
85
  href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
86
  return href
87
 
88
-
89
  def main():
90
  st.sidebar.title('Web Datasets Bulk Downloader')
91
- url = st.sidebar.text_input('Please enter a Web URL to bulk download text and files')
 
 
 
 
 
 
92
 
93
- # Load history
94
  with open("history.json", "r") as f:
95
  history = json.load(f)
96
 
97
- # Save the history of URL entered as a json file
98
  if url:
99
  subdir = hashlib.md5(url.encode()).hexdigest()
100
  if not os.path.exists(subdir):
@@ -112,7 +120,6 @@ def main():
112
  for subdir in history.values():
113
  show_download_links(subdir)
114
 
115
- # Display history as markdown
116
  with st.expander("URL History and Downloaded Files"):
117
  for url, subdir in history.items():
118
  st.markdown(f"#### {url}")
 
8
  import json
9
 
10
  EXCLUDED_FILES = ['app.py', 'requirements.txt', 'pre-requirements.txt', 'packages.txt', 'README.md','.gitattributes', "backup.py","Dockerfile"]
11
+ URLS = {
12
+ "Chordify - Play Along Chords": "https://chordify.net/",
13
+ "National Guitar Academy - Guitar Learning": "https://www.guitaracademy.com/",
14
+ "Ultimate Guitar - Massive Song Database": "https://www.ultimate-guitar.com/",
15
+ }
16
 
 
17
  if not os.path.exists("history.json"):
18
  with open("history.json", "w") as f:
19
  json.dump({}, f)
 
39
  file_url = urllib.parse.urljoin(base_url, link.get('href'))
40
  local_filename = os.path.join(subdir, urllib.parse.urlparse(file_url).path.split('/')[-1])
41
 
 
42
  if not local_filename.endswith('/') and local_filename != subdir:
43
  link['href'] = local_filename
44
  download_file(file_url, local_filename)
45
 
 
46
  with open(os.path.join(subdir, "index.html"), "w") as file:
47
  file.write(str(soup))
48
 
 
50
  files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
51
  return [f for f in files if f not in EXCLUDED_FILES]
52
 
 
53
  def show_file_operations(file_path):
54
  st.write(f"File: {os.path.basename(file_path)}")
55
+ unique_key = hashlib.md5(file_path.encode()).hexdigest()
56
+ col1, col2, col3 = st.columns(3)
57
+
58
+ with col1:
59
+ if st.button(f"✏️ Edit", key=f"edit_{unique_key}"):
60
+ file_content = ""
61
+ with open(file_path, "r") as f:
62
+ file_content = f.read()
63
+ file_content = st.text_area("Edit the file content:", value=file_content, height=250, key=f"text_area_{unique_key}")
64
+
65
+ with col2:
66
+ if st.button(f"💾 Save", key=f"save_{unique_key}"):
67
  with open(file_path, "w") as f:
68
  f.write(file_content)
69
+ st.success(f"File saved!")
70
 
71
+ with col3:
72
+ if st.button(f"🗑️ Delete", key=f"delete_{unique_key}"):
73
+ os.remove(file_path)
74
+ st.markdown(f"File deleted!")
75
 
76
  def show_download_links(subdir):
77
  st.write(f'Files for {subdir}:')
 
83
  else:
84
  st.write(f"File not found: {file}")
85
 
 
86
  def get_download_link(file):
87
  with open(file, "rb") as f:
88
  bytes = f.read()
 
90
  href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
91
  return href
92
 
 
93
  def main():
94
  st.sidebar.title('Web Datasets Bulk Downloader')
95
+ url_input_method = st.sidebar.radio("Choose URL Input Method", ["Enter URL", "Select from List"])
96
+ url = ""
97
+ if url_input_method == "Enter URL":
98
+ url = st.sidebar.text_input('Please enter a Web URL to bulk download text and files')
99
+ else:
100
+ selected_site = st.sidebar.selectbox("Select a Website", list(URLS.keys()))
101
+ url = URLS[selected_site]
102
 
 
103
  with open("history.json", "r") as f:
104
  history = json.load(f)
105
 
 
106
  if url:
107
  subdir = hashlib.md5(url.encode()).hexdigest()
108
  if not os.path.exists(subdir):
 
120
  for subdir in history.values():
121
  show_download_links(subdir)
122
 
 
123
  with st.expander("URL History and Downloaded Files"):
124
  for url, subdir in history.items():
125
  st.markdown(f"#### {url}")