awacke1 commited on
Commit
6826ec7
·
1 Parent(s): 15c1de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -26
app.py CHANGED
@@ -12,10 +12,8 @@ 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
- # ... Add other URLs here
16
  }
17
 
18
- # Create a history.json file if it doesn't exist yet
19
  if not os.path.exists("history.json"):
20
  with open("history.json", "w") as f:
21
  json.dump({}, f)
@@ -41,12 +39,10 @@ def download_html_and_files(url, subdir):
41
  file_url = urllib.parse.urljoin(base_url, link.get('href'))
42
  local_filename = os.path.join(subdir, urllib.parse.urlparse(file_url).path.split('/')[-1])
43
 
44
- # Skip if the local filename is a directory
45
  if not local_filename.endswith('/') and local_filename != subdir:
46
  link['href'] = local_filename
47
  download_file(file_url, local_filename)
48
 
49
- # Save the modified HTML content
50
  with open(os.path.join(subdir, "index.html"), "w") as file:
51
  file.write(str(soup))
52
 
@@ -54,24 +50,28 @@ def list_files(directory_path='.'):
54
  files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
55
  return [f for f in files if f not in EXCLUDED_FILES]
56
 
57
-
58
  def show_file_operations(file_path):
59
  st.write(f"File: {os.path.basename(file_path)}")
60
-
61
- # Edit button
62
- if st.button(f"✏️ Edit {os.path.basename(file_path)}"):
63
- with open(file_path, "r") as f:
64
- file_content = f.read()
65
- file_content = st.text_area("Edit the file content:", value=file_content, height=250)
66
- if st.button(f"💾 Save {os.path.basename(file_path)}"):
 
 
 
 
 
67
  with open(file_path, "w") as f:
68
  f.write(file_content)
69
- st.success(f"File {os.path.basename(file_path)} saved!")
70
 
71
- # Delete button
72
- if st.button(f"🗑️ Delete {os.path.basename(file_path)}"):
73
- os.remove(file_path)
74
- st.markdown(f"🎉 File {os.path.basename(file_path)} deleted!")
75
 
76
  def show_download_links(subdir):
77
  st.write(f'Files for {subdir}:')
@@ -83,7 +83,6 @@ def show_download_links(subdir):
83
  else:
84
  st.write(f"File not found: {file}")
85
 
86
-
87
  def get_download_link(file):
88
  with open(file, "rb") as f:
89
  bytes = f.read()
@@ -91,25 +90,19 @@ def get_download_link(file):
91
  href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Click to download {os.path.basename(file)}</a>'
92
  return href
93
 
94
-
95
  def main():
96
  st.sidebar.title('Web Datasets Bulk Downloader')
97
-
98
- # Option to choose between entering URL or selecting from dropdown
99
  url_input_method = st.sidebar.radio("Choose URL Input Method", ["Enter URL", "Select from List"])
100
-
101
  url = ""
102
  if url_input_method == "Enter URL":
103
  url = st.sidebar.text_input('Please enter a Web URL to bulk download text and files')
104
  else:
105
  selected_site = st.sidebar.selectbox("Select a Website", list(URLS.keys()))
106
  url = URLS[selected_site]
107
-
108
- # Load history
109
  with open("history.json", "r") as f:
110
  history = json.load(f)
111
 
112
- # Save the history of URL entered as a json file
113
  if url:
114
  subdir = hashlib.md5(url.encode()).hexdigest()
115
  if not os.path.exists(subdir):
@@ -127,7 +120,6 @@ def main():
127
  for subdir in history.values():
128
  show_download_links(subdir)
129
 
130
- # Display history as markdown
131
  with st.expander("URL History and Downloaded Files"):
132
  for url, subdir in history.items():
133
  st.markdown(f"#### {url}")
 
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}")