'Python flightradar24.api list iteration

Good evening, Trying to iterate over a list that scraped from flightradar24 api package.

Currently using these 2 lines for example :

flights = fr_api.get_flights()
# print(flights)
flights_list = flights[0:2]

Whenever trying to iterate over the flights_list variable:

for x in flights_list:
    if "N254TH" in x:
        print(x)

then getting an error:

TypeError: argument of type 'Flight' is not iterable

As the generated list is not separated but comes as a whole list separated by comma but looks like 1 element. Is there a way to iterate over it as i would need to fetch only data if latitude is greater than specified.

[<(BALL) N254TH - Altitude: 64600 - Ground Speed: 2 - Heading: 116 - Latitude 21.1318 - Longtitude -156.9354 - From N/A - To N/A>, <(GLID) STM32 - Altitude: 2263 - Ground Speed: 1 - Heading: 123 - Latitude -33.3779 - Longtitude -70.5793 - From N/A - To N/A>]

Here is Flight class that's imported from package.

class Flight(object):

    __default_text = "N/A"

    def __init__(self, flight_id, info):

        self.id = flight_id
        self.icao_24bit = self.__get_info(info[0])
        self.latitude = self.__get_info(info[1])
        self.longitude = self.__get_info(info[2])
        self.heading = self.__get_info(info[3])
        self.altitude = self.__get_info(info[4])
        self.ground_speed = self.__get_info(info[5])
        self.squawk = self.__get_info(info[6])
        self.aircraft_code = self.__get_info(info[8])
        self.registration = self.__get_info(info[9])
        self.time = self.__get_info(info[10])
        self.origin_airport_iata = self.__get_info(info[11])
        self.destination_airport_iata = self.__get_info(info[12])
        self.number = self.__get_info(info[13])
        self.airline_iata = self.__get_info(info[13][:2])
        self.on_ground = self.__get_info(info[14])
        self.vertical_speed = self.__get_info(info[15])
        self.callsign = self.__get_info(info[16])
        self.airline_icao = self.__get_info(info[18])

    def __get_info(self, info):

        return info if (info or info == 0) and info != self.__default_text else self.__default_text

    def __repr__(self):

        return self.__str__()

    def __str__(self):

        template = "<({}) {} - Altitude: {} - Ground Speed: {} - Heading: {} - Latitude {} - Longtitude {} - From {} - To {}>"
        return template.format(self.aircraft_code, self.registration, self.altitude, self.ground_speed, self.heading, self.latitude, self.longitude, self.origin_airport_iata, self.destination_airport_iata)


Solution 1:[1]

You can try this

for x in flights_list:
    if "Specified Latitude As Float" < x.Latitude:
        print(x)

Give it a try hope it helps as I understand the case

Solution 2:[2]

Changed str function in the Flight class to :

def __str__(self):
        R = 6373.0
        # latitude of the specific location
        latitude_home = math.radians(53.008759)
        # longtitude of the specific location
        longtitude_home = math.radians(-0.410570)
        # latitude of the aircraft
        latitude_plane = math.radians(self.latitude)
        # longtitude of the aircraft
        longtitude_plane = math.radians(self.longitude)

        dlat = latitude_plane - latitude_home
        dlon = longtitude_plane - longtitude_home
        a = math.sin(dlat / 2)**2 + math.cos(latitude_home) * \
            math.cos(latitude_plane) * math.sin(dlon / 2)**2
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
        distance = round(R * c, 2)
        template = "<({}) {} - Altitude: {} - Ground Speed: {} - Heading: {} - Latitude {} - Longtitude {} - From {} - To {} - Distance {}>"
        if distance <= 200:
            return template.format(self.aircraft_code, self.registration, self.altitude, self.ground_speed, self.heading, self.latitude, self.longitude, self.origin_airport_iata, self.destination_airport_iata, distance)
        else:
            return ""

Have to use else: return at the end of the code otherwise getting NoneType error. But this leads to another issue.. now getting a list with empty elements which can't be removed..

, <(B738) G-JZHO - Altitude: 3250 - Ground Speed: 164 - Heading: 198 - Latitude 52.0113 - Longtitude 0.4066 - From ACE - To STN - Distance 123.97>, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 

Solution 3:[3]

To filter flights by their registration, you should use the registration attribute. Then, your code should look like this:

for x in flights_list:
    if "N254TH" == x.registration:
        print(x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Badrelden Ahmed
Solution 2 Stag
Solution 3 JeanExtreme002