コード例 #1
0
ファイル: weatherIn.py プロジェクト: shivani1998cs/ChatBot
def main(self, s):
    loc = s.replace('weather ', '').replace('in ', '')  # Trim input command to get only the location

    # Checks country
    country = mapps.getLocation()['country_name']

    # If country is US, shows weather in Fahrenheit
    if country == 'United States':
        send_url = (
            "http://api.openweathermap.org/data/2.5/weather?q={0}"
            "&APPID=ab6ec687d641ced80cc0c935f9dd8ac9&units=imperial".format(loc)
        )
        unit = ' ºF in '

    # If country is not US, shows weather in Celsius
    else:
        send_url = (
            "http://api.openweathermap.org/data/2.5/weather?q={0}"
            "&APPID=ab6ec687d641ced80cc0c935f9dd8ac9&units=metric".format(loc)
        )
        unit = ' ºC in '
    r = requests.get(send_url)
    j = json.loads(r.text)
    temperature = j['main']['temp']
    description = j['weather'][0]['main']
    print(Fore.BLUE + "It's " + str(temperature) + unit + str(loc.title()) + " (" + str(description) + ")" + Fore.RESET)
コード例 #2
0
def main(self, s):
    cmd_key_words = ['check', 'weather', 'forecast', 'in', 'for']
    cmd_words = s.strip().split()
    # location will be defined by the words given that are not the key words
    location = ' '.join(
        filter(lambda word: word.lower() not in cmd_key_words,
               cmd_words)).strip()

    current_location = mapps.getLocation()

    # if no location is given, use the current location
    if not location:
        location = "{},{}".format(current_location['city'],
                                  current_location['country_code'])

    country = current_location['country_name']

    # If country is not US, shows weather in Celsius
    units = {'url_units': 'metric', 'str_units': 'ºC'}
    # If country is US, shows weather in Fahrenheit
    if country == 'United States':
        units = {'url_units': 'imperial', 'str_units': 'ºF'}

    send_url = (
        "http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&cnt={1}"
        "&APPID=ab6ec687d641ced80cc0c935f9dd8ac9&units={2}".format(
            location, '7', units['url_units']))

    r = requests.get(send_url)
    j = json.loads(r.text)

    week_from_today = WeekDay().get_week_from_today()

    try:
        print_say(
            "Weather forecast in {} for the next 7 days.".format(
                j['city']['name'].title()), self, Fore.BLUE)
        for cnt, day_dict in enumerate(j['list']):
            print_say("{}:".format(week_from_today[cnt]), self, Fore.BLUE)
            print_say("\tWeather: {}".format(day_dict['weather'][0]['main']),
                      self, Fore.BLUE)
            print_say(
                "\tMax temperature: {} {}".format(
                    round(day_dict['temp']['max'], 1), units['str_units']),
                self, Fore.BLUE)
            print_say(
                "\tMin temperature: {} {}\n".format(
                    round(day_dict['temp']['min'], 1), units['str_units']),
                self, Fore.BLUE)
    except KeyError:
        print_say("The forecast information could not be found.", self,
                  Fore.RED)
コード例 #3
0
ファイル: weather_pinpoint.py プロジェクト: bwh91/Jarvis
def main(MEMORY):
    location = MEMORY.get_data('city')  # Will return None if no value
    if location is None:
        loc = str(location)
        city = mapps.getLocation()['city']
        print(Fore.RED + "It appears you are in " + city +
              " Is this correct? (y/n)" + Fore.RESET)

        try:
            i = raw_input()
        except:
            i = input()
        if i == 'n' or i == 'no':
            print("Enter Name of city: ")
            try:
                i = raw_input()
            except:
                i = input()
            city = i

        mapps.weather(str(city))

        MEMORY.update_data('city', city)
        MEMORY.save()
    else:
        loc = str(location)
        city = mapps.getLocation()['city']
        if city != loc:
            print(Fore.RED + "It appears you are in " + city +
                  ". But you set your location to " + loc + Fore.RESET)
            print(Fore.RED + "Do you want weather for " + city +
                  " instead? (y/n)" + Fore.RESET)
            try:
                i = raw_input()
            except:
                i = input()
            if i == 'y' or i == 'yes':
                try:
                    print(Fore.RED + "Would you like to set " + city +
                          " as your new location? (y/n)" + Fore.RESET)
                    try:
                        i = raw_input()
                    except:
                        i = input()
                    if i == 'y' or i == 'yes':
                        MEMORY.update_data('city', city)
                        MEMORY.save()

                    mapps.weather(city)
                except:
                    print(Fore.RED + "I couldn't locate you" + Fore.RESET)
            else:
                try:
                    mapps.weather(loc)
                except:
                    print(Fore.RED + "I couldn't locate you" + Fore.RESET)
        else:
            try:
                mapps.weather(loc)
            except:
                print(Fore.RED + "I couldn't locate you" + Fore.RESET)