Ejemplo n.º 1
0
 def test_update_weather_data(self):
     """Testing that new weather data is loaded correctly"""
     forecast1 = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd1 = models.WeatherData(forecast1, self.location)
     forecast2 = forecastio.manual(
         os.path.join('fixtures', 'us_cincinnati.json'))
     wd2 = models.WeatherData(forecast2, self.location)
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd1)
     self.assertEqual(50.84, wbs.weather_data.apparentTemperature)
     self.assertIn('51', wbs.normal())
     self.assertIn('66ºF', wbs.forecast())
     self.assertEqual('normal', wbs.special().type)
     self.assertEqual(0, len(wd1.alerts))
     wbs.set_weather(wd2)
     self.assertEqual(73.09, wbs.weather_data.apparentTemperature)
     self.assertIn('73', wbs.normal())
     self.assertIn('78ºF', wbs.forecast())
     self.assertEqual('moderate-rain', wbs.special().type)
     alert = wbs.alert(wd2.alerts[0], wd2.timezone)
     self.assertIn('Severe Thunderstorm Warning', alert)
     self.assertIn('Wed, Oct 19 at 19:30:00 EDT', alert)
     self.assertIn(
         'https://alerts.weather.gov/cap/wwacapget.php?x=OH12561A63BE38.SevereThunderstormWarning.'
         '12561A63C2E8OH.ILNSVSILN.f17bc0b3ead1db18bf60532894d9925e', alert)
Ejemplo n.º 2
0
    def test_expired(self):
        """Test that an alert is expired or active"""
        forecast = forecastio.manual(os.path.join('fixtures', 'us_alert.json'))
        alert = models.WeatherAlert(forecast.alerts()[0])
        self.assertTrue(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2017, 10, 18, 4, 4))))
        self.assertFalse(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2016, 10, 18, 4, 4))))
        self.assertFalse(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2015, 10, 18, 4, 4))))

        forecast = forecastio.manual(os.path.join('fixtures', 'ca_alert.json'))
        alert = models.WeatherAlert(forecast.alerts()[0])
        self.assertTrue(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2017, 2, 7, 17, 12))))
        self.assertFalse(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2017, 2, 7, 17, 11))))
        self.assertFalse(
            alert.expired(
                pytz.utc.localize(datetime.datetime(2017, 2, 3, 17, 11))))
Ejemplo n.º 3
0
 def test_update_weather_data(self, mock_get):
     """Testing that new weather data is loaded correctly"""
     forecast1 = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd1 = models.WeatherData(forecast1, self.location)
     forecast2 = forecastio.manual(os.path.join('fixtures', 'us_cincinnati.json'))
     wd2 = models.WeatherData(forecast2, self.location)
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd1)
     self.assertEqual(50.84, wbs.weather_data.apparentTemperature)
     self.assertIn('51', wbs.normal())
     self.assertIn('66ºF', wbs.forecast())
     self.assertEqual('normal', wbs.special().type)
     dt = datetime.datetime.utcfromtimestamp(1475129665)  # datetime.datetime(2016, 9, 29, 6, 14, 25)
     alert = wbs.alert(title='test1', expires=pytz.utc.localize(dt), uri='test.uri1')
     self.assertIn('test1', alert)
     self.assertIn('Thu, Sep 29 at 06:14:25 UTC', alert)
     self.assertIn('test.uri1', alert)
     wbs.set_weather(wd2)
     self.assertEqual(73.09, wbs.weather_data.apparentTemperature)
     self.assertIn('73', wbs.normal())
     self.assertIn('78ºF', wbs.forecast())
     self.assertEqual('moderate-rain', wbs.special().type)
     dt = datetime.datetime.utcfromtimestamp(1475129666)  # datetime.datetime(2016, 9, 29, 6, 14, 26)
     alert = wbs.alert(title='test2', expires=pytz.utc.localize(dt), uri='test.uri2')
     self.assertIn('test2', alert)
     self.assertIn('Thu, Sep 29 at 06:14:26 UTC', alert)
     self.assertIn('test.uri2', alert)
