How to allow users to enter number as a parameter in an API url call

Question:

I just started learning python today, so please take it easy. I have created a method that allows someone to enter a number and it will deliver the park name and weather for that location from MLB and Weather.gov. I have them hard coded for a couple of test cases to make sure it works. I want for the user to be able to input the venue number in it so that I can display the information for the proper location. I have searched around quite a bit but I can’t seem to find exactly what Im looking for. For example, in the following url: https://statsapi.mlb.com/api/v1/venues/**3289**?hydrate=location, I want the user to pick the number that goes right there. Once I am able to do this, I should be able to take the latitude and longitude from the API call and use that in the weather API call. I’m just stuck right now. I am using the command line so all I need is for someone to be able to input mlbweather(xxx) and hit return. I tried using params, but that just seems to append to the end of the url and adds a ? and equals, so that doesnt work.

def mlbweather(venueNum):
    citi = 3289
    wrigley = 17

    if venueNum == citi:
        mlb_api = requests.get('https://statsapi.mlb.com/api/v1/venues/3289?hydrate=location')
        mlb_data = mlb_api.text
        parse_json = json.loads(mlb_data)
        venueWanted = parse_json['venues'][0]['name']
        print("Venue:" + " " + venueWanted)
        weather_api = requests.get('https://api.weather.gov/gridpoints/OKX/37,37/forecast')
        weather_data = weather_api.text
        parse_json = json.loads(weather_data)
        weatherWanted = parse_json['properties']['periods'][0]['detailedForecast']
        print("Current Weather: n"  + weatherWanted)

    elif venueNum == wrigley:
        mlb_api = requests.get('https://statsapi.mlb.com/api/v1/venues/17?hydrate=location')
        mlb_data = mlb_api.text
        parse_json = json.loads(mlb_data)
        venueWanted = parse_json['venues'][0]['name']
        print("Venue:" + " " + venueWanted)
        weather_api = requests.get('https://api.weather.gov/gridpoints/LOT/74,75/forecast')
        weather_data = weather_api.text
        parse_json = json.loads(weather_data)
        weatherWanted = parse_json['properties']['periods'][0]['detailedForecast']
        print("Current Weather: n" + weatherWanted)

    else: 
        print("Either you typed an invalid venue number or we don't have that info")
Asked By: Steven Eck

||

Answers:

You’re looking for simple string concatenation:

def mlbweather(venueNum):
    mlb_api = requests.get('https://statsapi.mlb.com/api/v1/venues/' + str(venueNum) + '?hydrate=location')
    mlb_data = mlb_api.text
    parse_json = json.loads(mlb_data)
    venueWanted = parse_json['venues'][0]['name']
    print("Venue:" + " " + venueWanted)
    weather_api = requests.get('https://api.weather.gov/gridpoints/OKX/37,37/forecast')
    weather_data = weather_api.text
    parse_json = json.loads(weather_data)
    weatherWanted = parse_json['properties']['periods'][0]['detailedForecast']
    print("Current Weather: n"  + weatherWanted)

mlbweather(3289)

Venue: Citi Field
Current Weather: 
Patchy fog after 4am. Partly cloudy, with a low around 72. South wind 2 to 7 mph.

Alternatively, you can use fstrings:

mlb_api = requests.get(f'https://statsapi.mlb.com/api/v1/venues/{venueNum}?hydrate=location')
Answered By: dcsuka
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.