vigilance.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

u'''this script retrieves weather alert level (vigilance météo)
from meteo.gp rss feed.
it requires to be internet connected.
'''
#
#       vigilance.py
#       
#       Copyright 2011 olivier watte <owatte@avaland.org>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import feedparser
import sys



def main():
    '''print meteo alert level
    
    if sys.argv[1] is 'h' or '--help', or if len(sys.argv) == 1,
    print %prog usage
    
    else if sys.argv[1] is a known alias,
    print meteo alert level
    
    else print ERROR, no data for sys.argv[1]

    known aliases :
    - idn ilesdunord nord = Iles du Nord
    - gp guadeloupe 971 = Guadeloupe
    - mq martinique 972 = Martinique
    - gf guyane 974 = Guyane    
     
    aliases are key insensitive. 
    '''
    
    url_feed_vigilance = "http://www.meteo.gp/rss/rss_vigi.xml"

    usage = '\n'.join(['retrieves weather alert level ',
    'from meteo.gp website',
    'for French West Indies and French Guyane ',
    '',
    'usage : %prog <area_alias>',
    'where area_alias in :',
    '****************************************',
    '* area         * aliases               *',
    '****************************************',
    '* Iles du Nord * idn, ilesdunord, nord *',
    '* Guadeloupe   * gp, guadeloupe, 971   *',
    '* Martinique   * mq, martinique, 972   *',
    '* Guyane       * gf, guyane, 974       *',
    '****************************************',
    '',
    'example for the Guadeloupe:',
    'vigilance.py 971',
    'vigilance.py gp']
    )

    country_aliases = [ 
        ['IlesduNord', ['idn', 'ilesdunord', 'nord']],
        ['Guadeloupe', ['gp', 'guadeloupe', '971']], 
        ['Martinique',['mq', 'martinique', '972' ]],
        ['Guyane', ['gf', 'guyane', '974']]
    ]    
    
    if len(sys.argv)==1 or sys.argv[1] in ['-h', '--help']:
        print usage
    else:
        url_feed_vigilance = url_feed_vigilance
        wanted = sys.argv[1].lower()
        vigilance = get_vigilance_levels(url_feed_vigilance)              
        area = [ alias[0] for alias in country_aliases if wanted in alias[1] ]

        if len(area) > 0 and vigilance.has_key(area[0]):
            result = ''.join([
                vigilance[area[0]], 
                ' (',
                vigilance['updated'],
                ')'
            ])
        else :
            result = ''.join([
                'ERREUR : no data for ', 
                sys.argv[1],
                '\n',
                usage
            ])
        print result

def get_vigilance_levels(url_feed_vigilance):
    '''return meteo alert levelextracts weather vigilance level from the RSS.
    
    @url_feed_vigilance : RSS feed to parse
    returns the alert level color (in french):
    - vert (green)
    - jaune (yellow)
    - rouge (red)
    - violet (purple)
    '''
    
    feed = feedparser.parse(url_feed_vigilance)
    vigilance = feed['entries'][0].summary
    
    bruits = [u'\xa0', u'\n', ' ']
    for bruit in bruits :
        vigilance = vigilance.replace(bruit, '')
    vigilances = vigilance.split('.')

    vigilance = {}
    # alert level
    for dataz in vigilances:
        if (":" in dataz): 
            data = dataz.split(":")
            area = data[0]
            level = data[1]
            vigilance[area] = level
    # feed date
    vigilance['updated'] = feed['entries'][0].updated       
     
    return vigilance


if __name__ == '__main__':
    main()
    

:)