Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Meteo2Text
#!/usr/bin/env python
# coding: utf-8
from bs4 import BeautifulSoup
import urllib
import urllib2
import re
import sys
ws_re = re.compile('(\d+) km/h')
prec_re = re.compile('(\d+)%')
def hourly_weather(url):
r = urllib2.urlopen(url)
soup = BeautifulSoup(r)
day_divs = soup.find_all("div", class_="group-day-more-info")
days = []
for div in day_divs:
day = []
table = div.find('table')
rows = table.find_all('tr')
for hour_col in rows[0].find_all('strong'):
hour = {}
hour['start'] = int(hour_col.contents[0][:2])
day.append(hour)
for r, row in enumerate(rows[1:], start=1):
for c, col in enumerate(row.find_all('td')):
if r == 1: # sky
day[c]['sky'] = col.find('span').text
elif r == 3: # wind
speeds = ws_re.findall(col.text)
day[c]['wind'] = map(int, speeds)
elif r == 5: # precipitations
prec = prec_re.search(col.text)
if prec is not None:
prec = int(prec.group(0)[:-1])
day[c]['prec'] = prec
days.append(day)
day = days[0]
res = ""
for h in day:
if h['prec'] is not None:
prec = '{0}%'.format(h['prec'])
else:
prec = '?'
res += u'{0}h > {1}h : {2} // {3} // {4}\n'.format(h['start'], (h['start']+3) % 24, h['sky'], '{0} km/h'.format(h['wind'][0]), prec)
return res
report = hourly_weather('http://www.meteofrance.com/previsions-meteo-france/besancon/25000')
API_BASE = 'https://smsapi.free-mobile.fr/sendmsg?pass={0}&user={1}&{2}'
password = 'LePassword'
user = 'LeUser'
urllib2.urlopen(API_BASE.format(password, user, urllib.urlencode({'msg':report})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment