Insta_Details / app.py
gskdsrikrishna's picture
Update app.py
d89d5ff verified
raw
history blame
2.59 kB
import streamlit as st
import requests
import json
import os
# Load API Key from Hugging Face Secrets
API_KEY = os.getenv("INSTA_API")
# API Details
API_URL = "https://instagram-scraper-api2.p.rapidapi.com/v1/info"
HEADERS = {
"x-rapidapi-key": API_KEY,
"x-rapidapi-host": "instagram-scraper-api2.p.rapidapi.com"
}
# Streamlit UI
st.set_page_config(page_title="Instagram Scraper", layout="wide")
st.title("πŸ“Έ Instagram Scraper API")
# User Input
username = st.text_input("Enter Instagram Username:", "mrbeast")
if st.button("Fetch Details"):
with st.spinner("Fetching data..."):
response = requests.get(API_URL, headers=HEADERS, params={"username_or_id_or_url": username})
if response.status_code == 200:
data = response.json()
st.success("βœ… Data Retrieved Successfully!")
# **Display JSON Response by Default**
st.subheader("πŸ“œ JSON Response")
st.json(data)
# Extracting Links
links = [value for key, value in data.items() if isinstance(value, str) and value.startswith("http")]
# Display Links Below JSON
if links:
st.subheader("πŸ”— Extracted Links:")
for link in links:
st.markdown(f"[πŸ”— {link}]({link})")
else:
st.info("No links found in the response.")
# **Download Buttons Below Links**
st.subheader("⬇️ Download Data")
# Convert JSON to formatted text
json_data = json.dumps(data, indent=4)
text_data = "\n".join(f"{key}: {value}" for key, value in data.items())
# Download JSON File
st.download_button(label="Download JSON", data=json_data, file_name=f"{username}_data.json", mime="application/json")
# Download Text File
st.download_button(label="Download Text", data=text_data, file_name=f"{username}_details.txt", mime="text/plain")
# Download Profile Picture (If available)
profile_pic = data.get("profile_picture")
if profile_pic:
st.image(profile_pic, caption="Profile Picture")
img_bytes = requests.get(profile_pic).content
st.download_button(label="Download Profile Picture", data=img_bytes, file_name=f"{username}_profile.jpg", mime="image/jpeg")
else:
st.error("❌ Failed to retrieve data. Please check the username.")
# Footer
st.markdown("---")
st.caption("Powered by RapidAPI & Streamlit")