Ejemplo n.º 4
0
 def test_bad_data(self):
     """Testing that bad data will gracefully fail"""
     forecast = forecastio.manual(os.path.join('fixtures', 'bad_data_unavailable.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertFalse(wd.valid)
     forecast = forecastio.manual(os.path.join('fixtures', 'bad_data_temperature.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertFalse(wd.valid)
     forecast = forecastio.manual(os.path.join('fixtures', 'bad_data_summary.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertFalse(wd.valid)
Ejemplo n.º 5
0
 def test_optional_fields(self):
     """Testing that bad data will gracefully fail"""
     forecast = forecastio.manual(
         os.path.join('fixtures', 'optional_fields.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertEqual(wd.precipType, 'rain')
     self.assertEqual(wd.windBearing, 'unknown direction')
Ejemplo n.º 6
0
 def test_normal(self):
     """Testing that normal events are formatted"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast, self.location)
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     normal_string = wbs.normal()
     self.assertIn(normal_string, wbs.normal_conditions)
Ejemplo n.º 7
0
 def test_alerts(self):
     """Testing that alerts are loaded correctly into a list"""
     location = models.WeatherLocation(34.2, -118.36, 'Los Angeles, CA')
     forecast = forecastio.manual(os.path.join('fixtures', 'us_alert.json'))
     wd = models.WeatherData(forecast, location)
     self.assertEqual(wd.alerts[0].title, 'Wind Advisory')
     self.assertEqual(wd.alerts[1].title, 'Beach Hazards Statement')
     self.assertEqual(wd.alerts[2].title, 'Red Flag Warning')
Ejemplo n.º 8
0
 def test_precipitation(self):
     """Testing that precipitation conditions are met"""
     wbs = models.WeatherBotString(self.weatherbot_strings)
     forecast_us = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast_us, self.location)
     wbs.set_weather(wd)
     self.assertEqual(wbs.precipitation(), models.Condition(type='none', text=''))
     wd.precipIntensity = 0.3
     wd.precipProbability = 0.5
     wd.precipType = 'rain'
     wbs.set_weather(wd)
     self.assertEqual(wbs.precipitation(), models.Condition(type='none', text=''))
     wd.precipIntensity = 0.3
     wd.precipProbability = 1
     wd.precipType = 'none'
     wbs.set_weather(wd)
     self.assertEqual(wbs.precipitation(), models.Condition(type='none', text=''))
     wd.precipIntensity = 0
     wd.precipProbability = 1
     wd.precipType = 'rain'
     wbs.set_weather(wd)
     self.assertEqual(wbs.precipitation(), models.Condition(type='none', text=''))
     wd.precipIntensity = 0
     wd.precipProbability = 1
     wd.precipType = 'none'
     wbs.set_weather(wd)
     self.assertEqual(wbs.precipitation(), models.Condition(type='none', text=''))
     # testing with a few possible conditions
     wd.precipIntensity = 0.3
     wd.precipProbability = 1
     wd.precipType = 'rain'
     wbs.set_weather(wd)
     precip = wbs.precipitation()
     self.assertEqual(precip.type, 'moderate-rain')
     self.assertIn(precip.text, wbs.precipitations['rain']['moderate'])
     wd.precipIntensity = 0.4
     wd.precipProbability = 0.85
     wd.precipType = 'snow'
     wbs.set_weather(wd)
     precip = wbs.precipitation()
     self.assertEqual(precip.type, 'heavy-snow')
     self.assertIn(precip.text, wbs.precipitations['snow']['heavy'])
     wd.precipIntensity = 0.06
     wd.precipProbability = 1
     wd.precipType = 'sleet'
     wbs.set_weather(wd)
     precip = wbs.precipitation()
     self.assertEqual(precip.type, 'light-sleet')
     self.assertIn(precip.text, wbs.precipitations['sleet']['light'])
     wd.precipIntensity = 0.005
     wd.precipProbability = 1
     wd.precipType = 'rain'
     wbs.set_weather(wd)
     precip = wbs.precipitation()
     self.assertEqual(precip.type, 'very-light-rain')
     self.assertIn(precip.text, wbs.precipitations['rain']['very-light'])
    def setUp(self):

        URL = "http://test_url.com/"
        responses.add(responses.GET, URL,
                      body=open('tests/fixtures/test.json').read(),
                      status=200,
                      content_type='application/json',
                      match_querystring=True)

        self.forecast = forecastio.manual("http://test_url.com/")
Ejemplo n.º 10
0
 def test_alert(self):
     """Testing that alerts are formatted"""
     wbs = models.WeatherBotString(self.weatherbot_strings)
     forecast = forecastio.manual(os.path.join('fixtures', 'ca_alert.json'))
     location = models.WeatherLocation(50.564167, -111.898889, 'Brooks, Alberta')
     wd = models.WeatherData(forecast, location)
     wbs.set_weather(wd)
     alert = wbs.alert(wd.alerts[0], wd.timezone)
     self.assertIn('Snowfall Warning', alert)
     self.assertIn('https://weather.gc.ca/warnings/report_e.html?ab6', alert)
Ejemplo n.º 11
0
 def test_no_expires(self):
     """Test that a WeatherAlert is loaded correctly"""
     forecast = forecastio.manual(os.path.join('fixtures', 'ca_alert.json'))
     alert = models.WeatherAlert(forecast.alerts()[0])
     self.assertEqual('Snowfall Warning', alert.title)
     self.assertEqual(pytz.utc.localize(datetime.datetime(2017, 2, 4, 17, 11)), alert.time)
     self.assertEqual('https://weather.gc.ca/warnings/report_e.html?ab6', alert.uri)
     self.assertEqual('warning', alert.severity)
     self.assertEqual('d5c1870d583f95441a41d452355173bad49f60f87c2962f195bd7873f0997d4b', alert.sha())
     self.assertEqual('<WeatherAlert: Snowfall Warning at 2017-02-04 17:11:00+00:00>', str(alert))
Ejemplo n.º 12
0
 def test_init(self, mock_get):
     """Test that a WeatherAlert is loaded correctly"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us_alert.json'))
     alert = models.WeatherAlert(forecast.alerts()[0])
     self.assertEqual(alert.title, 'Wind Advisory for Los Angeles, CA')
     self.assertEqual(alert.time, pytz.utc.localize(datetime.datetime(2016, 10, 18, 4, 4)))
     self.assertEqual(alert.expires, pytz.utc.localize(datetime.datetime(2016, 10, 20, 19, 0)))
     self.assertEqual(alert.uri, 'https://alerts.weather.gov/cap/wwacapget.php?x=CA12561A519050.WindAdvisory.'
                                 '12561A725D30CA.LOXNPWLOX.9240bcf720aae1b01b10f53f012e61bb')
     self.assertEqual(alert.sha(), '7e220f06588bad306e05953409d8ec7ebe538ab76ecd4f4a562ac0e406a81c2e')
     self.assertEqual(str(alert), '<WeatherAlert: Wind Advisory for Los Angeles, CA at 2016-10-18 04:04:00+00:00>')
Ejemplo n.º 13
0
 def test_init(self, mock_get):
     """Test that a WeatherAlert is loaded correctly"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us_alert.json'))
     alert = models.WeatherAlert(forecast.alerts()[0])
     self.assertEqual('Wind Advisory', alert.title)
     self.assertEqual(pytz.utc.localize(datetime.datetime(2016, 10, 18, 4, 4)), alert.time)
     self.assertEqual(pytz.utc.localize(datetime.datetime(2016, 10, 20, 19, 0)), alert.expires)
     self.assertEqual('https://alerts.weather.gov/cap/wwacapget.php?x=CA12561A519050.WindAdvisory.'
                      '12561A725D30CA.LOXNPWLOX.9240bcf720aae1b01b10f53f012e61bb', alert.uri)
     self.assertEqual('a6bf597275fdf063c76a42b05c3c81ed093701b2344c3c98cfde36875f7a4c3d', alert.sha())
     self.assertEqual('<WeatherAlert: Wind Advisory at 2016-10-18 04:04:00+00:00>', str(alert))
Ejemplo n.º 14
0
    def setUp(self):

        URL = "http://test_url.com/"
        responses.add(responses.GET,
                      URL,
                      body=open('tests/fixtures/test.json').read(),
                      status=200,
                      content_type='application/json',
                      match_querystring=True)

        self.forecast = forecastio.manual("http://test_url.com/")
Ejemplo n.º 15
0
def get_forecast(coords, time):
    """Given (lat, lng) tuple and Pendulum datetime object, returns forecast."""

    lat, lng = coords

    # Convert datetime object to ISO 8601 string.
    time = time.to_iso8601_string()

    url = 'https://api.darksky.net/forecast/%s/%s,%s,%s' \
        % (FIO_KEY, lat, lng, time)

    return forecastio.manual(url)
Ejemplo n.º 16
0
 def test_dict(self):
     """Testing that __dict__ returns the correct data"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast, self.location)
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     self.assertEqual({
         'language': wbs.language,
         'weather_data': wbs.weather_data,
         'forecasts': wbs.forecasts,
         'forecast_endings': wbs.forecasts_endings,
         'normal_conditions': wbs.normal_conditions,
         'special_conditions': wbs.special_conditions,
         'precipitations': wbs.precipitations
     }, wbs.__dict__())
Ejemplo n.º 17
0
def Weather(conn):
    ip_url = 'https://freegeoip.net/json'
    request_zip = requests.get(ip_url)
    load_zip = json.loads(request_zip.text)

    lat = str(load_zip['latitude'])
    longs = str(load_zip['longitude'])
    API = key.api_keys['WEATHER_API']

    forecast = forecastio.manual(
        'https://api.darksky.net/forecast/6a92bd8d0626c735970600815a0323a7/' +
        lat + ',' + longs + '')
    byHour = forecast.hourly()

    high_low = []
    for currentData in forecast.daily().data:
        high_low_list = []
        high_low_list.extend(
            (currentData.temperatureLow, currentData.temperatureHigh))
        high_low.append(high_low_list)
    forecast_array = []
    high = str(int(round(high_low[0][1])))
    low = str(int(round(high_low[0][0])))
    #LOOP THROUGH HOURLY DATA
    for hourlyData in byHour.data:
        #CREATE ARRAY TO APPEND TO MASTER ARRAY
        forecast_array_list = []
        #GET TEMPERATURE TIME DATE AND SUMMARY
        temp = str(int(round(hourlyData.temperature)))
        time = hourlyData.time
        time = time - datetime.timedelta(hours=5)
        time = str(time).split()
        test_time = time[1]
        time_date = time[0]
        test_time = test_time[:-3]
        #CONVERT TIME TO STANDARD 12 HR TIME
        time = timeConvert(test_time)
        summary = hourlyData.summary
        #APPEND VARIABLES TO SINGLE ARRAY CREATED EARLIER AND THEN APPEND TO MASTER ARRAY FOR 2D ARRAY
        forecast_array_list.extend((temp, time, summary, time_date))
        forecast_array.append(forecast_array_list)

    #DELETE 25 ROWS AS WE DONT NEED ALL OF THEM
    #for i in range(25):
    #del forecast_array[-i]
    conn.send((forecast_array, high, low))
    conn.close()
    return (forecast_array, high, low)
Ejemplo n.º 18
0
 def test_forecast(self):
     """Testing that forecasts are formatted correctly"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast, self.location)
     self.weatherbot_strings['forecast_endings'] = []
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     forecast_string = wbs.forecast()
     self.assertIn(forecast_string, wbs.forecasts)
     self.weatherbot_strings['forecast_endings'] = ['Test ending!']
     self.weatherbot_strings['forecasts'] = ['The forecast for today is {summary_lower} {high}/{low}.']
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     forecast_string = wbs.forecast()
     self.assertEqual(forecast_string,
                      'The forecast for today is mostly cloudy throughout the day. 66ºF/50ºF. Test ending!')
Ejemplo n.º 19
0
 def update(self):
     """Get the latest data from Dark Sky."""
     try:
         if not self.manual_url:
             self.data = forecastio.load_forecast(
                 self._api_key,
                 self.latitude,
                 self.longitude,
                 units=self.requested_units)
         else:
             self.data = forecastio.manual(self.manual_url)
         self.currently = self.data.currently()
         self.hourly = self.data.hourly()
         self.daily = self.data.daily()
     except (ConnectError, HTTPError, Timeout, ValueError) as error:
         _LOGGER.error("Unable to connect to Dark Sky. %s", error)
         self.data = None
Ejemplo n.º 20
0
 def _update(self):
     """Get the latest data from Dark Sky."""
     try:
         if not self.manual_url:
             self.data = forecastio.load_forecast(
                 self._api_key,
                 self.latitude,
                 self.longitude,
                 units=self.requested_units)
         else:
             self.data = forecastio.manual(self.manual_url)
         if self._connect_error:
             self._connect_error = False
             _LOGGER.info("Reconnected to Dark Sky")
     except (ConnectError, HTTPError, Timeout, ValueError) as error:
         if not self._connect_error:
             self._connect_error = True
             _LOGGER.error("Unable to connect to Dark Sky: %s", error)
         self.data = None
     self.unit_system = self.data and self.data.json["flags"]["units"]
Ejemplo n.º 21
0
 def test_init(self):
     """Testing that weather data is loaded correctly"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertEqual(wd.units, utils.get_units('us'))
     self.assertEqual(wd.windBearing, 'SW')
     self.assertEqual(wd.windSpeed, 10.81)
     self.assertEqual(wd.apparentTemperature, 50.84)
     self.assertEqual(wd.temp, 50.84)
     self.assertEqual(wd.humidity, 89)
     self.assertEqual(wd.precipIntensity, 0)
     self.assertEqual(wd.precipProbability, 0)
     self.assertEqual(wd.precipType, 'none')
     self.assertEqual(wd.summary, 'Partly Cloudy')
     self.assertEqual(wd.icon, 'partly-cloudy-day')
     self.assertEqual(wd.location, self.location)
     self.assertEqual(wd.timezone, 'Europe/Copenhagen')
     self.assertEqual(wd.alerts, [])
     self.assertTrue(wd.valid)
     self.assertEqual(str(wd),
                      '<WeatherData: Lyngby-Taarbæk, Hovedstaden(55.76,12.49) at 2016-10-01 05:56:38+00:00>')
Ejemplo n.º 22
0
 def update(self):
     """Get the latest data from PirateWeather."""
     try:
         #self.data = forecastio.load_forecast(
         #    self._api_key, self.latitude, self.longitude, units=self.requested_units
         #)
         forecastString = "https://api.pirateweather.net/forecast/" + self._api_key + "/" + str(
             self.latitude) + "," + str(
                 self.longitude) + "?units=" + self.requested_units
         self.data = forecastio.manual(forecastString)
         self.currently = self.data.currently()
         self.hourly = self.data.hourly()
         self.daily = self.data.daily()
         if self._connect_error:
             self._connect_error = False
             _LOGGER.info("Reconnected to PirateWeather")
     except (ConnectError, HTTPError, Timeout, ValueError) as error:
         if not self._connect_error:
             self._connect_error = True
             _LOGGER.error("Unable to connect to PirateWeather. %s", error)
         self.data = None
Ejemplo n.º 23
0
def get_forecast_object(lat, lng, units='us', lang='en'):
    """
    Using the 'WEATHERBOT_DARKSKY_KEY' environmental variable, get the weather from Dark Sky at the given location.
    If there is an error, log it and return None.
    :type lat: float
    :param lat: latitude
    :type lng: float
    :param lng: longitude
    :type units: str
    :param units: units standard, ex 'us', 'ca', 'uk2', 'si', 'auto'
    :type lang: str
    :param lang: language, ex: 'en', 'de'. See https://darksky.net/dev/docs/forecast for more
    :return: Forecast object or None if HTTPError or ConnectionError
    """
    try:
        url = 'https://api.darksky.net/forecast/{0}/{1},{2}?units={3}&lang={4}'\
            .format(os.getenv('WEATHERBOT_DARKSKY_KEY'), lat, lng, units, lang)
        return forecastio.manual(url)
    except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as err:
        logging.error(err)
        logging.error('Error when getting Forecast object', exc_info=True)
        return None
Ejemplo n.º 24
0
 def _update(self):
     """Get the latest data from Dark Sky."""
     try:
         #self.data = forecastio.load_forecast(
         #    self._api_key,
         #    self.latitude,
         #    self.longitude,
         #    units=self.units,
         #    lang=self.language,
         #)
         forecastString = "https://api.pirateweather.net/forecast/" + self._api_key + "/" + str(
             self.latitude) + "," + str(
                 self.longitude) + "?units=" + self.units
         self.data = forecastio.manual(forecastString)
         if self._connect_error:
             self._connect_error = False
             _LOGGER.info("Reconnected to PirateWeather")
     except (ConnectError, HTTPError, Timeout, ValueError) as error:
         if not self._connect_error:
             self._connect_error = True
             _LOGGER.error("Unable to connect to PirateWeather: %s", error)
         self.data = None
     self.unit_system = self.data and self.data.json["flags"]["units"]
Ejemplo n.º 25
0
def forecastio_datapull(target_gids):



    cnx = mlbtab.mlb_connect()
    curA = cnx.cursor()

    query1 = ("""
            SELECT 
            g.gid,
            g.game_date,
            g.local_game_time,
            m.latitude,
            m.longitude,
            g.venue_w_chan_loc

            FROM pfx_game as g
            JOIN pfx_venue_w_loc_mapping as m
            ON g.venue_w_chan_loc = m.w_chan_loc

            WHERE g.gid IN ("""+target_gids+""")

    """)

    curA.execute(query1)

    games = np.array(curA.fetchall())

    curA.close()
    cnx.close()
    
    def scrapeWrite(file, fields, dic, line_end=True):
        n = 0
        for field in fields:
            if n == 0:
                file.write('"'+str(dic[field])+'"')
                n += 1
            else:
                file.write(',"'+str(dic[field])+'"')
        if line_end == True:
            file.write('\n')
        else:
            file.write(',')

    def cleanCSV(filename):

        f = open(filename,"r",encoding='utf-8')
        lines = f.readlines()
        f.close()

        f = open(filename,"w",encoding='utf-8')
        for line in lines:
            f.write(line.replace('""',''))
        f.close()

    def csvstore_delete(filename):
        try:
            os.remove(filename)
        except:
            print('    ! WARNING: not deleted - ',filename)


    def key_check(key,datadic,targetdic):
        if key in set(datadic.keys()):
            targetdic[key] = datadic[key]
        else:
            targetdic[key] = ''

    def sqltable_nulling(attrlist):

        colvar_string = ''
        nullif_string = ''
        for col in attrlist:
            colvar_string += "@v_"+col+", "
            nullif_string += col+" = NULLIF(@v_"+col+",''), "

        colvar_string=colvar_string[:-2]
        nullif_string=nullif_string[:-2]

        return colvar_string, nullif_string

    # In[60]:

    script_starttime = datetime.datetime.now()
    print('started -',script_starttime)

    data_storage_days = 0




    api_key = settings.FORECASTIO_API_KEY

    game_attrlist = [
        "gid",
        "game_date",
        "local_game_time",
        "venue_w_chan_loc",
        "latitude",
        "longitude"
        ]

    weather_attrlist = [
        "temperature",
        "apparentTemperature",
        "humidity",
        "pressure",
        "dewPoint",
        "cloudCover",
        "uvIndex",
        "precipType",
        "windBearing",
        "windSpeed",

        "nearestStormBearing",
        "nearestStormDistance",
        "ozone",
        "precipIntensity",
        "precipProbability",
        "windGust"

    ]

    derived_attrlist = [
        "windVector_ns",
        "windVector_ew",
        "precipType_rain",
        "precipType_snow",
        "precipType_sleet"
    ]

    pull_date = datetime.datetime.now().date()

    game_columns_string = (', '.join(game_attrlist+weather_attrlist+derived_attrlist)+'\n')
    game_outfile=open("{}/forecastio_data_table___{}.csv".format(settings.PFX_SCRAPE_PATH, str(pull_date)), "w+", encoding='utf-8')
    if os.stat(game_outfile.name).st_size==0: game_outfile.write(game_columns_string)                    

    resultmsg = ''

    for game in games:

        game_dic = {}
        game_dic["gid"] = game[0]
        game_dic["game_date"] = str(game[1])
        game_dic["local_game_time"] = game[2]
        game_dic["local_game_time"] = (datetime.datetime.min + game_dic["local_game_time"]).strftime('%H:%M')
        game_dic["latitude"] = game[3]
        game_dic["longitude"] = game[4]
        game_dic["venue_w_chan_loc"] = game[5]

        Date = game[1]
        Time = game[2]
        Time = (datetime.datetime.min+Time).time()
        DateTime = str(Date)+'T'+str(Time)

        Lat = game[3]
        Lng = game[4]

        url = "https://api.darksky.net/forecast/"+api_key+"/"+str(Lat)+","+str(Lng)+","+DateTime

        forecast = forecastio.manual(url)

        currently = forecast.json["currently"]

        for attr in weather_attrlist:
            key_check(attr,currently,game_dic)


        if game_dic["windSpeed"] != '' and game_dic["windBearing"] != '':
            game_dic["windVector_ns"] = math.cos(game_dic["windBearing"]*math.pi/180) * game_dic["windSpeed"]
            game_dic["windVector_ew"] = math.sin(game_dic["windBearing"]*math.pi/180) * game_dic["windSpeed"]
        else:
            game_dic["windVector_ns"] = ''
            game_dic["windVector_ew"] = ''

        if game_dic["precipType"] != '' or game_dic["precipIntensity"] == 0:
            game_dic["precipType_rain"] = 0
            game_dic["precipType_snow"] = 0
            game_dic["precipType_sleet"] = 0
        else:
            game_dic["precipType_rain"] = ''
            game_dic["precipType_snow"] = ''
            game_dic["precipType_sleet"] = ''
        if game_dic["precipType"] == 'snow':
            game_dic["precipType_snow"] = 1
        elif game_dic["precipType"] == 'sleet':
            game_dic["precipType_sleet"] = 1
        elif game_dic["precipType"] == 'rain' or game_dic["precipIntensity"] > 0:
            game_dic["precipType_rain"] = 1

        scrapeWrite(game_outfile, game_attrlist+weather_attrlist+derived_attrlist, game_dic, line_end=True)

    game_outfile.close()

    cleanCSV(game_outfile.name)

    affectedrows = 0

    colvar_string, nullif_string = sqltable_nulling(game_attrlist+weather_attrlist+derived_attrlist)

    try:
        cnx = mysql.connector.connect(user='******', password='',
                                    host='127.0.0.1',
                                    database='mlb')
        cnx.get_warnings = True
        #print('status - Successfully connected to mySQL DB')
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("error - Something is wrong with your user name or password")
            sys.exit()
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("error - Database does not exist")
            sys.exit()
        else:
            print(err)
            sys.exit()

    curA = cnx.cursor()

    insert_query1 = ("""
                    LOAD DATA LOCAL INFILE 
                    '"""+game_outfile.name+"""' 
                    INTO TABLE forecastio_weather
                    COLUMNS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES
                    ("""+colvar_string+""")
                    SET
                    """+nullif_string+"""
                    """)


    curA.fetchwarnings()

    curA.execute(insert_query1)
    cnx.commit()
    affectedrows1 = curA.rowcount
    print("    forecastio_weather affected rows = {}".format(affectedrows1))
    resultmsg1 = str("\n    forecastio_weather affected rows = {}".format(affectedrows1))+"\n"
    



    curA.close()
    cnx.close()

    pastday = datetime.datetime.now().date() - datetime.timedelta(days=data_storage_days)
    past_gamefile = "{}/forecastio_data_table___{}.csv".format(settings.PFX_SCRAPE_PATH, str(pastday))

    csvstore_delete(past_gamefile)

    #print('status - csv deleted')

    script_endtime = datetime.datetime.now()
    script_totaltime = script_endtime - script_starttime

    endmsg = '    success - '+str(datetime.datetime.now())+' script total runtime = '+str(script_totaltime)

    #print(endmsg)

    emailmsg = '    started ' + str(script_starttime)+'\n\n+'+resultmsg+'\n\n'+endmsg
    print(emailmsg)
    #emailSend.emailSend(msg=emailmsg ,subject=sys.argv[0]+' report')

    print('\n')
    
    return affectedrows1
Ejemplo n.º 26
0
last = 0

while True:
    time.sleep(1)
    now = int(time.time())

    # 1000 API calls per day are free -> every 86 seconds. So 100 seconds sleep
    if now > last + 100:
        last = now
        lat = random.randint(35, 60)
        lon = random.randint(-11, 30)
        print 'coords: %d,%d' % (lat, lon)
        try:
            forecast = forecastio.manual(
                'https://api.forecast.io/forecast/%s/%d,%d?exclude=minutely,hourly,daily,flags'
                % (forecast_io_api_key, lat, lon))
        except:
            forecast = None
            pass
        if forecast != None:
            #print forecast.json

            # geohash 3 has a lat/lon error of 0.7, so it should be ok for lat and lon as int
            geohash = Geohash.encode(lat, lon, precision=3)

            if forecast.json.has_key('alerts'):
                alerts = forecast.json['alerts']
                for alert in alerts:
                    print alert
                    ttl = alert['expires'] - now
Ejemplo n.º 27
0
 def __init__(self):
     url = self.url.format(**self.get_params())
     self.forecast = manual(url)
Ejemplo n.º 28
0
client.loop_start()

last = 0 

while True:
	time.sleep(1)
	now = int(time.time())

# 1000 API calls per day are free -> every 86 seconds. So 100 seconds sleep
	if now > last + 100: 
		last = now
		lat = random.randint(35,60)
		lon = random.randint(-11,30)
		print 'coords: %d,%d' % (lat,lon)
		try:
			forecast = forecastio.manual('https://api.forecast.io/forecast/%s/%d,%d?exclude=minutely,hourly,daily,flags' % (forecast_io_api_key, lat, lon))
		except:
			forecast = None
			pass
		if forecast != None:
			#print forecast.json

# geohash 3 has a lat/lon error of 0.7, so it should be ok for lat and lon as int
			geohash = Geohash.encode(lat, lon, precision=3)

			if forecast.json.has_key('alerts'):
				alerts = forecast.json['alerts']
				for alert in alerts:
					print alert
					ttl = alert['expires'] - now
					payloaddict = dict(_type='msg',tst=now,ttl=ttl,prio=2,icon='fa-exclamation-triangle',desc=alert['description'],title=alert['title'], url=alert['uri'])
Ejemplo n.º 29
0
 def test_special(self):
     """Testing if special events are triggered"""
     forecast_si = forecastio.manual(os.path.join('fixtures', 'si.json'))
     forecast_us = forecastio.manual(os.path.join('fixtures', 'us.json'))
     forecast_ca = forecastio.manual(os.path.join('fixtures', 'ca.json'))
     forecast_uk2 = forecastio.manual(os.path.join('fixtures', 'uk2.json'))
     wd = models.WeatherData(forecast_si, self.location)
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     self.assertEqual('normal', wbs.special().type)
     self.assertEqual('', wbs.special().text)
     """Testing if wind-chill type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.apparentTemperature = -34
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     self.assertEqual('wind-chill', wbs.special().type)
     self.assertIn(wbs.special().text, wbs.special_conditions[wbs.special().type])
     wd = models.WeatherData(forecast_us, self.location)
     wd.apparentTemperature = -30
     wbs = models.WeatherBotString(self.weatherbot_strings)
     wbs.set_weather(wd)
     self.assertEqual('wind-chill', wbs.special().type)
     self.assertIn(wbs.special().text, wbs.special_conditions[wbs.special().type])
     """Testing if precip type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.precipProbability = 0.9
     wd.precipType = 'rain'
     wd.precipIntensity = 10.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-rain', wbs.special().type)
     self.assertIn(wbs.special().text, wbs.precipitations['rain']['heavy'])
     wd = models.WeatherData(forecast_us, self.location)
     wd.precipProbability = 0.9
     wd.precipType = 'rain'
     wd.precipIntensity = 1.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-rain', wbs.special().type)
     self.assertIn(wbs.special().text, wbs.precipitations['rain']['heavy'])
     wd = models.WeatherData(forecast_us, self.location)
     wd.precipProbability = 0.9
     wd.precipType = 'none'
     wd.precipIntensity = 1.0
     wbs.set_weather(wd)
     self.assertEqual('normal', wbs.special().type)
     self.assertEqual('', wbs.special().text)
     """Testing if medium-wind type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.icon = 'medium-wind'
     wbs.set_weather(wd)
     self.assertEqual('medium-wind', wbs.special().type)
     """Testing if heavy-wind type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.icon = 'heavy-wind'
     wbs.set_weather(wd)
     self.assertEqual('heavy-wind', wbs.special().type)
     wd = models.WeatherData(forecast_si, self.location)
     wd.windSpeed = 15.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-wind', wbs.special().type)
     wd = models.WeatherData(forecast_ca, self.location)
     wd.windSpeed = 56.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-wind', wbs.special().type)
     wd = models.WeatherData(forecast_us, self.location)
     wd.windSpeed = 35.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-wind', wbs.special().type)
     wd = models.WeatherData(forecast_uk2, self.location)
     wd.windSpeed = 35.0
     wbs.set_weather(wd)
     self.assertEqual('heavy-wind', wbs.special().type)
     """Testing if fog type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.icon = 'fog'
     wbs.set_weather(wd)
     self.assertEqual('fog', wbs.special().type)
     """Testing if cold type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.temp = -28.0
     wbs.set_weather(wd)
     self.assertEqual('cold', wbs.special().type)
     wd = models.WeatherData(forecast_us, self.location)
     wd.temp = -20.0
     wbs.set_weather(wd)
     self.assertEqual('cold', wbs.special().type)
     """Testing if super-hot type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.temp = 43.0
     wbs.set_weather(wd)
     self.assertEqual('super-hot', wbs.special().type)
     wd = models.WeatherData(forecast_us, self.location)
     wd.temp = 110.0
     wbs.set_weather(wd)
     self.assertEqual('super-hot', wbs.special().type)
     """Testing if hot type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.temp = 37.0
     wbs.set_weather(wd)
     self.assertEqual('hot', wbs.special().type)
     wd = models.WeatherData(forecast_us, self.location)
     wd.temp = 100.0
     wbs.set_weather(wd)
     self.assertEqual('hot', wbs.special().type)
     """Testing if dry type is triggered"""
     wd = models.WeatherData(forecast_si, self.location)
     wd.humidity = 25.0
     wbs.set_weather(wd)
     self.assertEqual('dry', wbs.special().type)
Ejemplo n.º 30
0
def getHourlyWeather():
    forecast = forecastio.manual(url, 60)
    byHour = forecast.hourly()
    return byHour
Ejemplo n.º 31
0
def getHourlyWeather():
    global numRequests
    numRequests = numRequests + 1
    forecast = forecastio.manual(url, 60)
    byHour = forecast.hourly()
    return byHour
Ejemplo n.º 32
0
 def test_json(self):
     """Testing that json() returns a dict containing the response from the Dark Sky API"""
     forecast = forecastio.manual(os.path.join('fixtures', 'us.json'))
     wd = models.WeatherData(forecast, self.location)
     self.assertEqual(wd.json(), forecast.json)