Esempio n. 1
0
class DataHandler:
    """
    Uses the Database and Apixu Objects to generate then insert weather data
    into the model
    """
    # Structure of location data for Location table in model
    location = {
        "name": None,
        "region": None,
        "country": None,
        "lat": None,
        "lon": None,
        "tz_id": None,
        "localtime_epoch": None,
        "localtime": None
    }

    current = {
        # last row id in location
        "last_updated_epoch": None,
        "last_updated": None,
        "temp_c": 'Polling',
        "temp_f": None,
        "is_day": None,
        "wind_mph": None,
        "wind_kph": None,
        "wind_degree": None,
        "wind_dir": None,
        "pressure_mb": None,
        "pressure_in": None,
        "precip_mm": None,
        "precip_in": None,
        "humidity": None,
        "cloud": None,
        "feelslike_c": None,
        "feelslike_f": None
    }

    # Structure of condition data for Current_Condition table in model
    condition = {
        # last row id in location
        "text": None,
        "icon": 'http://cdn.apixu.com/weather/64x64/day/116.png',
        "code": None
    }

    def __init__(self):
        self.apixu = Apixu()

    def _read_current_weather(self):
        current_weather = self.apixu.get_current_weather('Brisbane')
        return current_weather

    def create_data_chunks(self, weather_data):
        current_data = weather_data['current']
        location_data = weather_data['location']
        condition_data = weather_data['current']['condition']

        for key, value in location_data.iteritems():
            if key == 'name':
                self.location['name'] = value
            if key == 'country':
                self.location['country'] = value
            if key == 'region':
                self.location['region'] = value
            if key == 'lat':
                self.location['lat'] = value
            if key == 'lon':
                self.location['lon'] = value
            if key == 'tz_id':
                self.location['tz_id'] = value
            if key == 'localtime_epoch':
                self.location['localtime_epoch'] = value
            if key == 'localtime':
                self.location['localtime'] = value

        for key, value in current_data.iteritems():
            if key == 'last_updated_epoch':
                self.current['last_updated_epoch'] = value
            if key == 'last_updated':
                self.current['last_updated'] = value
            if key == 'temp_c':
                self.current['temp_c'] = value
            if key == 'temp_f':
                self.current['temp_f'] = value
            if key == 'is_day':
                self.current['is_day'] = value
            if key == 'wind_mph':
                self.current['wind_mph'] = value
            if key == 'wind_kph':
                self.current['wind_kph'] = value
            if key == 'wind_degree':
                self.current['wind_degree'] = value
            if key == 'wind_dir':
                self.current['wind_dir'] = value
            if key == 'pressure_mb':
                self.current['pressure_mb'] = value
            if key == 'pressure_in':
                self.current['pressure_in'] = value
            if key == 'precip_mm':
                self.current['precip_mm'] = value
            if key == 'precip_in':
                self.current['precip_in'] = value
            if key == 'humidity':
                self.current['humidity'] = value
            if key == 'cloud':
                self.current['cloud'] = value
            if key == 'feelslike_c':
                self.current['feelslike_c'] = value
            if key == 'feelslike_f':
                self.current['feelslike_f'] = value

        for key, value in condition_data.iteritems():
            if key == 'text':
                self.condition['text'] = value
            if key == 'icon':
                self.condition['icon'] = value
            if key == 'code':
                self.condition['code'] = value

    def icon_change_check(self, current_icon):
        if self.condition['icon'] is not current_icon:
            self.condition['icon'] = current_icon

    def download_icon(self):
        f = urllib2.urlopen(self.condition['icon'])
        data = f.read()
        with open("assets/weather_icon.jpg", "wb") as code:
            code.write(data)

    def get_data_file(self, file):
        pass

    def write_read_to_file(self, file):
        pass
Esempio n. 2
0
 def __init__(self):
     self.apixu = Apixu()