gskdsrikrishna commited on
Commit
4f22392
Β·
verified Β·
1 Parent(s): 9c9b778

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -15
app.py CHANGED
@@ -2,6 +2,8 @@ import streamlit as st
2
  import requests
3
  import json
4
  import os
 
 
5
 
6
  # Load API Key from Hugging Face Secrets
7
  API_KEY = os.getenv("INSTA_API")
@@ -13,7 +15,7 @@ HEADERS = {
13
  "x-rapidapi-host": "instagram-scraper-api2.p.rapidapi.com"
14
  }
15
 
16
- # Function to extract all links recursively from JSON
17
  def extract_links(obj):
18
  links = []
19
  if isinstance(obj, dict):
@@ -46,16 +48,41 @@ if st.button("Fetch Details"):
46
  st.subheader("πŸ“œ JSON Response")
47
  st.json(data)
48
 
49
- # Extracting Links from Nested JSON
50
  links = extract_links(data)
51
 
52
- # Display Links Below JSON
53
- if links:
54
- st.subheader("πŸ”— Extracted Links:")
55
- for link in links:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  st.markdown(f"[πŸ”— {link}]({link})")
57
- else:
58
- st.info("❌ No links found in the response.")
59
 
60
  # **Download Buttons Below Links**
61
  st.subheader("⬇️ Download Data")
@@ -70,13 +97,6 @@ if st.button("Fetch Details"):
70
  # Download Text File
71
  st.download_button(label="Download Text", data=text_data, file_name=f"{username}_details.txt", mime="text/plain")
72
 
73
- # Download Profile Picture (If available)
74
- profile_pic = data.get("profile_picture")
75
- if profile_pic:
76
- st.image(profile_pic, caption="Profile Picture")
77
- img_bytes = requests.get(profile_pic).content
78
- st.download_button(label="Download Profile Picture", data=img_bytes, file_name=f"{username}_profile.jpg", mime="image/jpeg")
79
-
80
  else:
81
  st.error("❌ Failed to retrieve data. Please check the username.")
82
 
 
2
  import requests
3
  import json
4
  import os
5
+ import zipfile
6
+ import io
7
 
8
  # Load API Key from Hugging Face Secrets
9
  API_KEY = os.getenv("INSTA_API")
 
15
  "x-rapidapi-host": "instagram-scraper-api2.p.rapidapi.com"
16
  }
17
 
18
+ # Function to extract all links recursively
19
  def extract_links(obj):
20
  links = []
21
  if isinstance(obj, dict):
 
48
  st.subheader("πŸ“œ JSON Response")
49
  st.json(data)
50
 
51
+ # Extract all links
52
  links = extract_links(data)
53
 
54
+ # Separate image links and other links
55
+ image_links = [link for link in links if any(ext in link for ext in [".jpg", ".jpeg", ".png", ".webp"])]
56
+ other_links = [link for link in links if link not in image_links]
57
+
58
+ # Display Images
59
+ if image_links:
60
+ st.subheader("πŸ–ΌοΈ Extracted Images:")
61
+ img_bytes_list = []
62
+ img_filenames = []
63
+
64
+ for idx, img_url in enumerate(image_links):
65
+ st.image(img_url, caption=f"Image {idx + 1}", use_column_width=True)
66
+
67
+ # Download images as bytes
68
+ img_bytes = requests.get(img_url).content
69
+ img_bytes_list.append(img_bytes)
70
+ img_filenames.append(f"{username}_image_{idx+1}.jpg")
71
+
72
+ # **Download All Images as ZIP**
73
+ if img_bytes_list:
74
+ with io.BytesIO() as zip_buffer:
75
+ with zipfile.ZipFile(zip_buffer, "w") as zip_file:
76
+ for filename, img_data in zip(img_filenames, img_bytes_list):
77
+ zip_file.writestr(filename, img_data)
78
+ zip_buffer.seek(0)
79
+ st.download_button("πŸ“₯ Download All Images", data=zip_buffer, file_name=f"{username}_images.zip", mime="application/zip")
80
+
81
+ # Display Other Links
82
+ if other_links:
83
+ st.subheader("πŸ”— Other Links:")
84
+ for link in other_links:
85
  st.markdown(f"[πŸ”— {link}]({link})")
 
 
86
 
87
  # **Download Buttons Below Links**
88
  st.subheader("⬇️ Download Data")
 
97
  # Download Text File
98
  st.download_button(label="Download Text", data=text_data, file_name=f"{username}_details.txt", mime="text/plain")
99
 
 
 
 
 
 
 
 
100
  else:
101
  st.error("❌ Failed to retrieve data. Please check the username.")
102