ElegantSolutions commited on
Commit
493967f
·
verified ·
1 Parent(s): 2320627

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -4
app.py CHANGED
@@ -9,6 +9,48 @@ from difflib import SequenceMatcher
9
  import json
10
  from urllib.parse import quote_plus
11
  import base64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # -----------------------------------------------
14
  # 🔧 UTILITY FUNCTIONS
@@ -141,7 +183,7 @@ if uploaded_file and api_key:
141
  if output_file:
142
  with open(output_file, "rb") as f:
143
  csv_bytes = f.read()
144
-
145
  b64 = base64.b64encode(csv_bytes).decode()
146
  href = f'<a href="data:text/csv;base64,{b64}" download="updated_with_linkedin_links.csv" id="auto-download-link"></a>'
147
 
@@ -154,15 +196,14 @@ if uploaded_file and api_key:
154
  """,
155
  unsafe_allow_html=True
156
  )
157
-
158
  st.success("✅ Processing complete. Your file is downloading automatically! Or click below if it didn't start.")
159
- # st.success("✅ Processing complete. Download the updated file below.")
160
  st.download_button(
161
  label="📥 Download CSV with LinkedIn Links",
162
  data=f,
163
  file_name="updated_with_linkedin_links.csv",
164
  mime="text/csv"
165
- )
166
  shutil.rmtree(os.path.dirname(output_file)) # Cleanup temp directory
167
  elif not api_key:
168
  st.warning("⚠️ Please enter your BrightData SERP API key to proceed.")
 
9
  import json
10
  from urllib.parse import quote_plus
11
  import base64
12
+ import zipfile
13
+ from datetime import datetime
14
+
15
+
16
+ # -----------zip_and_auto_download helper function--------------
17
+ def zip_and_auto_download(file_bytes, inner_filename, zip_prefix="Download"):
18
+ """
19
+ Wraps given bytes in a ZIP and auto-downloads it via base64 + fallback button.
20
+ """
21
+ # Create temp ZIP file
22
+ zip_filename = f"{zip_prefix}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
23
+ zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
24
+
25
+ with zipfile.ZipFile(zip_file_path, 'w') as zipf:
26
+ zipf.writestr(inner_filename, file_bytes)
27
+
28
+ # Read ZIP bytes
29
+ with open(zip_file_path, "rb") as f:
30
+ zip_bytes = f.read()
31
+
32
+ # Fallback download button
33
+ st.download_button(
34
+ label=f"📥 Download {zip_filename}",
35
+ data=zip_bytes,
36
+ file_name=zip_filename,
37
+ mime="application/zip"
38
+ )
39
+
40
+ # Auto-download link
41
+ b64 = base64.b64encode(zip_bytes).decode()
42
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}" id="auto-download-link"></a>'
43
+ st.markdown(href, unsafe_allow_html=True)
44
+ st.markdown(
45
+ """
46
+ <script>
47
+ document.getElementById('auto-download-link').click();
48
+ </script>
49
+ """,
50
+ unsafe_allow_html=True
51
+ )
52
+
53
+ os.remove(zip_file_path)
54
 
55
  # -----------------------------------------------
56
  # 🔧 UTILITY FUNCTIONS
 
183
  if output_file:
184
  with open(output_file, "rb") as f:
185
  csv_bytes = f.read()
186
+
187
  b64 = base64.b64encode(csv_bytes).decode()
188
  href = f'<a href="data:text/csv;base64,{b64}" download="updated_with_linkedin_links.csv" id="auto-download-link"></a>'
189
 
 
196
  """,
197
  unsafe_allow_html=True
198
  )
199
+
200
  st.success("✅ Processing complete. Your file is downloading automatically! Or click below if it didn't start.")
 
201
  st.download_button(
202
  label="📥 Download CSV with LinkedIn Links",
203
  data=f,
204
  file_name="updated_with_linkedin_links.csv",
205
  mime="text/csv"
206
+ )
207
  shutil.rmtree(os.path.dirname(output_file)) # Cleanup temp directory
208
  elif not api_key:
209
  st.warning("⚠️ Please enter your BrightData SERP API key to proceed.")