AWeirdDev / flights

Fast, robust Google Flights scraper (API) for Python.
https://pypi.org/project/fast-flights
16 stars 4 forks source link

Cookies & parse_response issues with V1 #3

Open JobeOneKenobi opened 1 month ago

JobeOneKenobi commented 1 month ago

V1 works the same using the v0 functionality, but when I try the new formatting for the get_flights, two issues:

  1. the cookies=Cookies.new() line, the Cookies reference is not working
  2. the parse_response function is breaking somehow, it's trynig to split a none value.

I'm testing it in an isolated environment with a simple main script, and installed it with pip only here is my test code

from fast_flights import FlightData, Passengers, create_filter, get_flights

Create a new filter

filter = create_filter( flight_data=[

Include more if it's not a one-way trip

    FlightData(
        date="2024-09-02",  # Date of departure
        from_airport="RSW",
        to_airport="DCA"
    ),
    # ... include more for round trips and multi-city trips
],
trip="one-way",  # Trip (round-trip, one-way, multi-city)
seat="economy",  # Seat (economy, premium-economy, business or first)
passengers=Passengers(
    adults=1,
    children=0,
    infants_in_seat=0,
    infants_on_lap=0
),

)

Get flights with a filter

result = get_flights( filter, dangerously_allow_looping_last_item=True, cookies=Cookies.new().to_dict(), currency="USD", language="en" )

The price is currently... low/typical/high

print("The price is currently", result.current_price)

Display the first flight

print(result.flights[0])

AWeirdDev commented 1 month ago

Hey there,

Could you please clarify what "Cookies reference is not working" mean? As for the broken parse_response() function, could you please provide an error log pointing where the error occurred or what type of error was it?

P.S. I was in a rush so I wasn't aware of my code being that broken, thanks for pointing this out. I'll look into the code tomorrow (if possible). Thanks!

Best

JobeOneKenobi commented 1 month ago

Yes, I'll confess I'm a novice with Github, and by no means any expert coder.

For the cookies thing actually it looks like I just needed to add an import line: image

JobeOneKenobi commented 1 month ago

Ope, tack that back, when I do that it gives this error: C:\Users\jobel\PycharmProjects\pythonProject\venv\Scripts\python.exe "C:\Users\jobel\PycharmProjects\pythonProject\Flight Helper\main.py" Traceback (most recent call last): File "C:\Users\jobel\PycharmProjects\pythonProject\Flight Helper\main.py", line 2, in from fast_flights.cookies_impl import Cookies File "C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\cookies_impl.py", line 5, in from .cookies_pb2 import Cookies as SOCS, Information, Datetime # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ImportError: cannot import name 'Cookies' from 'fast_flights.cookies_pb2' (C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\cookies_pb2.py)

Process finished with exit code 1

JobeOneKenobi commented 1 month ago

But anyways this is the error code for running it without the cookies line, that is the issue with parsing the response which seems to be the bigger problem (I haven't encountered any need for the cookies thing yet anyway):

Traceback (most recent call last): File "C:\Users\jobel\PycharmProjects\pythonProject\Flight Helper\main.py", line 28, in result = get_flights( ^^^^^^^^^^^^ File "C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\core.py", line 119, in get_flights results = parse_response( ^^^^^^^^^^^^^^^ File "C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\core.py", line 70, in parse_response departure_time = dp_ar_node[0].text(strip=True)


IndexError: list index out of range

Process finished with exit code 1

###
Now you mentioned a log file? I'm assuming you mean adding code that saves out errors like this to a log / txt file so that I don't have to copy past efrom the output screen? That is a good idea I should do that.
AWeirdDev commented 1 month ago

Hi there,

I've updated fast-flights to v1.2.

Cookies couldn't be imported because of the missing import in __init__.py. As for the IndexError, I assume it's because the departure time is not present sometimes.

pip install -U fast-flights

Let me know if I made other silly mistakes!

Best, AWeirdDev

JobeOneKenobi commented 1 month ago

Alright new, and even wierder, parse response error. Old ones seem to to be resolved using same modified example code, updated to 1.2 and got the following error:

Traceback (most recent call last): File "C:\Users\jobel\SynologyDrive\PycharmProjects\pythonProject\flights_package_test.py", line 28, in result = get_flights( ^^^^^^^^^^^^ File "C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\core.py", line 127, in get_flights results = parse_response( ^^^^^^^^^^^^^^^ File "C:\Users\jobel\PycharmProjects\pythonProject\venv\Lib\site-packages\fast_flights\core.py", line 103, in parse_response "stops": 0 if stops == "Nonstop" else int(stops.split(" ", 1)[0]), ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''

Process finished with exit code 1

But here's what's wierd I went in and added some print statements to the nonstop parse / code and got this for this same code / result:

1 stop ['1', 'stop'] 1 stop ['1', 'stop'] 1 stop ['1', 'stop'] 2 stops ['2', 'stops'] 2 stops ['2', 'stops'] 1 stop ['1', 'stop'] 2 stops

So I have no clue how it's parsing / splitting out an empty string? '' Freaking wierd.

Here is my code just to be sure:

from fast_flights import FlightData, Passengers, create_filter, get_flights, Cookies

Create a new filter

filter1 = create_filter( flight_data=[

Include more if it's not a one-way trip

    FlightData(
        date="2024-08-21",  # Date of departure
        from_airport="IAD",
        to_airport="JFK"
    ),
    # ... include more for round trips and multi-city trips
],
trip="one-way",  # Trip (round-trip, one-way, multi-city)
seat="economy",  # Seat (economy, premium-economy, business or first)
passengers=Passengers(
    adults=1,
    children=0,
    infants_in_seat=0,
    infants_on_lap=0
),

)

Get flights with a filter

result = get_flights( filter1, dangerously_allow_looping_last_item=True, cookies=Cookies.new().to_dict(), currency="USD", language="en" )

The price is currently... low/typical/high

print("The price is currently", result.current_price)

Display the first flight

print(result.flights[1])

JobeOneKenobi commented 1 month ago

Tried a few different dates and airports