Commit
·
7dc760d
1
Parent(s):
c9d5bf0
Working /experimenting
Browse files- Dockerfile +1 -1
- real_estate.py +67 -0
- requirements.txt +2 -1
Dockerfile
CHANGED
@@ -53,4 +53,4 @@ RUN python -m playwright install chromium
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
-
CMD ["python", "-m", "uvicorn", "
|
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
+
CMD ["python", "-m", "uvicorn", "real_estate:app", "--host", "0.0.0.0", "--port", "7860"]
|
real_estate.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeout
|
4 |
+
from typing import List, Optional
|
5 |
+
import datetime
|
6 |
+
import logging
|
7 |
+
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
app = FastAPI(title="RealEstateSnap")
|
10 |
+
|
11 |
+
class Listing(BaseModel):
|
12 |
+
title: str
|
13 |
+
price: Optional[str]
|
14 |
+
address: Optional[str]
|
15 |
+
bedrooms: Optional[str]
|
16 |
+
bathrooms: Optional[str]
|
17 |
+
listing_url: str
|
18 |
+
image_url: Optional[str]
|
19 |
+
platform: str
|
20 |
+
timestamp: str
|
21 |
+
|
22 |
+
async def scrape_craigslist(location: str, limit: int = 10) -> List[Listing]:
|
23 |
+
listings = []
|
24 |
+
async with async_playwright() as p:
|
25 |
+
browser = await p.chromium.launch(headless=True)
|
26 |
+
page = await browser.new_page()
|
27 |
+
url = f"https://{location.replace(' ', '').lower()}.craigslist.org/search/apa"
|
28 |
+
logging.info(f"➡️ Going to {url}")
|
29 |
+
await page.goto(url)
|
30 |
+
items = await page.query_selector_all(".result-row")[:limit]
|
31 |
+
for item in items:
|
32 |
+
try:
|
33 |
+
title = await item.inner_text(".result-title")
|
34 |
+
href = await item.get_attribute(".result-title", "href")
|
35 |
+
price = await item.inner_text(".result-price")
|
36 |
+
listings.append(Listing(
|
37 |
+
title=title.strip(),
|
38 |
+
price=price.strip(),
|
39 |
+
address="",
|
40 |
+
bedrooms="",
|
41 |
+
bathrooms="",
|
42 |
+
listing_url=href,
|
43 |
+
image_url=None,
|
44 |
+
platform="craigslist",
|
45 |
+
timestamp=datetime.datetime.utcnow().isoformat()
|
46 |
+
))
|
47 |
+
except PlaywrightTimeout:
|
48 |
+
logging.warning("Skipped a troublesome item")
|
49 |
+
await browser.close()
|
50 |
+
return listings
|
51 |
+
|
52 |
+
@app.get("/realestate", response_model=List[Listing])
|
53 |
+
async def get_listings(
|
54 |
+
location: str = Query(...),
|
55 |
+
platform: Optional[List[str]] = Query(["craigslist"])
|
56 |
+
):
|
57 |
+
platform = [p.lower() for p in platform]
|
58 |
+
results = []
|
59 |
+
if "craigslist" in platform:
|
60 |
+
try:
|
61 |
+
results += await scrape_craigslist(location)
|
62 |
+
except Exception as e:
|
63 |
+
raise HTTPException(status_code=500, detail=str(e))
|
64 |
+
# TODO: Add Zillow and Realtor here...
|
65 |
+
if not results:
|
66 |
+
raise HTTPException(status_code=404, detail="No listings found")
|
67 |
+
return results
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ fastapi
|
|
2 |
uvicorn[standard]
|
3 |
pydantic
|
4 |
playwright
|
5 |
-
typing
|
|
|
|
2 |
uvicorn[standard]
|
3 |
pydantic
|
4 |
playwright
|
5 |
+
typing
|
6 |
+
python-multipart
|