Search code examples
pythonopenweathermapweather-api

How can i calculate the values in the weather api


How can i calculate the response from the weather api like:

a = x["wind"]["speed"] returns like 3.09 
b = x["wind"]["deg"] return 36
c = x["visibility"] returns 2200
d = x["main"]["sea_level"] returns 1004
e = x["main"]["grnd_level"] returns 979
f = x["sys"]["sunrise"] returns like 1621468669
g = x["sys"]["sunset"] return like 1621517866

how can i calculate the time of f and g | speed of a | direction of b | visibility of c | and level of d and e in meters


Solution

  • If I am not mistaken, those integer values are UNIX timestamps. To get the corresponding datetime, do this:

    from datetime import datetime
    
    dateobjf = datetime.fromtimestamp(f+x["timezone"])
    dateobjg = datetime.fromtimestamp(g+x["timezone"])
    

    Edit: x["timezone"] added to return time in some given timezone. Edit 2: For code to print out the GMT offset:

    hrs = abs(x["timezone"])//3600
    mins = abs(x["timezone"])//60-hrs*60
    if x["timezone"]>0:
        tzinfo = f"GMT +{hrs}:{mins:02d}"
    elif x["timezone"]==0:
        tzinfo = "GMT 0:00"
    else:
        tzinfo = f"GMT -{hrs}:{mins:02d}"
    print(tzinfo)