-
Notifications
You must be signed in to change notification settings - Fork 173
Parse Google Flights shopping XHR responses #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jeffchang5
wants to merge
1
commit into
AWeirdDev:dev
Choose a base branch
from
jeffchang5:codex/google-flights-xhr-parser
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
|
|
||
|
|
||
| @dataclass | ||
| class FetchResult: | ||
| """Result returned by integrations that capture both HTML and XHR data.""" | ||
|
|
||
| html: str = "" | ||
| xhr_bodies: list[str | bytes] = field(default_factory=list) | ||
| url: str | None = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from .base import Integration | ||
| from .bright_data import BrightData | ||
| from .playwright import Playwright | ||
|
|
||
| __all__ = ["Integration", "BrightData"] | ||
| __all__ = ["Integration", "BrightData", "Playwright"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from ..fetch_result import FetchResult | ||
| from ..querying import Query | ||
| from .base import Integration | ||
|
|
||
| GOOGLE_FLIGHTS_URL = "https://www.google.com/travel/flights" | ||
| XHR_MARKER = "FlightsFrontendService/GetShoppingResults" | ||
|
|
||
|
|
||
| class Playwright(Integration): | ||
| """Local Chromium integration that captures Google Flights shopping XHRs.""" | ||
|
|
||
| def fetch_html(self, q: Query | str, /) -> FetchResult: | ||
| from playwright.sync_api import sync_playwright | ||
|
|
||
| url = q.url() if isinstance(q, Query) else GOOGLE_FLIGHTS_URL + "?q=" + q | ||
| response_objects: list[Any] = [] | ||
|
|
||
| with sync_playwright() as p: | ||
| browser = p.chromium.launch(headless=True) | ||
| context = browser.new_context( | ||
| user_agent=( | ||
| "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) " | ||
| "AppleWebKit/537.36 (KHTML, like Gecko) " | ||
| "Chrome/145.0.0.0 Safari/537.36" | ||
| ), | ||
| locale="en-US", | ||
| viewport={"width": 1280, "height": 900}, | ||
| ) | ||
| page = context.new_page() | ||
| page.add_init_script(""" | ||
| Object.defineProperty(navigator, 'webdriver', {get: () => undefined}); | ||
| window.chrome = {runtime: {}}; | ||
| Object.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5]}); | ||
| Object.defineProperty(navigator, 'languages', {get: () => ['en-US', 'en']}); | ||
| """) | ||
|
|
||
| def on_response(response: Any) -> None: | ||
| if XHR_MARKER in response.url: | ||
| response_objects.append(response) | ||
|
|
||
| page.on("response", on_response) | ||
| try: | ||
| page.goto( | ||
| GOOGLE_FLIGHTS_URL, | ||
| timeout=25_000, | ||
| wait_until="domcontentloaded", | ||
| ) | ||
| page.wait_for_timeout(1_000) | ||
| except Exception: | ||
| pass | ||
|
|
||
| page.goto(url, timeout=25_000, wait_until="domcontentloaded") | ||
| try: | ||
| page.wait_for_load_state("networkidle", timeout=12_000) | ||
| except Exception: | ||
| pass | ||
| page.wait_for_timeout(2_000) | ||
|
|
||
| xhr_bodies: list[bytes] = [] | ||
| for response in response_objects: | ||
| try: | ||
| xhr_bodies.append(response.body()) | ||
| except Exception: | ||
| pass | ||
|
|
||
| html = page.content() | ||
| browser.close() | ||
|
|
||
| return FetchResult(html=html, xhr_bodies=xhr_bodies, url=url) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL-encode the raw string query.
When
qis astr, the value is concatenated directly into the URL. Any spaces,&,#,+,%, or non-ASCII characters will produce a malformed URL or be parsed by Google as additional query parameters. A natural-language query like"Flights from TPE to MYJ on 2025-12-22 one way economy class"(the same example used in theget_flightsdocstring) will be broken on the spaces and-.🛡️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents