forked from passivebot/facebook-marketplace-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
71 lines (51 loc) · 2.63 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import json
import requests
from PIL import Image
# Create a title for the web app.
st.title("Passivebot's Facebook Marketplace Scraper")
# Add a list of supported cities.
supported_cities = ["New York", "Los Angeles", "Las Vegas", "Chicago", "Houston", "San Antonio", "Miami", "Orlando", "San Diego", "Arlington", "Baltimore", "Cincinnati", "Denver", "Fort Worth", "Jacksonville", "Memphis", "Nashville", "Philadelphia", "Portland", "San Jose", "Tucson", "Atlanta", "Boston", "Columbus", "Detroit", "Honolulu", "Kansas City", "New Orleans", "Phoenix", "Seattle", "Washington DC", "Milwaukee", "Sacramento", "Austin", "Charlotte", "Dallas", "El Paso", "Indianapolis", "Louisville", "Minneapolis", "Oklahoma City", "Pittsburgh", "San Francisco", "Tampa"]
# Take user input for the city, query, and max price.
city = st.selectbox("City", supported_cities, 0)
query = st.text_input("Query", "Macbook Pro")
max_price = st.text_input("Max Price", "1000")
# Create a button to submit the form.
submit = st.button("Submit")
# If the button is clicked.
if submit:
# TODO - Remove any commas from the max_price before sending the request.
if "," in max_price:
max_price = max_price.replace(",", "")
else:
pass
res = requests.get(f"http://127.0.0.1:8000/crawl_facebook_marketplace?city={city}&query={query}&max_price={max_price}"
)
# Convert the response from json into a Python list.
results = res.json()
# Display the length of the results list.
st.write(f"Number of results: {len(results)}")
# Iterate over the results list to display each item.
for item in results:
try:
st.header(item["title"])
# Check if the image URL is not None before displaying the image
img_url = item["image"]
if img_url:
st.image(img_url, width=200)
# Check if the price is not None before displaying it
price = item["price"]
if price:
st.write(price)
# Check if the location is not None before displaying it
location = item["location"]
if location:
st.write(location)
# Check if the link is not None before creating the URL link
link = item['link']
if link:
st.write(f"https://www.facebook.com{link}")
st.write("----")
except Exception as e:
# Handle the exception (print an error message, log it, etc.)
st.error(f"Error processing an item: {e}")