#!/usr/bin/env python
#
# weather-now - Current weather conditions
# This file is part of vane
#
# Copyright (c) 2013 Trevor Parker <trevor@trevorparker.com>
# All rights reserved
#
# Distributed under the terms of the modified BSD license (see LICENSE)

import argparse
import json
import requests
import sys
import urllib

parser = argparse.ArgumentParser(description='Current weather conditions')
parser.add_argument(
    '--units', dest='units', type=str, default='imperial',
    choices=['imperial', 'metric'], help='units to display')
parser.add_argument(
    'loc', type=str, nargs=argparse.REMAINDER,
    help="location, usually 'City, State' or 'City, Country'")
args = parser.parse_args()
loc = urllib.quote_plus(' '.join(args.loc))
units = args.units

try:
    u = "http://api.openweathermap.org/data/2.5/weather?q={0}&units={1}"
    r = requests.get(u.format(loc, units))
    j = json.loads(r.text)
except:
    sys.stderr.write("Couldn't load current conditions\n")

if (j['cod'] != 200):
    sys.exit(j['message'])

temperature = j['main']['temp']
temperature_unit = 'F' if (units == 'imperial') else 'C'
conditions = j['weather'][0]['description']

s = "{0} with a temperature of {1}" u"\u00B0" "{2}"
print s.format(
    conditions[0].upper() + conditions[1:].lower(),
    int(round(temperature)), temperature_unit)
