FBS / app.py
JaySMTA's picture
Update app.py
65a05a4 verified
Raw History Blame Contribute Delete
4.28 kB
import requests
import pandas as pd
import gradio as gr
import re
import os
import tempfile
ZYLALABS_API_KEY = os.environ.get("ZYLALABS_API_KEY")
DASHBOARD_PASSWORD = os.environ.get("DASHBOARD_PASSWORD", "1984")
print(f"Using API key: {ZYLALABS_API_KEY[:5]}...") # Only print the first 5 characters
def extract_username(url):
match = re.search(r'facebook\.com/([^/?]+)', url)
return match.group(1) if match else None
def scrape_facebook(profile_url):
profile_id = extract_username(profile_url)
if not profile_id:
print(f"Could not extract username from URL: {profile_url}")
return {}
url = f"https://zylalabs.com/api/4512/facebook+scraper+-+fetcher+pro+api/5534/profile"
headers = {
"Authorization": f"Bearer {ZYLALABS_API_KEY}"
}
params = {
"id": profile_id
}
try:
response = requests.post(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
print(f"Raw API response for {profile_id}: {data}") # Add this line
profile_info = {
'Profile URL': profile_url,
'Bio': data.get('about', ''),
'Follower Count': data.get('followers', 0),
'Following Count': data.get('following', 0),
'Bio Links': data.get('website', ''),
'Full Name': data.get('name', ''),
'Username': data.get('username', ''),
'Num Posts': data.get('posts_count', 0),
'Profile ID': profile_id,
'Email': data.get('email', ''),
'Phone Number': data.get('phone', ''),
'City Name': data.get('location', {}).get('name', ''),
'Country': data.get('location', {}).get('country', ''),
'Date Joined': data.get('joined_date', '')
}
return profile_info
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
return {}
def get_facebook_info(df):
columns = [
'Profile URL', 'Bio', 'Follower Count', 'Following Count', 'Bio Links',
'Full Name', 'Username', 'Num Posts', 'Profile ID', 'Email',
'Phone Number', 'City Name', 'Country', 'Date Joined'
]
result_df = pd.DataFrame(columns=columns)
profile_urls = df['Profile URL'].values
for url in profile_urls:
try:
profile_info = scrape_facebook(url)
if profile_info:
# Create a new DataFrame with the profile info and concat it
new_row = pd.DataFrame([profile_info])
result_df = pd.concat([result_df, new_row], ignore_index=True)
else:
print(f"No data returned for profile: {url}")
except Exception as e:
print(f"Error processing {url}: {str(e)}")
return result_df
def gradio_interface(csv_file, password):
if password != DASHBOARD_PASSWORD:
return "Incorrect Password", None, None
try:
df = pd.read_csv(csv_file.name, names=['Profile URL'], header=None)
print(f"Processing URLs: {df['Profile URL'].tolist()}")
output_df = get_facebook_info(df)
print("Output DataFrame:")
print(output_df)
# Create a temporary file to store the CSV data
with tempfile.NamedTemporaryFile(mode="w+", suffix=".csv", delete=False) as temp_file:
output_df.to_csv(temp_file.name, index=False)
completion_status = "Done"
return completion_status, temp_file.name, output_df.to_string()
except Exception as e:
return f"An error occurred: {str(e)}", None, None
iface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.File(label="Input CSV File (must be CSV File with Facebook URLs)"),
gr.Textbox(label="Enter Dashboard Password", type="password")
],
outputs=[
gr.Textbox(label="Completion Status"),
gr.File(label="Download Scraped Data"),
gr.Textbox(label="Scraped Data Preview")
],
title="Facebook Scraper Dashboard",
description="This dashboard scrapes data from Facebook profiles using the Zylalabs API."
)
if __name__ == "__main__":
iface.launch(share=True)