IMDweather

Fetch today's weather and forecast from India Meteorological Department. - Pratap Vardhan


In [1]:
"""
IMD.py
======
Fetch Today's Weather from India Meteorological Department
"""
import os
import time
import hashlib
import requests
from bs4 import BeautifulSoup
import pandas as pd

In [2]:
if not os.path.exists('.cache'):
    os.makedirs('.cache')

_UA = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
_SESSION = requests.Session()
IMD = "http://www.imd.gov.in/"
SERVERLINK = "http://202.54.31.7/citywx/city_weather1.php?"
DAYOLD = time.time() - 1 * 24 * 60 * 60
LOOKUP = "baselookup.csv"

def get(url):
    """Return cached page for url"""
    path = os.path.join('.cache', hashlib.md5(url).hexdigest() + '.html')
    # Fetch IMD site only if file doesn't exist or if older than a day
    if not os.path.exists(path) or os.stat(path).st_mtime < DAYOLD:
        print 'Fetching %s from Web' % url
        response = _SESSION.get(url, headers={'User-Agent': _UA})
        with open(path, 'w') as fil:
            fil.write(response.text.encode('utf-8'))
    return BeautifulSoup(open(path), 'html.parser')

In [3]:
def fetch_imd(place):
    """Fetch Results from IMD"""
    if not os.path.exists(LOOKUP):
        soup = get(IMD)
        places = []
        for point in soup.find_all('area'):
            places.append({
                           'ID': "'%s'" % point.get('href').split('id=')[1],
                           'PLACE': point.get('title')
                           })
        pd.DataFrame(places).to_csv(LOOKUP, index=False)

    lookup = pd.read_csv(LOOKUP)
    id = lookup[lookup.PLACE==place.upper()].ID.values
    if id:
        soup = get(SERVERLINK + 'id=' + id[0][1:-1])
        daytable = soup.find('table').find('table').find_all('tr')[1:]
        print "IMD - Weather @ %s" % place
        for rows in daytable:
            row = rows.find_all('td')
            print "%s: %s" % (row[0].text, row[1].text.strip())
        print "Today's Forecast: %s" % soup.find('table').select('\
                tr[bgcolor,ffffff] td[colspan,2] font[size,1+]')[1].text.strip()
        print time.strftime("%d/%m/%Y")
    else:
        print "%s is not indexed on IMD site." % place

In [4]:
# Usage
fetch_imd('Hyderabad')


IMD - Weather @ Hyderabad
Maximum Temp(oC): 32.6
Departure from Normal(oC): 1
Minimum Temp (oC): 23.8
Departure from Normal(oC): 2
24 Hours Rainfall (mm): 1.2
Todays Sunset (IST): 18:19
Tommorows Sunrise (IST): 06:04
Moonset (IST): 11:50
Moonrise (IST): 23:27
Today's Forecast: SKY CONDITION WOULD BE GENERALLY CLOUDY.  RAIN OR THUNDERSHOWERS   WOULD  OCCUR IN PARTS OF CITY.  MAXIMUM AND MINIMUM TEMPERATURES WOULD BE AROUND 33 AND 24 DEGREES CELSIUS RESPECTIVELY.
15/09/2014