File size: 16,691 Bytes
88678e4
75f7619
4ed529d
88a6677
16e0fb1
5dc68a0
88678e4
a3a56fa
1327b80
88678e4
16e0fb1
 
 
 
 
 
 
 
5dc68a0
 
16e0fb1
88678e4
 
dd55d7d
16e0fb1
 
 
e88d9e1
16e0fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8a8b8
 
 
16e0fb1
 
 
 
 
5dc68a0
16e0fb1
5dc68a0
16e0fb1
 
 
5dc68a0
 
 
 
 
 
 
 
 
 
16e0fb1
 
 
 
5dc68a0
 
 
 
 
 
 
 
 
 
 
16e0fb1
5dc68a0
 
 
 
 
 
 
 
 
 
 
 
 
16e0fb1
 
 
 
 
 
 
e88d9e1
1327b80
16e0fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
5dc68a0
16e0fb1
 
5dc68a0
16e0fb1
 
 
 
 
5dc68a0
 
16e0fb1
 
 
 
 
 
 
 
2efa288
 
 
 
 
 
 
 
 
 
16e0fb1
88a6677
8483a18
2efa288
88a6677
16e0fb1
 
 
 
 
53b935b
16e0fb1
 
53b935b
 
 
 
 
 
 
 
 
16e0fb1
53b935b
 
 
 
16e0fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed529d
0e200a9
dd55d7d
8c6da18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e200a9
8c6da18
ba8a8b8
0e200a9
 
 
 
 
 
 
 
97c8e04
0e200a9
97c8e04
 
 
55b53a1
97c8e04
 
 
 
5dc68a0
ec89302
0e200a9
55b53a1
97c8e04
 
 
 
 
 
 
 
 
 
 
55b53a1
0e200a9
16e0fb1
55b53a1
0e200a9
16e0fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55b53a1
16e0fb1
 
 
 
0e200a9
 
f14b44c
0e200a9
 
 
 
 
 
 
 
 
 
 
 
8c6da18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f14b44c
0e200a9
 
 
 
 
 
 
 
 
 
 
 
 
ec89302
0e200a9
8c6da18
0e200a9
 
 
 
 
 
 
 
 
 
97c8e04
 
0e200a9
 
dd55d7d
 
5dc68a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a87400b
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import asyncio
import re
import os
import httpx
import random
import time
from typing import List, Dict
from datetime import datetime
from urllib.parse import quote

# Import bypass utilities
from rate_limit_bypass import (
    smart_requester, 
    get_random_user_agent, 
    generate_user_agent,
    cache,
    RequestDelayer
)
from scraper_health import scraper_metrics


