Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions fast_flights/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from .primp import Client, Response


def fetch(params: dict) -> Response:
client = Client(impersonate="chrome_126", verify=False)
def fetch(params: dict, timeout: int = 30) -> Response:
client = Client(impersonate="chrome_126", verify=False, timeout=timeout)
res = client.get("https://www.google.com/travel/flights", params=params)
assert res.status_code == 200, f"{res.status_code} Result: {res.text_markdown}"
return res
Expand All @@ -21,6 +21,7 @@ def get_flights_from_filter(
currency: str = "",
*,
mode: Literal["common", "fallback", "force-fallback"] = "common",
timeout: int = 30,
) -> Result:
data = filter.as_b64()

Expand All @@ -33,21 +34,20 @@ def get_flights_from_filter(

if mode in {"common", "fallback"}:
try:
res = fetch(params)
res = fetch(params, timeout)
except AssertionError as e:
if mode == "fallback":
res = fallback_playwright_fetch(params)
res = fallback_playwright_fetch(params, timeout)
else:
raise e

else:
res = fallback_playwright_fetch(params)
res = fallback_playwright_fetch(params, timeout)

try:
return parse_response(res)
except RuntimeError as e:
if mode == "fallback":
return get_flights_from_filter(filter, mode="force-fallback")
return get_flights_from_filter(filter, mode="force-fallback", timeout=timeout)
raise e


Expand All @@ -59,6 +59,7 @@ def get_flights(
seat: Literal["economy", "premium-economy", "business", "first"],
fetch_mode: Literal["common", "fallback", "force-fallback"] = "common",
max_stops: Optional[int] = None,
timeout: Optional[int] = 30,
) -> Result:
return get_flights_from_filter(
TFSData.from_interface(
Expand All @@ -69,6 +70,7 @@ def get_flights(
max_stops=max_stops,
),
mode=fetch_mode,
timeout=timeout,
)


Expand Down
4 changes: 2 additions & 2 deletions fast_flights/fallback_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async def main():
"""


def fallback_playwright_fetch(params: dict) -> Any:
client = Client(impersonate="chrome_100", verify=False)
def fallback_playwright_fetch(params: dict, timeout: int = 30) -> Any:
client = Client(impersonate="chrome_100", verify=False, timeout=timeout)

res = client.post(
"https://try.playwright.tech/service/control/run",
Expand Down