import requests
import json


def get_weather(city_name, weather_api):
    """
    given a city name and api key for Open Weather Map, return local weather conditions

    :param city_name: name of the city
    :type city_name: str

    :param weather_api: Open Weather Map API
    :type weather_api: str

    :return: results dictionary
    """

    response = requests.get(
        "https://community-open-weather-map.p.rapidapi.com/weather?mode=json&q={}".format(city_name),
        headers={
            "X-RapidAPI-Host": "community-open-weather-map.p.rapidapi.com",
            "X-RapidAPI-Key": weather_api
        })

    content = json.loads(response.content.decode('utf8').replace("'", '"'))

    res_dict = dict()
    res_dict['humidity'] = content['main']['humidity']
    res_dict['temp'] = round((content['main']['temp'] - 273.15), 1)
    res_dict['temp_max'] = round((content['main']['temp_max'] - 273.15), 1)
    res_dict['temp_min'] = round((content['main']['temp_min'] - 273.15), 1)
    res_dict['weather'] = content['weather'][0]['main']

    return res_dict