class PatchrightAirbnbScraper:
    def __init__(self):
        self.firecrawl_key = os.getenv("FIRECRAWL_API_KEY") or os.getenv("firecrawl_api_key")
        self.cache = cache
        self.delayer = RequestDelayer(min_delay=5, max_delay=15)

    async def search_airbnb(self, region: str, checkin: str, checkout: str, adults: int = 4, children: int = 0, pets: int = 1, budget_max: int = 500) -> List[Dict]:
        """
        Smart search with fallback strategies.
        """
        # Specificity fix: If region is a single word and likely European, append "Germany" or "Netherlands"
        # to avoid landing in "Hamburg, NY" etc.
        search_region = region
        if "," not in region:
            low_region = region.lower()
            if any(x in low_region for x in ["hamburg", "berlin", "münchen", "munich", "köln", "cologne"]):
                search_region = f"{region}, Germany"
            elif any(x in low_region for x in ["amsterdam", "rotterdam", "utrecht", "zandvoort", "texel", "zeeland"]):
                search_region = f"{region}, Netherlands"

        # Calculate nights for parsing
        d1 = datetime.strptime(checkin, "%Y-%m-%d")
        d2 = datetime.strptime(checkout, "%Y-%m-%d")
        nights = max(1, (d2 - d1).days)
        
        strategies = [
            ("curl", self._search_curl),
            ("firecrawl", self._search_firecrawl),
        ]

        for name, strategy in strategies:
            started = time.perf_counter()
            try:
                print(f"   [Scraper] Trying {name} strategy for {search_region}...")
                deals = await strategy(search_region, checkin, checkout, adults, children, pets, budget_max, nights)
                duration = time.perf_counter() - started
                scraper_metrics.record(
                    source="airbnb",
                    strategy=name,
                    success=bool(deals),
                    duration=duration,
                    result_count=len(deals) if deals else 0,
                    error=None if deals else "no_results",
                )

                if deals and len(deals) > 0:
                    print(f"   ✅ {name} strategy succeeded: {len(deals)} deals")
                    return deals
            except Exception as e:
                duration = time.perf_counter() - started
                scraper_metrics.record(
                    source="airbnb",
                    strategy=name,
                    success=False,
                    duration=duration,
                    result_count=0,
                    error=str(e),
                )
                err_short = self._truncate_text(str(e), 100)
                print(f"   ❌ {name} strategy failed: {err_short}")
                continue

        fallback_started = time.perf_counter()
        fallback_deals = self._get_fallback_data(search_region, nights)
        fallback_duration = time.perf_counter() - fallback_started
        scraper_metrics.record(
            source="airbnb",
            strategy="fallback",
            success=bool(fallback_deals),
            duration=fallback_duration,
            result_count=len(fallback_deals),
            error=None if fallback_deals else "no_results",
        )
        return fallback_deals

    async def _search_curl(self, region: str, checkin: str, checkout: str, adults: int, children: int, pets: int, budget_max: int, nights: int) -> List[Dict]:
        """
        Fast strategy using local httpx request with rotated User-Agents.
        Note: Airbnb often blocks this, hence why it's the first (fast) attempt.
        """
        await self.delayer.wait()
        url = f"https://www.airbnb.com/s/{quote(region)}/homes?checkin={checkin}&checkout={checkout}&adults={adults}&children={children}&pets={pets}&price_max={budget_max}"
        
        headers = {
            "User-Agent": get_random_user_agent(),
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
            "Accept-Language": "en-US,en;q=0.5",
            "DNT": "1",
            "Upgrade-Insecure-Requests": "1",
        }
        
        async with httpx.AsyncClient(headers=headers, timeout=30.0, follow_redirects=True) as client:
            response = await client.get(url)
            if response.status_code == 200:
                # Basic check for block
                if "dropped her ice cream" in response.text or "unusual activity" in response.text:
                    raise Exception("429 Blocked by Airbnb (Ice Cream/Bot detection)")

                # If we got real HTML, parse it (parsing logic might need to be different for raw HTML vs Markdown)
                # For now, we reuse the markdown parser if the text looks okay, or return empty to trigger next strategy
                return []  # Placeholder: HTML parsing is complex, fallback to Firecrawl for now
            elif response.status_code == 429:
                raise Exception("429 Too Many Requests")
            else:
                raise Exception(f"HTTP Error {response.status_code}")

        return []

    async def _search_firecrawl(self, region: str, checkin: str, checkout: str, adults: int, children: int, pets: int, budget_max: int, nights: int) -> List[Dict]:
        """Verified strategy using Firecrawl cloud scraping."""
        if not self.firecrawl_key:
            raise Exception("Firecrawl API key missing")
            
        url = f"https://www.airbnb.com/s/{quote(region)}/homes?checkin={checkin}&checkout={checkout}&adults={adults}&children={children}&pets={pets}&price_max={budget_max}"
        
        async def make_firecrawl_call():
            async with httpx.AsyncClient(timeout=120.0) as client:
                payload = {
                    "url": url, 
                    "formats": ["markdown"], 
                    "waitFor": 8000,
                    "actions": [
                        {"type": "scroll", "direction": "down", "amount": 500},
                        {"type": "wait", "milliseconds": 2000}
                    ]
                }
                return await client.post(
                    "https://api.firecrawl.dev/v1/scrape",
                    headers={"Authorization": f"Bearer {self.firecrawl_key}"},
                    json=payload
                )

        response = await smart_requester.request(make_firecrawl_call)
        
        if response.status_code == 200:
            data = response.json().get('data', {})
            html = data.get('html', '')
            markdown = data.get('markdown', '')
            
            deals = []
            # Check for Airbnb Error Page (Ice Cream Girl / 503)
            if html and "dropped her ice cream" not in html and "temporarily unavailable" not in html:
                # Airbnb HTML parsing is complex, we mainly use markdown, 
                # but we can try to find properties in markdown here
                deals = self._parse_markdown(markdown, region, nights)
            
            if not deals and markdown:
                deals = self._parse_markdown(markdown, region, nights)
                
            if not deals:
                raise Exception("Airbnb blocked or no results found")
                    
            return deals
        else:
            raise Exception(f"Firecrawl API Error: {response.status_code}")

    def _get_fallback_data(self, region: str, nights: int, *args, **kwargs) -> List[Dict]:
        """Emergency fallback data when all scraping fails."""
        print(f"   ⚠️ Using fallback data for {region}")
        return [
            {
                "name": f"Gemütliches Haus in {region} (Fallback)",
                "location": region,
                "price_per_night": 120,
                "rating": 4.5,
                "reviews": 10,
                "pet_friendly": True,
                "source": "fallback",
                "url": "https://www.airbnb.com",
                "image_url": "https://images.unsplash.com/photo-1518780664697-55e3ad937233?auto=format&fit=crop&q=80&w=720"
            }
        ]

    def _parse_markdown(self, text: str, region: str, searched_nights: int) -> List[Dict]:
        deals = []
        
        # 0. Check for "No results" or "Other dates" sections
        # If we see "Results for other dates", we should truncate the text to avoid parsing them
        other_dates_patterns = [
            "Results for other dates", "Ergebnisse für andere Daten",
            "Suggested results", "Vorgeschlagene Ergebnisse",
            "Try adjusting your search", "Versuche es mit anderen Filtern"
        ]
        
        clean_text = text
        for p in other_dates_patterns:
            if p in text:
                # Truncate text at the first occurrence of such a section
                clean_text = text.split(p)[0]
                break
        
        # 1. Identify all Room IDs and their positions in the CLEAN text
        id_pattern = re.compile(r'rooms/(\d+)')
        matches = [(m.group(1), m.start()) for m in id_pattern.finditer(clean_text)]
        
        # Deduplicate while preserving order of first appearance
        seen = set()
        unique_matches = []
        for rid, pos in matches:
            if rid not in seen:
                seen.add(rid)
                unique_matches.append((rid, pos))
        
        for i, (room_id, pos) in enumerate(unique_matches):
            # Define the text block for this listing
            # Instead of starting at pos, we look at the range between IDs
            # or a generous buffer before the current ID
            prev_pos = unique_matches[i-1][1] if i > 0 else 0
            
            # The block should start after the previous deal or at a reasonable offset
            start_search = max(prev_pos, pos - 2000)
            end_search = unique_matches[i+1][1] if i + 1 < len(unique_matches) else len(clean_text)
            
            block = self._substring(clean_text, start_search, end_search)
            
            # --- PARSING LOGIC ---
            
            # 1. Images (capture up to 5)
            images = []
            # Look for all images in this block
            img_matches = re.findall(r'!\[.*?\]\((https://[^)]+)\)', block)
            for img_url in img_matches:
                full_url = img_url.split('?')[0] + "?im_w=720"
                if full_url not in images:
                    images.append(full_url)
                if len(images) >= 5: break
            
            image_url = images[0] if images else ""

            # 2. Name
            # Strategy: Look for the title which is often a bold line or a line following the "Apartment in..."
            name = "[DEBUG: NAME FEHLT]"
            
            # Remove image markdown from block to avoid noise
            clean_block = re.sub(r'!\[.*?\]\(.*?\)', '', block)
            lines = [l.strip() for l in clean_block.split('\n') if l.strip()]
            
            # Pattern for "Type in Location"
            type_pattern = r'(Apartment|Home|Condo|Villa|House|Guest suite|Cottage|Loft|Room|Private room) in ([A-Za-z\s,\-]+)'
            
            for idx, line in enumerate(lines):
                # If we find the type line, the name is usually the next line
                if re.search(type_pattern, line, re.I):
                    if idx + 1 < len(lines):
                        potential_name = lines[idx+1]
                        # Ensure it's not a rating line or another room ID
                        if "stars" not in potential_name.lower() and "rooms/" not in potential_name:
                            name = potential_name
                            break
                    # If it's the only line or next is invalid, use current minus the prefix
                    name = re.sub(type_pattern, '', line, flags=re.I).strip()
                    if not name: name = "Airbnb Stay"
                    break

            if name == "[DEBUG: NAME FEHLT]" or len(name) < 3:
                 # Fallback: Use the first non-link, non-rating line
                 for l in lines:
                     if "rooms/" not in l and "rating" not in l.lower() and "review" not in l.lower() and len(l) > 5:
                         name = l
                         break

            # Cleanup name: remove leading/trailing punctuation often found in markdown
            name = name.strip('*,# ')
            if name.lower() == region.lower(): # If name is just the city, it's a bad parse
                 name = f"Stay in {region}"

            # 3. Price
            price_per_night = 0
            # Search for "$1,350 ... for 5 nights" pattern
            # Matches: $1,234 or €1.234
            price_block_match = re.search(r'([\$\€\£])\s*([\d,\.]+).*?for\s+(\d+)\s+nights', block, re.DOTALL | re.IGNORECASE)
            
            if price_block_match:
                currency, amount_str, nights_found = price_block_match.groups()
                amount = int(re.sub(r'[^\d]', '', amount_str))
                nights_found = int(nights_found)
                if nights_found > 0:
                    price_per_night = round(amount / nights_found)
            else:
                # Fallback: Find any price and assume it is nightly if low, or total if high
                # Check for "per night" or "Nacht" nearby
                nightly_match = re.search(r'([\$\€\£])\s*([\d,\.]+)\s*(per night|night|Nacht)', block, re.IGNORECASE)
                if nightly_match:
                    price_per_night = int(re.sub(r'[^\d]', '', nightly_match.group(2)))
                else:
                    prices = re.findall(r'[\$\€\£]\s*([\d,\.]+)', block)
                    valid_prices = []
                    for p in prices:
                        try: 
                            v = int(re.sub(r'[^\d]', '', p))
                            valid_prices.append(v)
                        except: pass
                    
                    if valid_prices:
                        best_guess = min(valid_prices)
                        if best_guess > 1000:
                            price_per_night = round(best_guess / searched_nights)
                        else:
                            price_per_night = best_guess

            # 4. Rating / Reviews
            rating = 4.8
            reviews = 20
            # "4.32 out of 5 average rating, 141 reviews"
            rating_match = re.search(r'([\d\.]+)\s*out of 5', block)
            if rating_match:
                try: rating = float(rating_match.group(1))
                except: pass
            
            rev_match = re.search(r'(\d+)\s*reviews', block)
            if rev_match:
                try: reviews = int(rev_match.group(1))
                except: pass

            # Add to list
            # Availability logic: If no price could be determined, it's not a valid deal for these dates
            if price_per_night > 0:
                deals.append({
                    "name": name, 
                    "location": region, 
                    "price_per_night": price_per_night,
                    "rating": rating, 
                    "reviews": reviews, 
                    "pet_friendly": True,
                    "source": "airbnb (cloud)", 
                    "url": f"https://www.airbnb.com/rooms/{room_id}",
                    "image_url": image_url,
                    "images": images
                })
                
        return deals

    def _truncate_text(self, value: object, limit: int = 120) -> str:
        text = str(value)
        if len(text) <= limit:
            return text

        result = ""
        idx = 0
        while idx < limit and idx < len(text):
            result = result + text[idx]
            idx += 1
        return result

    def _substring(self, text: str, start: int, end: int) -> str:
        safe_start = max(0, start)
        safe_end = max(safe_start, end)
        text_len = len(text)

        if safe_start >= text_len:
            return ""
        if safe_end > text_len:
            safe_end = text_len

        out = ""
        idx = safe_start
        while idx < safe_end:
            out = out + text[idx]
            idx += 1
        return out


SmartAirbnbScraper = PatchrightAirbnbScraper