# -*- coding: utf8 -*-
import urllib2
from bs4 import BeautifulSoup
import sys
from pprint import pprint
# -------------------------------------------
def do_request():
url = 'http://pogoda.wp.pl/miasto,Bialystok,index.html'
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
return soup
# ------------------------------------------
def search_city(city):
soup = do_request()
links = soup.find_all('a')
for link in links:
if city in link.get('href'):
return link.get('href')
# -------------------------------------------
def get_cities_list():
soup = do_request()
links = soup.find_all('a')
cities = []
for link in links:
if "/cid" in link.get('href'):
city = link.get('href').split(',')[3]
cities.append(city)
return sorted(set(cities))
# -------------------------------------------
def get_weather_for_city(miasto):
baseURL = 'http://pogoda.wp.pl'
url = baseURL + search_city(miasto)
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
# print(soup.body)
weather_html = soup.find_all("div","static")[0]
weather_t = weather_html.get_text().split('\n')
# getting rid of empty list elements
weather_t = weather_t[1:-1]
weather_today = dict()
weather_today['temp'] = weather_t[3].split(':')[1]
weather_today['feel_temp'] = weather_t[4].split(':')[1]
weather_today['clouds'] = weather_t[5]
weather_today['dawn'] = weather_t[6].split(':',1)[1]
weather_today['dull'] = weather_t[7].split(':',1)[1]
weather_today['wind'] = weather_t[8].split(':')[1]
weather_today['pressure'] = weather_t[9].split(':')[1]
weather_tomorrow = dict()
weather_tomorrow['temp'] = weather_t[11].split(':')[1]
weather_tomorrow['feel_temp'] = weather_t[12].split(':')[1]
weather_tomorrow['clouds'] = weather_t[13]
weather_tomorrow['dawn'] = weather_t[14].split(':',1)[1]
weather_tomorrow['dull'] = weather_t[15].split(':',1)[1]
weather_tomorrow['wind'] = weather_t[16].split(':')[1]
weather_tomorrow['pressure'] = weather_t[17].split(':')[1]
weather = dict()
weather ['city'] = city
weather['today'] = weather_today
weather['tomorrow'] = weather_tomorrow
return weather
def print_weather(weather):
print u"""\
+------------------------------------------------------+
| Pogodynka dla miasta: %20s
+------------------------------------------------------+""" % (weather['city'])
time = ['today', 'tomorrow']
for t in time:
if t == 'today':
time = 'dzisiaj'
elif t == 'tomorrow':
time = 'jutro'
print u"""| Pogoda na %s
| +-- Temperatura: %-5s
| +-- Odczuwalna temperatura: %-5s
| +-- Zachmurzenie: %s
| +-- Wschód Słońca: %-7s
| +-- Zachód Słońca: %-7s
| +-- Prędkosć wiatru: %-7s
| +-- Ciśnienie: %-5s
+------------------------------------------------------+""" % (time, weather[t]['temp'], weather[t]['feel_temp'],\
weather[t]['clouds'], weather[t]['dawn'], weather[t]['dull'], weather[t]['wind'], \
weather[t]['pressure'])
if len(sys.argv) < 2:
print "Usage: %s <city>" % sys.argv[0]
print "Available cities: %s" % get_cities_list()
sys.exit(1)
if __name__ == "__main__":
city = sys.argv[1]
if sys.argv[1] == 'list':
cities = get_cities_list()
print "Lista dostepnych miast: "
print ", ".join(cities)
else:
weather = get_weather_for_city(city)
print_weather(weather)
Comments