def __init__(self):
        self.properties = {}
        with open(WeatherAgent.__props_path__, "r") as props:
            self.properties = json.load(props)

        self.weatherbit_key = self.properties["weatherbit_api_key"]
        self.weather_api = Api(self.weatherbit_key)
示例#2
0
  def login(self):
    if self.login_data["apikey"] :
      self.api = Api(self.login_data["apikey"])

    else :
      warnings.warn('MWP_WARNING. The "%s" Weather Package(WP) has not been \
properly activated due to the apikey' % (self.name.upper()))
class WeatherAgent:
    """
    Class for get operations on weather based on city.
    """

    __props_path__ = os.path.join("resources", "properties.json")

    def __init__(self):
        self.properties = {}
        with open(WeatherAgent.__props_path__, "r") as props:
            self.properties = json.load(props)

        self.weatherbit_key = self.properties["weatherbit_api_key"]
        self.weather_api = Api(self.weatherbit_key)

    def get_weather_by_city(self, city):
        """
        Get the current weather details for a city.
        :param city:
        :return: Weather json
        """

        current = self.weather_api.get_current(city=city)
        #print(current.json)

        return current.json

    def get_avg_temp_for_city(self, city):
        """
        Function that returns the average temperature for city provided.
        :param city: <City>,<State> e.g. Boston,MA
        :return: Temperature in Celcius
        """
        end_date = datetime.date.today()
        start_date = end_date - datetime.timedelta(1)

        # Converting to the eventually expected formatted string to bypass date.hour check.
        # 'hour' somehow is not a part of the 'datetime.date'
        end_date = end_date.strftime('%Y-%m-%d:%H')
        start_date = start_date.strftime('%Y-%m-%d:%H')

        history = self.weather_api.get_history(city=city,
                                               start_date=start_date,
                                               end_date=end_date)

        history_json = history.json

        # Getting average from all sources.
        sum = 0
        for data_item in history_json["data"]:
            sum += data_item["temp"]

        avg = sum / len(history_json["data"])

        return avg
示例#4
0
 def __init__(self, api_key=None):
     self.weather_condition_templates = {
         "precipitation": {
             "chance": " there is a %s per cent chance of precipitation ",
             "snow": "with a possible % inches of snow "
         },
         "hi_lo": {
             "general":
             " with a high of %s degrees and a low of %s degree celsius",
             "average": " an average temperature of %s degree celsius"
         },
         "conditions": {
             "general": "will be %s",
             "general_hr": "The weather %s will be %s"
         }
     }
     if api_key is None:
         self.api_key = "b09681fb4107467b9563c75a73f1a4a1"
     self.api = Api(self.api_key)
示例#5
0
class weather_report:
    def __init__(self, api_key=None):
        self.weather_condition_templates = {
            "precipitation": {
                "chance": " there is a %s per cent chance of precipitation ",
                "snow": "with a possible % inches of snow "
            },
            "hi_lo": {
                "general":
                " with a high of %s degrees and a low of %s degree celsius",
                "average": " an average temperature of %s degree celsius"
            },
            "conditions": {
                "general": "will be %s",
                "general_hr": "The weather %s will be %s"
            }
        }
        if api_key is None:
            self.api_key = "b09681fb4107467b9563c75a73f1a4a1"
        self.api = Api(self.api_key)

    def query_weather(self, location):
        forecast = self.api.get_forecast(city=location)
        return forecast

    def resolve_time_diff(self, time_delta):
        time_tokens = time_delta.__str__().split(",")
        dd = 0
        hh_mm_ss = time_tokens[0]
        if len(time_tokens) > 1:
            dd = int(time_tokens[0].split(" ")[0])
            hh_mm_ss = time_tokens[1]
        time_split = hh_mm_ss.split(":")
        hh = int(time_split[0])
        return dd, hh

    def get_weather_report(self, location, query_time):
        assert query_time > datetime.datetime.now()
        dd, hh = self.resolve_time_diff(query_time - datetime.datetime.now())

        self.api.set_forecast_granularity('daily')
        dailyForecastPoints = self.query_weather(location).get_series(
            ['datetime', 'weather', 'max_temp', 'min_temp', 'precip'])
        self.api.set_forecast_granularity('3hourly')
        hourlyForecastPoints = self.query_weather(location).get_series(
            ['datetime', 'weather', 'temp', 'precip', 'snow', 'rh'])

        day_forecast_point = dd
        hour_forecast_point = dd * 8 + (hh // 3)
        day_weather = dailyForecastPoints[day_forecast_point]
        hour_weather = hourlyForecastPoints[hour_forecast_point]

        date_term = "today"
        hour_term = " around " + str(hh) + " hours from now"
        if dd > 0:
            date_term = "on " + str(query_time.day) + " " + str(
                query_time.month)

        day_report = self.weather_condition_templates["conditions"][
            "general"] % (day_weather["weather"]["description"])
        day_report += ". " + self.weather_condition_templates["hi_lo"][
            "general"] % (str(
                day_weather["max_temp"]), str(day_weather["min_temp"]))
        day_report += ". And " + self.weather_condition_templates[
            "precipitation"]["chance"] % (str(day_weather["precip"]))
        hour_report = self.weather_condition_templates["conditions"][
            "general_hr"] % (hour_term, hour_weather["weather"]["description"])
        hour_report += ". With " + self.weather_condition_templates["hi_lo"][
            "average"] % (str(hour_weather["temp"]))
        hour_report += ". And " + self.weather_condition_templates[
            "precipitation"]["chance"] % (str(day_weather["precip"]))

        return day_report, hour_report


# wr = weather_report()
# day_rep, hr_rep = wr.get_weather_report("Washington", datetime.datetime(2018, 4, 14, 0, 0))
# text_to_speech(day_rep + ". " + hr_rep)
# print(day_rep + ". " + hr_rep)
示例#6
0
from peewee import *
from raven import Client
from weatherbit.api import Api

load_dotenv(find_dotenv())

if os.environ.get('SENTRY_DSN'):
    sentry = Client(os.environ.get('SENTRY_DSN'))

db = MySQLDatabase(os.environ.get("DB_NAME"),
                   user=os.environ.get("DB_USER"),
                   host=os.environ.get("DB_HOST"),
                   password=os.environ.get("DB_PASS"),
                   charset='utf8mb4')

weather_api = Api(os.environ.get('WEATHER_API_KEY'))

API_URL = 'http://data-mobility.brussels/geoserver/bm_bike/wfs?service=wfs&version=1.1.0&request=GetFeature&typeName=bm_bike:rt_counting&outputFormat=csv'
EXPORT_PATH = os.path.join(os.path.dirname(__file__), 'public/data.json')
API_TIMEOUT = os.environ.get("API_TIMEOUT", 30)


class BikeCounter(Model):
    """ Contains the counting data, almost mimic the api fields """
    class Meta:
        """ Meta class for peewee db """
        database = db

    gid = TextField(default='')
    device_name = TextField(default='')
    road_nl = TextField(default='')
示例#7
0
    print('''\n--Something's up!! {} not in {}'''.format(
        model_best_cv, [model_best_val, model_best_test]))
    print('--Best model in CV not best in val and in test datasets/n')
# plot metrics
# df_metrics.pivot(index='df', columns='model', values='MSE').plot()
# df_metrics.dtypes

# get datetime column from date and period by mapping time and period with "df_map_HH_Period"
# plt.scatter(x = df_train['pred.GradBoostRegr'], y = df_train['dmd_coeff'])

###################################################################################################################################################
################
#### Step 5 ####
################
from weatherbit.api import Api  # pip install pyweatherbit
api_call = Api(key_API_WeatherFct)
api_call.set_granularity('daily')  # 'hourly', '3hourly'
df_weather_Fct = pd.DataFrame()
for iCity in list_cities:
    print('{}: Downloading Weather Forecast for {}'.format(
        ddt.now().strftime('%d%b%Y %H:%M:%S:'), iCity))
    tmp_df_weather_fct = pd.DataFrame(
        api_call.get_forecast(city=iCity, country='GB').get_series(
            ['temp', 'max_temp', 'min_temp', 'precip']))
    tmp_df_weather_fct['City'] = iCity
    df_weather_Fct = df_weather_Fct.append(tmp_df_weather_fct)
# simple daily average; more accurate would be to have a population- or other-weighted average temperature; other measure for wind speed
df_weather_Fct_avg = df_weather_Fct[[
    'datetime', 'temp'
]].groupby('datetime').mean().reset_index()
df_weather_Fct_avg['date'], df_weather_Fct_avg['time'] = df_weather_Fct_avg[
示例#8
0
def IvyAbility(command):
    
    #open website function/ability
    if "open reddit" in command:
        reg_ex = re.search('open reddit (.*)', command)
        url = 'https://www.reddit.com/'
        if reg_ex:
            subreddit = reg_ex.group(1)
            url = url + 'r/' + subreddit
        webbrowser.open(url)
        IvyResponse("Your cool shit reddit site has openned for you")
    elif 'open google' in command:
        reg_ex = re.search('open google(.*)', command)
        url = 'https://www.google.co.uk/'
        webbrowser.open(url)
        IvyResponse("Your requested website has opened for you")
    elif 'open' in command:
        reg_ex = re.search('open(.*)', command)
        if reg_ex:
            domain = reg_ex.group(1)
            url = 'https://www.' + domain
            webbrowser.open(url)
            IvyResponse('Opened')
    elif 'google' in command:
        reg_ex = re.search('google(.*)', command)
        if reg_ex:
            domain = reg_ex.group(1)
            url = 'https://www.google.com/search?q=' + domain
            webbrowser.open(url)
            IvyResponse("I has searched your requested on website. Take a look at what I found")
        else:
            pass
        
    #find weather and weather forecast function/ability
    elif 'current weather in' in command:
        reg_ex = re.search('current weather in (.*)', command)
        if reg_ex:
            city = reg_ex.group(1)
            owm = OWM(API_key='37a0944f43b76456cbc580615fe4c676')
            obs = owm.weather_at_place(city)
            w = obs.get_weather()
            k = w.get_status()
            x = w.get_temperature(unit='celsius')
            IvyResponse('Current weather in %s is %s. The maximum temperature is %0.2f and the minimum temperature is %0.2f degree celcius' % (city, k, x['temp_max'], x['temp_min']))
    elif 'weather forecast in' in command:
        reg_ex = re.search('weather forecast in (.*)', command)
        if reg_ex:
            api_key = 'fb14f083b77e4c99baf4f4b6e1d8c50d'
            api = Api(api_key)
            location = reg_ex.group(1)
            IvyResponse('How many days you want to forecast?')
            day_of_forecast = myCommand()
            forecast = api.get_forecast(days = day_of_forecast, city = location)
            forecast_list = forecast.get_series(['temp','max_temp','min_temp','weather'])
            IvyResponse("The weather forecast for " + str(location))
            weather_data = []
            for forecast in forecast_list:
                weather_data.append(f''' On {forecast['datetime'] : %d %B %Y},
                         the weather is {forecast['weather']['description']}. The temperature will be {forecast['temp']} celsius degree
                         with maximum temperature will be {forecast['max_temp']} and minimum temperature will be {forecast['min_temp']}
                         '''
                         )
            b = ''.join(weather_data)
            IvyResponse(b)
        
    #send email/mail funcion
    elif 'email' in command or 'mail' in command:
        IvyResponse("Who is the recipient?")
        recipient = myCommand()
        if "john" in recipient or "david" in recipient:
            recipient_mail_add = '*****@*****.**'
        elif "mine" in recipient or "my" in recipient:
            IvyResponse("You meant Mai, isn't it?")
            y_n = myCommand()
            if 'yes' in y_n or 'yeah' in y_n:
                recipient_mail_add = '*****@*****.**'
            else:
                pass
        else:
            IvyResponse("What is his or her mail address please?")
            recipient_mail_add = myCommand()
        IvyResponse('What is the subject of this email please?')
        subject_mail = myCommand()
        IvyResponse('What do you want to say in the email?')
        body = myCommand()
        IvyResponse('Do you want to attach anything to the mail?')
        att_ans = myCommand()
        if 'yes' in att_ans or 'yea' in att_ans:
            IvyResponse('What do you want to send sir?')
            file = myCommand()
            if 'song' in file:
                path = '/Users/baileyvu/Desktop/Bad Guy - Billie Eilish_Justin Bieber.m4a'
                file_name = path
                IvyResponse('Sending email through Gmail')
            elif 'ivy' in file:
                path = '/Users/baileyvu/Desktop/test_7.py'
                file_name = path
                IvyResponse('Sending email though gmail')
                try:
                    yag = yagmail.SMTP('*****@*****.**')
                    yag.send(
                            to=recipient_mail_add,
                            subject=subject_mail,
                            contents=body,
                            attachments=file_name)
                    IvyResponse('Mail sent')
                except sr.UnknownValueError:
                    IvyResponse('I can\'t understand what you mean!')
            
        else:
            IvyResponse('Sending your requested Mail')
            try:
                yag = yagmail.SMTP('*****@*****.**')
                yag.send(
                        to=recipient_mail_add,
                        subject=subject_mail,
                        contents=body
                        )
                IvyResponse('Mail sent')
            except sr.UnknownValueError:
                IvyResponse('I can\'t understand what you mean!')
    
    #'tell me about' function
    elif 'tell me about' in command:
        reg_ex = re.search('tell me about (.*)', command)
        try:
            if reg_ex:
                topic = reg_ex.group(1)
                ny = wikipedia.page(topic)
                detail = ny.content[:1000].encode('utf-8')
                IvyResponse(str(detail))
        except Exception as e:
            IvyResponse(e)
            
    #telling time function
    elif 'time' in command:
        now = datetime.datetime.now()
        format_time = '%I:%M %p'
        ans1 = 'It is ' + now.strftime(format_time) + ' at the moment.'
        ans2 = 'Current time is ' + now.strftime(format_time)
        IvyResponse(random.choice([ans1, ans2]))
    elif 'what day is' in command:
        reg_ex = re.search('what day is (.*)', command)
        try:
            if reg_ex:
                dt = reg_ex.group(1)
                time_format = datetime.datetime.strptime(dt, '%d %B %Y')
                tf = datetime.datetime.strftime(time_format, '%d/%-m/%Y')
                day, month, year = (int(x) for x in tf.split('/'))    
                ans = datetime.date(year, month, day)
                IvyResponse(' It is ' + ans.strftime("%A") + ' on ' + dt)
        except Exception as e:
                now = datetime.datetime.now()
                format_day = '%A %d %B, %Y'
                IvyResponse('Today is ' + now.strftime(format_day))
    
    #open application
    elif 'launch' in command:
        reg_ex = re.search('launch\s(.*)', command)
        if reg_ex:
            appname = reg_ex.group(1)
            appname1 = appname + ".app"
            subprocess.Popen(["open", "-n", "/Applications/" + appname1], stdout=subprocess.PIPE)
        IvyResponse("I have launch the application as you wanted, No thanks needed!")
    
    #Using wolframalpha to calculate, tell weather in a specific city + specific day.
    elif 'calculate' in command:
        app_id = "PUK3TX-59QXTTY2EY"
        client = wolframalpha.Client(app_id)
        indx = command.lower().split().index('calculate')
        query = command.split()[indx +1:]
        res = client.query(' '.join(query))
        answer = next(res.results).text
        IvyResponse("The answer is " + answer)
        return
    elif 'information' in command:
        IvyResponse('What do you need to know, ' + name + '?')
        ans = myCommand()
        if 'specific' in str(ans) or 'temperature' in str(ans):
            IvyResponse("Where to, sir?")
            temp_location = myCommand()
            IvyResponse('Which day?')
            temp_time = myCommand()
            IvyResponse('On it! Give me a sec')
            app_id = "PUK3TX-59QXTTY2EY"
            client = wolframalpha.Client(app_id)
            res = client.query('temperture in ' + temp_location + ' on ' + temp_time)
            answer = next(res.results).text
            IvyResponse("It is " + answer)
        else:
            pass
示例#9
0
def store_to_db(lat, lon, start_date, end_date, db_name, db_password, db_user,
                db_host):
    """
    Queries the weatherbit using API/requests to retrieve information from desired <start_date> to <end_date>
    and stores in weather table.

    :param lat: lattitude coordinate
    :param lon: longtitude coordinate
    :param start_date: date object
    :param end_date: date object
    :return void:
    """
    api_key = os.environ.get('API_KEY')
    api = Api(api_key)

    try:
        start_date = datetime.strptime(start_date, '%Y-%m-%d')
        end_date = datetime.strptime(end_date, '%Y-%m-%d')

        conn = psycopg2.connect(dbname=db_name,
                                password=db_password,
                                user=db_user,
                                host=db_host)
        cur = conn.cursor()

        if start_date < end_date:

            for n in range((end_date - start_date).days):
                sdate = start_date + timedelta(n)
                edate = start_date + timedelta(n + 1)

                # Using an API Wrapper
                # api.set_granularity('daily')
                # history = api.get_history(lat=lat, lon=lon, start_date=str(sdate.date()), end_date=str(edate.date()))
                # print(history.get_series(['temp','min_temp','max_temp', 'min_temp_ts', 'max_temp_ts']))

                ## Using the API directly
                response = requests.get(
                    "https://api.weatherbit.io/v2.0/history/daily?lat=" +
                    str(lat) + "&lon=" + str(lon) + "&start_date=" +
                    str(sdate.date()) + "&end_date=" + str(edate.date()) +
                    "&key=" + api_key)

                if response.status_code == 200:
                    query = """ INSERT INTO weather (lat, lon, mean_temp, min_temp, min_temp_time, max_temp, max_temp_time) VALUES (%s,%s,%s,%s,%s,%s,%s) """
                    # query = """ INSERT INTO test_weather (lat, lon, mean_temp, min_temp, min_temp_time, max_temp, max_temp_time) VALUES (%s,%s,%s,%s,%s,%s,%s) """
                    record = (lat, lon, response.json()["data"][0]["temp"],
                              response.json()["data"][0]["min_temp"],
                              datetime.fromtimestamp(
                                  int(response.json()["data"][0]["min_temp_ts"]
                                      )).strftime('%Y-%m-%d %H:%M:%S'),
                              response.json()["data"][0]["max_temp"],
                              datetime.fromtimestamp(
                                  int(response.json()["data"][0]["max_temp_ts"]
                                      )).strftime('%Y-%m-%d %H:%M:%S'))
                    cur.execute(query, record)

        conn.commit()
        conn.close()

    except Exception as e:
        print("Exiting store_to_db due to exception: ", e.__class__)

    finally:
        if conn:
            cur.close()
            conn.close()
from datetime import date, datetime
import credentials
import time
import calendar

sense = SenseHat()

e = (0, 0, 0)  # empty
o = (255, 128, 0)  # orange
w = (200, 200, 200)  # white
r = (255, 51, 51)  # red
db = (0, 128, 255)  # deep_blue
dg = (96, 96, 96)  # deep_grey

api_key = credentials.weatherbit_api_key
api = Api(api_key)
api.set_granularity('daily')
forecast_Rbg = api.get_forecast(city="Regensburg,DE")
forecast_Ingl = api.get_forecast(city="Ingolstadt,DE")


class Display_Weekdays:
    def __init__(self):
        my_date = date.today()
        self.weekday = calendar.day_name[my_date.weekday()]

    def display_weekday_shortcut(self):
        weekday_shortcut = ((self.weekday)[0:3]).upper()
        sense.show_message(weekday_shortcut)

# Class for making Weatherbit API calls. This class will be instantiated as a
# singleton to gather all necessary information from the API.
from weatherbit.api import Api
import API_Key
import json

# Get the private key string from API_KEY.
api_key = API_Key.API_Key().Weatherbit_Key

# Authenticate our API Key. This variable will be used for all API calls.
weatherbit = Api(api_key)

# Set the API for retrieving the daily forecast.
weatherbit.set_granularity('daily')


# Gets the forecast for the current day.
# Params:
#   lat:    Float: The latitude of the city.
#   long:   Float: The longitude of the city.
# Return: List containing the precipitation, average temperature, and max
# temperature per hour from midnight to the current hour.
def get_daily_forecast(lat, long):
    forecast = weatherbit.get_forecast(lat=lat, lon=long)
    precip = forecast.json['data'][0]['precip']
    precip = precip / 25.4  # Convert mm to inches
    avg_temp = forecast.json['data'][0]['temp']
    avg_temp = avg_temp * 1.8 + 32  # Convert Celsius to Fahrenheit
    max_temp = forecast.json['data'][0]['max_temp']
    max_temp = max_temp * 1.8 + 32
    weather = [precip, avg_temp, max_temp]
示例#12
0
class weatherbit(awp): 
  def __init__(self, location, listOfTimePredictions) :
    self.name = "weatherbit"
    self.allowedTimePredictions = "N-N-0"
    
    super(weatherbit, self).__init__(self.name, listOfTimePredictions)

    self.login()
    self.setLocation(location)

  def login(self):
    if self.login_data["apikey"] :
      self.api = Api(self.login_data["apikey"])

    else :
      warnings.warn('MWP_WARNING. The "%s" Weather Package(WP) has not been \
properly activated due to the apikey' % (self.name.upper()))
   
  def setLocation(self,location):
    self.forecast = self.api.get_forecast(lat=location[0],lon=location[1])
  
  def update(self,days=["ALL"]) :
    listOfTimePredictions = self.WD.keys()
    
    for api_periods in self.weather.keys() :
      if api_periods    == "daily" :
        self.api.set_granularity('daily')
        self.weather[api_periods] = self.forecast.get_series(['temp','precip','rh','wind_spd'])
      elif api_periods  == "hourly" :
        self.api.set_granularity('hourly')
        self.weather[api_periods] = self.forecast.get_series(['temp','precip','rh','wind_spd'])
      elif api_periods  == "currently" :
        self.api.set_granularity('hourly')
        self.weather[api_periods] = self.forecast.get_series(['temp','precip','rh','wind_spd'])[0]

    for timePrediction in listOfTimePredictions :
      self.setTemperature(timePrediction)
      self.setWindVelocity(timePrediction)
      self.setRainProbability(timePrediction)
      self.setRainIntensity(timePrediction)
      self.setRelativeHumidity(timePrediction)

  def setTemperature(self,timePrediction="0-0-0"):
    #Units: ºC
    value = self.setWeatherProperty(timePrediction,'temp') 
    self.WD[timePrediction]["Temperature"] = value

  def setWindVelocity(self,timePrediction="0-0-0"):
    #Units: m/s
    value = self.setWeatherProperty(timePrediction,'wind_spd') 
    self.WD[timePrediction]["WindVelocity"] = value

  def setRainProbability(self,timePrediction="0-0-0"):
    #Units: Percentatge [0-1]
    #Warning: Weatherbit does not calculate Weather Prediction
    self.WD[timePrediction]["RainProbability"] = None

  def setRainIntensity(self,timePrediction="0-0-0"):
    #Units: mm/hour
    value = self.setWeatherProperty(timePrediction,'precip') 
    self.WD[timePrediction]["RainIntensity"] = value

  def setRelativeHumidity(self,timePrediction="0-0-0"):
    #Units: Percentatge [0-1]
    value = self.setWeatherProperty(timePrediction,'rh') 
    self.WD[timePrediction]["RelativeHumidity"] = value/100.0 if value else value

  def setWeatherProperty(self,timePrediction,WP) :
    value = None
    day, hour, minute, predictionType = decomposeTimePrediction(timePrediction)

    try :
      if day :
        value = self.weather[predictionType][day][WP]
      elif hour : # N hours
        value = self.weather[predictionType][hour][WP]
      elif not day  and \
           not hour and \
           not minute :
        value = self.weather[predictionType][WP]
    except :
      print("WP[%s]: \"%s\" is not available for \"%s\" prediction. " % (self.name,WP,predictionType))

    return value
示例#13
0
from machine import Pin
from time import localtime, sleep
from weatherbit.api import Api
import threading

api_key = "key"
lat = 49.44684
lon = 8.357229999999959
api = Api(api_key)
api.set_granularity('daily')


class MQTT_subcriber():  #Class for controlling the ESP
    pass


def auto_water(
):  #This function looks what the forecast says and let flow enough water for everything
    pass


def manuell():  #function for manuell control e.g. if it is not enough water
    pass


threading.Thread(target=auto_water).start()
threading.Thread(target=manuell).start()
示例#14
0
import os
import requests
import json
import geocoder

from slackclient import SlackClient
from weatherbit.api import Api
import weatherConditions as weatherConditions

degree_sign = u'\N{DEGREE SIGN}'

# Get Api Key
api_key = str(os.environ.get("WEATHERBIT_API_KEY"))

# init api
api = Api(api_key)

# Weather Advice
listWeather = weatherConditions.get_all_weather_conditions()

# Responses
fillerOne = ["Looks like ", "Seems like ", ""]


def get_weather_no_location():
    base_url = "https://api.weatherbit.io/v2.0/current?ip=auto&units=I&key=" + api_key
    r = requests.get(base_url)
    weatherConditions = []
    try:
        j = json.loads(r.text)
        for x in j['data']:
示例#15
0
    elif (temperature < 45):
        return "8"
    elif (temperature < 50):
        return "9"
    elif (temperature < 55):
        return "A"
    elif (temperature < 60):
        return "B"
    elif (temperature < 65):
        return "C"
    elif (temperature < 70):
        return "D"
    elif (temperature < 75):
        return "D"
    elif (temperature < 80):
        return "F"


api_key = "388a906c36084818ba4688a859cabee1"
api = Api(api_key)
key_values = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
              "D", "E", "F")
rtn_key = ""
for i in range(0, 32):
    lat = random.randrange(-90, 90)
    lon = random.randrange(-180, 80)
    forecast = api.get_forecast(lat=lat, lon=lon)
    temp = forecast.get_series(['temp'])[0]['temp']
    rtn_key += temp_map(temp)
print(rtn_key)
示例#16
0
def weatherapi(year="2017"):
    # variaveis e paths necessárias
    api_key = "af22cf6a216a427fb88685100b43d048"
    pathfolder = "Dataset" + str(year)
    path = os.path.join("Ardidas" + str(year) + "final",
                        "ardidas" + str(year[-2:]) + ".shp")
    sf = shapefile.Reader(path)
    json_folder = check_jsonstats_folder("JsonApis")
    verify_filename = "LastIndex_WeatherApi" + " " + str(year) + ".json"
    jverifypath = os.path.join(json_folder, verify_filename)
    json_requests = "Requests" + "_forKey_ " + f"{api_key}" + ".json"
    jrequestspath = os.path.join(json_folder, json_requests)
    timefile = "Last_time" + "_forKey_ " + f"{api_key}" + ".json"
    jtimefilepath = os.path.join(json_folder, timefile)
    jsonstring = "Last Iteration" + " " + str(year)
    counter_requests = "Counter"
    lastdate = "Lastdate"
    verify = False
    # verificar existência e extrair info de jsons de controlo
    # controlo de nº de requests/ timestamp em que se atingiu esse limite/ ultimo index no momento paragem
    my_file_index = Path(jverifypath)
    my_file_requests = Path(jrequestspath)
    my_file_lasttime = Path(jtimefilepath)

    if my_file_requests.is_file():
        with open(jrequestspath, "r") as file:
            data_r = file.read()
        file_dict = json.loads(data_r)
        number_requests = file_dict[counter_requests]
    else:
        number_requests = 0

    if my_file_lasttime.is_file():
        with open(jtimefilepath, "r") as file:
            data_t = file.read()
        file_dict = json.loads(data_t)
        last_date = file_dict[lastdate]
        last_date_obj = datetime.strptime(last_date, "%d/%m/%Y %H:%M:%S")
        delta = datetime.now() - last_date_obj
        delta_in_sec = delta.total_seconds()
        delta_in_hours = divmod(delta_in_sec, 3600)[0]
        if delta_in_hours < 24:
            sys.exit(
                "Ainda não passou o periodo de espera para efetuar requests à api!"
            )
        else:
            pass
    else:
        pass

    if my_file_index.is_file():

        with open(jverifypath, "r") as file:
            data = file.read()

        file_dict = json.loads(data)

        index = file_dict[jsonstring]

    else:
        index = 0

    api = Api(api_key)

    # ciclo para percorrer a totalidade do dataset
    for i in range(index, len(sf)):

        if my_file_requests.is_file():
            verify = verify_last_request_date(jrequestspath)
            update_verify_jsontime(number_requests, jtimefilepath, verify)

        shape = sf.shapeRecord(i)

        pathjson = os.path.join(pathfolder, str(i))
        filename = "Weather.json"
        filepath = os.path.join(pathjson, filename)

        longitude, latitude = centerpointshape(shape)

        if int(year) == 2017:
            DHInicio = shape.record.DHInicio
            DHFim = shape.record.DHFim

        else:
            DHInicio = shape.record.data_inici
            DHFim = shape.record.data_fim

        # formatar data extraida do shapefile
        date_format_str = '%Y-%m-%d %H:%M:%S.%f'
        start = datetime.strptime(DHInicio, date_format_str)
        final = datetime.strptime(DHFim, date_format_str)

        # calculos para seleccionar o tipo de request a efetuar
        delta = final - start
        delta_in_sec = delta.total_seconds()
        delta_in_hours = divmod(delta_in_sec, 3600)[0]
        dias_completos = math.floor(delta_in_hours / 24)
        num_horas_restantes = delta_in_hours - (24 * dias_completos)

        initial_dateime = start + timedelta(hours=-1)
        initial_date = initial_dateime.strftime("%Y-%m-%d:%H")

        # primeiro caso
        if delta_in_hours >= 24:
            # parte relativa aos dias completos
            for k in range(1, dias_completos + 1):
                new_end = initial_dateime + timedelta(days=1)
                new_end_date = new_end.strftime("%Y-%m-%d:%H")
                filename = os.path.join(pathjson, f"{k}.json")

                weather_request(api, latitude, longitude, initial_date,
                                new_end_date, filename)
                number_requests += 1

                initial_dateime = new_end
                initial_date = initial_dateime.strftime("%Y-%m-%d:%H")

                updatejsonrequest(number_requests, jrequestspath)
                update_verify_jsontime(number_requests, jtimefilepath, verify)

            # parte relativa às horas restantes
            if num_horas_restantes != 0:
                new_initial = start + timedelta(days=dias_completos, hours=-1)
                new_initial_date = new_initial.strftime("%Y-%m-%d:%H")

                temporal_dif = final - new_initial
                temporal_dif_hours = temporal_dif.total_seconds() / 3600
                if temporal_dif_hours < 24:
                    new_end = initial_dateime + timedelta(
                        hours=math.floor(temporal_dif_hours) + 1)
                else:
                    new_end = initial_dateime + timedelta(
                        hours=math.floor(temporal_dif_hours))

                new_end_date = new_end.strftime("%Y-%m-%d:%H")

                filename = os.path.join(pathjson, f"{dias_completos + 1}.json")
                weather_request(api, latitude, longitude, new_initial_date,
                                new_end_date, filename)
                number_requests += 1

                updatejsonrequest(number_requests, jrequestspath)
                update_verify_jsontime(number_requests, jtimefilepath, verify)

            # Merge dos jsons e posterior eliminação dos jsons temporários
            listfiles = []
            for file in os.listdir(pathjson):
                if re.match(r"[0-9]+.json", file):
                    fpath = os.path.join(pathjson, file)
                    listfiles.append(fpath)

            merge_json(listfiles, pathjson)
            del_files(listfiles)

        # segundo caso (no limite de durar 24 horas)
        elif 22 <= delta_in_hours < 24:
            final_plus_1day = start + timedelta(days=1, hours=-1)
            end_date = final_plus_1day.strftime("%Y-%m-%d:%H")

            weather_request(api, latitude, longitude, initial_date, end_date,
                            filepath)
            number_requests += 1

        # terceiro caso (última possibilidade)
        else:
            final_plus_2hours = final + timedelta(hours=1)
            end_date = final_plus_2hours.strftime("%Y-%m-%d:%H")

            weather_request(api, latitude, longitude, initial_date, end_date,
                            filepath)
            number_requests += 1

        # criação de dicionarios para json de controlo de index
        json_dict = {jsonstring: i}
        json_dict_exception = {jsonstring: 0}
        """ 
            Escrita de ficheiro json para guardar index onde o programa parou ou reiniciar index se terminou o ciclo.
            Por questão de segurança guarda o penúltimo indice para assegurar que a parte meteorológica do dataset 
        é gerada na sua totalidade e corretamente.
        
        """
        if i < len(sf) - 1:
            with open(jverifypath, 'w') as output:
                json.dump(json_dict, output, indent=4)
        else:
            with open(jverifypath, 'w') as output:
                json.dump(json_dict_exception, output, indent=4)

        updatejsonrequest(number_requests, jrequestspath)
示例#17
0
from weatherbit.api import Api
import config

api = Api(config.weatherbit_key)


class WeatherData:
    def __init__(self, city, state):
        self.city = city
        self.state = state

    def getPrecip(self):
        api.set_granularity('daily')
        forecast = api.get_forecast(city=self.city,
                                    state=self.state,
                                    country="US")
        precip_forecast = forecast.get_series(['precip'])
        print(precip_forecast)

    def getWindSpeed(self):
        api.set_granularity('daily')
        forecast = api.get_forecast(city=self.city,
                                    state=self.state,
                                    country="US")
        wind_spd_forecast = forecast.get_series(['wind_spd'])
        print(wind_spd_forecast)

    def getWindDir(self):
        api.set_granularity('daily')
        forecast = api.get_forecast(city=self.city,
                                    state=self.state,
示例#18
0
    '{0} will be enjoying some nice 69°F weather today'
]

# A thread-safe task queue that stores actions to perform against Reddit
task_queue = SimpleQueue()

# Config the logger
logging.basicConfig(filename='weather_output.log',
                    filemode='w',
                    level=logging.INFO)

# Set up the weather API for later usage
config = configparser.ConfigParser()
config.read('weather_api.ini')
api_key = config['weatherbit.io']['api_key']
weather_api = Api(api_key)
weather_api.set_granularity('daily')

# Load locations to check from stored CSV file
locations = []
with open('top_150_cities.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile, fieldnames=['city', 'region', 'id'])
    for row in reader:
        locations.append(row)

# Setup Reddit Connection
reddit = praw.Reddit(
    'niceweatherbot',
    user_agent='python:niceweatherbot:v1.0.0 (by u/jakekarnes42)')

# Create a thread pool
from weatherbit.api import Api

api_key = "3005becb7e874467b183c6d4158ad28b"
lat = 38.00
lon = -125.75

api = Api(api_key)

# Set the granularity of the API - Options: ['daily','hourly','3hourly']
# Will only affect forecast requests.
api.set_granularity('daily')

# Query by lat/lon
# forecast = api.get_forecast(lat=lat, lon=lon)

# You can also query by city:
# forecast = api.get_forecast(city="Raleigh,NC")

# Or City, state, and country:
forecast = api.get_forecast(city="Raleigh", state="North Carolina",
                           country="US")

# To get a daily forecast of temperature, and precipitation:
print(forecast.get_series(['temp', 'precip']))

# Get daily history by lat/lon:
api.set_granularity('daily')
history = api.get_history(lat=lat, lon=lon, start_date='2018-02-01',
                          end_date='2018-02-02')

# To get a daily time series of temperature, precipitation, and rh: