Back to articles
Build a Weather CLI Tool in 20 Lines of Python (Using a Free API)
How-ToTools

Build a Weather CLI Tool in 20 Lines of Python (Using a Free API)

via Dev.to TutorialAlex Spinov

I wanted a weather check in my terminal. No browser, no app, just type weather Berlin and get the answer. 20 lines of Python later, I had it. The Complete Script import requests import sys def weather ( city ): url = f " https://api.open-meteo.com/v1/forecast " # First, geocode the city geo = requests . get ( f " https://geocoding-api.open-meteo.com/v1/search?name= { city } &count=1 " ). json () if not geo . get ( ' results ' ): print ( f " City ' { city } ' not found " ) return lat = geo [ ' results ' ][ 0 ][ ' latitude ' ] lon = geo [ ' results ' ][ 0 ][ ' longitude ' ] name = geo [ ' results ' ][ 0 ][ ' name ' ] country = geo [ ' results ' ][ 0 ]. get ( ' country ' , '' ) # Get weather w = requests . get ( url , params = { ' latitude ' : lat , ' longitude ' : lon , ' current_weather ' : True }). json ()[ ' current_weather ' ] print ( f " \n { name } , { country } " ) print ( f " Temperature: { w [ ' temperature ' ] } C " ) print ( f " Wind: { w [ ' windspeed ' ] } km/h " ) print ( f

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles