Advertisement
s4ros

pogodynka.py

Apr 8th, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. # -*- coding: utf8 -*-
  2. import urllib2
  3. from bs4 import BeautifulSoup
  4. import sys
  5. from pprint import pprint
  6.  
  7.  
  8. # -------------------------------------------
  9. def do_request():
  10.   url = 'http://pogoda.wp.pl/miasto,Bialystok,index.html'
  11.   response = urllib2.urlopen(url)
  12.   html = response.read()
  13.   soup = BeautifulSoup(html, 'html.parser')
  14.   return soup
  15.  
  16. # ------------------------------------------
  17. def search_city(city):
  18.   soup = do_request()
  19.   links = soup.find_all('a')
  20.   for link in links:
  21.     if city in link.get('href'):
  22.       return link.get('href')
  23.  
  24. # -------------------------------------------
  25. def get_cities_list():
  26.   soup = do_request()
  27.   links = soup.find_all('a')
  28.   cities = []
  29.   for link in links:
  30.     if "/cid" in link.get('href'):
  31.       city = link.get('href').split(',')[3]
  32.       cities.append(city)
  33.   return sorted(set(cities))
  34.  
  35. # -------------------------------------------
  36. def get_weather_for_city(miasto):
  37.   baseURL = 'http://pogoda.wp.pl'
  38.   url = baseURL + search_city(miasto)
  39.   response = urllib2.urlopen(url)
  40.   html = response.read()
  41.   soup = BeautifulSoup(html, 'html.parser')
  42. # print(soup.body)
  43.   weather_html = soup.find_all("div","static")[0]
  44.   weather_t = weather_html.get_text().split('\n')
  45.   # getting rid of empty list elements
  46.   weather_t = weather_t[1:-1]
  47.  
  48.   weather_today = dict()
  49.   weather_today['temp'] = weather_t[3].split(':')[1]
  50.   weather_today['feel_temp'] = weather_t[4].split(':')[1]
  51.   weather_today['clouds'] = weather_t[5]
  52.   weather_today['dawn'] = weather_t[6].split(':',1)[1]
  53.   weather_today['dull'] = weather_t[7].split(':',1)[1]
  54.   weather_today['wind'] = weather_t[8].split(':')[1]
  55.   weather_today['pressure'] = weather_t[9].split(':')[1]
  56.  
  57.   weather_tomorrow = dict()
  58.   weather_tomorrow['temp'] = weather_t[11].split(':')[1]
  59.   weather_tomorrow['feel_temp'] = weather_t[12].split(':')[1]
  60.   weather_tomorrow['clouds'] = weather_t[13]
  61.   weather_tomorrow['dawn'] = weather_t[14].split(':',1)[1]
  62.   weather_tomorrow['dull'] = weather_t[15].split(':',1)[1]
  63.   weather_tomorrow['wind'] = weather_t[16].split(':')[1]
  64.   weather_tomorrow['pressure'] = weather_t[17].split(':')[1]
  65.  
  66.   weather = dict()
  67.   weather ['city'] = city
  68.   weather['today'] = weather_today
  69.   weather['tomorrow'] = weather_tomorrow
  70.   return weather
  71.  
  72. def print_weather(weather):
  73.   print u"""\
  74. +------------------------------------------------------+
  75. |   Pogodynka dla miasta: %20s
  76. +------------------------------------------------------+""" % (weather['city'])
  77.   time = ['today', 'tomorrow']
  78.   for t in time:
  79.     if t == 'today':
  80.       time = 'dzisiaj'
  81.     elif t == 'tomorrow':
  82.       time = 'jutro'
  83.     print u"""| Pogoda na %s
  84. | +-- Temperatura: %-5s
  85. | +-- Odczuwalna temperatura: %-5s
  86. | +-- Zachmurzenie: %s
  87. | +-- Wschód Słońca: %-7s
  88. | +-- Zachód Słońca: %-7s
  89. | +-- Prędkosć wiatru: %-7s
  90. | +-- Ciśnienie: %-5s
  91. +------------------------------------------------------+""" % (time, weather[t]['temp'], weather[t]['feel_temp'],\
  92.       weather[t]['clouds'], weather[t]['dawn'], weather[t]['dull'], weather[t]['wind'], \
  93.       weather[t]['pressure'])
  94.  
  95. if len(sys.argv) < 2:
  96.   print "Usage: %s <city>" % sys.argv[0]
  97.   print "Available cities: %s" % get_cities_list()
  98.   sys.exit(1)
  99.  
  100.  
  101. if __name__ == "__main__":
  102.   city = sys.argv[1]
  103.   if sys.argv[1] == 'list':
  104.     cities = get_cities_list()
  105.     print "Lista dostepnych miast: "
  106.     print ", ".join(cities)
  107.   else:
  108.     weather = get_weather_for_city(city)
  109.     print_weather(weather)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement