Skip to content

Commit

Permalink
Reforge the arch
Browse files Browse the repository at this point in the history
Get 12 days data and print them all.
  • Loading branch information
fandeliang committed May 18, 2015
1 parent 31683ca commit a2f98df
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions we.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,16 @@ def __init__(self, data):
self.main = weather['main']
self.description = weather['description']
#(NOTE) UTC vs Localtime
self.dt = data['dt']
self.data = data

if self.id in WEATHER_MAPE:
self.weather_icon = copy.deepcopy(WEATHER_ICON[WEATHER_MAPE[self.id]])
else:
self.weather_icon = copy.deepcopy(WEATHER_ICON['unknown'])

self.dt = data['dt']
self.date = time.asctime(time.localtime(self.dt))[0:10]

def get_wind_icon(self):
if self.wind_deg >= 337.5 or self.wind_deg < 22.5:
wind_icon = WIND_ICON['N']
Expand All @@ -200,7 +202,7 @@ def get_wind_icon(self):

return wind_icon

def format_today(self):
def format_daily(self):
temp = self.data['temp']
self.temp_min = temp['min']
self.temp_max = temp['max']
Expand All @@ -219,12 +221,10 @@ def format_today(self):
ret[2] += wind_ret
ret[3] += temp_ret
ret[4] += 'Humidity: ' + str(self.humidity)
ret.append(self.date)

return ret

def format_forecast(self):
pass


class OpenWeatherMap(object):
def __init__(self):
Expand All @@ -234,8 +234,7 @@ def __init__(self):
self.country = None
self.APPID = None
self.host = 'api.openweathermap.org'
self.forecast_path = '/data/2.5/forecast?units=metric&q=' + self.q
self.today_path = '/data/2.5/forecast/daily?units=metric&cnt=1&q=' + self.q
self.today_path = '/data/2.5/forecast/daily?units=metric&cnt=15&q=' + self.q

def http_request(self, path):
# try:... We need handle exception here.
Expand All @@ -247,47 +246,54 @@ def http_request(self, path):
if res.status in (200, 201, 202, 204):
return res.read()

def parse_forecast_weather(self):
resp = self.http_request(self.forecast_path)
data = json.loads(resp)

return data['list']

def parse_today_weather(self):
def get_weather_data(self):
resp = self.http_request(self.today_path)
# TypeError: expected string or buffer
data = json.loads(resp)

self.city = data['city']['name']
self.country = data['city']['country']
today_data = data['list'][0]
data = data['list']

return today_data
return data


def print_city_info(city, country):
print("Weather for City: %s %s\n" %(city, country))


def print_date_info(data):
lines = [
"┌──────────────────────────────┬──────────────────────────────┬──────────────────────────────┬──────────────────────────────┐",
"│ │ | │ │",
"├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤",
"| | | | |",
"| | | | |",
"| | | | |",
"| | | | |",
"| | | | |",
"└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘"]
line1 = lines[1]


def print_today_weather(data):
today = WeatherFormat(data)
ret = today.format_today()

for line in ret:
print(line)

print('\n')


def print_forecast_weather():
pass


def print_day_weather():
pass

if '__main__' == __name__:
weather = OpenWeatherMap()
today_data = weather.parse_today_weather()
forecast_data = weather.parse_forecast_weather()
data = weather.get_weather_data()

format_data = []
for d in data[0:12]:
daily_data = WeatherFormat(d)
format_data.append(daily_data.format_daily())

for i in range(3):
print_date_info(format_data[4*i: 4*i + 4])

print_city_info(weather.city, weather.country)
print_today_weather(today_data)

0 comments on commit a2f98df

Please sign in to comment.