Spaces:
Running
Running
from flask import Flask, request, render_template | |
import feedparser | |
import requests | |
from bs4 import BeautifulSoup | |
import urllib.parse | |
app = Flask(__name__) | |
# List of Craigslist cities (partial list for brevity; expand as needed) | |
CRAIGSLIST_CITIES = [ | |
"all", "newyork", "losangeles", "chicago", "houston", "phoenix", "philadelphia", | |
"sanantonio", "sandiego", "dallas", "sanjose", "austin", "jacksonville", | |
"sanfrancisco", "columbus", "seattle", "denver", "boston", "miami", "atlanta" | |
] | |
def search_craigslist(query, city): | |
""" | |
Search Craigslist for a query in a specific city or all cities. | |
Returns a list of posts with title, link, description, and date. | |
""" | |
posts = [] | |
query = urllib.parse.quote(query) # URL-encode the query | |
if city == "all": | |
# Search across multiple cities | |
for city_name in CRAIGSLIST_CITIES[1:]: # Skip "all" | |
url = f"https://{city_name}.craigslist.org/search/sss?format=rss&query={query}" | |
feed = feedparser.parse(url) | |
posts.extend(parse_feed(feed, city_name)) | |
else: | |
# Search in a specific city | |
url = f"https://{city}.craigslist.org/search/sss?format=rss&query={query}" | |
feed = feedparser.parse(url) | |
posts.extend(parse_feed(feed, city)) | |
return posts | |
def parse_feed(feed, city): | |
""" | |
Parse the RSS feed and extract relevant post information. | |
""" | |
posts = [] | |
for entry in feed.entries: | |
# Clean description using BeautifulSoup | |
description = entry.get("description", "") | |
soup = BeautifulSoup(description, "html.parser") | |
clean_description = soup.get_text(strip=True)[:200] + "..." if len(soup.get_text(strip=True)) > 200 else soup.get_text(strip=True) | |
post = { | |
"title": entry.get("title", "No title"), | |
"link": entry.get("link", "#"), | |
"description": clean_description, | |
"date": entry.get("published", "No date"), | |
"city": city.capitalize() | |
} | |
posts.append(post) | |
return posts | |
def index(): | |
posts = [] | |
query = "" | |
selected_city = "all" | |
if request.method == "POST": | |
query = request.form.get("query", "").strip() | |
selected_city = request.form.get("city", "all") | |
if query: | |
posts = search_craigslist(query, selected_city) | |
return render_template( | |
"index.html", | |
posts=posts, | |
query=query, | |
cities=CRAIGSLIST_CITIES, | |
selected_city=selected_city | |
) | |
if __name__ == "__main__": | |
app.run(debug=True) |