pythonpandasurlrequest

How do you make a request to OpenSky (flight tracker)


I am making a flight tracker that will track planes within certain areas around airports. This is my code so far (a modified tutorial)

#import
import requests
import json

import pandas as pd

#square shape
lon_min=-123.063498
lat_min=-49.082514
lon_max=-123.295758
lat_max=49.308148

#requests
user_name="wvzack"
password="########"
url_data="https://" + user_name + ":" + password + "@opensky-network.org/api/states/all?"+'lamin=' + str(lat_min) +'&lomin=' + str(lon_min) + '&lamax=' + str(lat_max) + '&lomax=' + str(lon_max)
response=requests.get(url_data).json()

#pandas
col_name=['icao24','callsign','origin_country','time_position','last_contact','long','lat','baro_altitude','on_ground','velocity',       
'true_track','vertical_rate','sensors','geo_altitude','squawk','spi','position_source']


flight_df=pd.DataFrame(response['states'])
flight_df=flight_df.loc[:,0:16]
flight_df.columns = col_name
flight_df=flight_df.fillna('No Info')
flight_df.head()
print(flight_df)

I have tried changing the login code but nothing really happened. I am getting this error:

ValueError: Length mismatch: Expected axis has 0 elements, new values have 17 elements or this error:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='wvzack', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000002079A603450>: Failed to resolve 'wvzack' ([Errno 11001] getaddrinfo failed)")) (I just get one of these sometimes both). Sorry if this post is not made properly this is my first time using stackoverflow.


Solution

  • The second error is because you url is malformed. the host should not be your username. Instead of passing you user and password in the url, use the HTTPBasicAuth method to pass the auth info, like this:

    import requests
    from requests.auth import HTTPBasicAuth
    
    #requests
    user_name="wvzack"
    password="########"
    basic = HTTPBasicAuth('user', 'pass')
    
    url_data="https://opensky-network.org/api/states/all?"+'lamin=' + str(lat_min) +'&lomin=' + str(lon_min) + '&lamax=' + str(lat_max) + '&lomax=' + str(lon_max)
    response=requests.get(url_data, auth=basic).json()
    

    to get the data, and inspect what you get. The error you get about the 17 features is because you have 0 columns and trying to rename them to a set of 17, which means the dataframe is empty.

    This should be resolved when you actually get the data properly.