Ejemplo n.º 1
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()))
    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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
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()
Ejemplo n.º 6
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
Ejemplo n.º 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[
Ejemplo n.º 8
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()
Ejemplo n.º 9
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='')
Ejemplo n.º 10
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)
Ejemplo n.º 11
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,
# 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]