#!/usr/bin/env python
# =============================================================================
# file = weather.py
# description = gbot module
# author = GR <https://github.com/shortdudey123>
# create_date = 2014-07-12
# mod_date = 2014-07-12
# version = 0.1
# usage = loaded by gbot
# notes =
# python_ver = 2.7.6
# =============================================================================

import urllib
import json
import re

adminOnly = False
commandName = 'weather'
version = 0.1
help = """
Gets the weather for the given US airport code
Usage: {0} <IATA airpot code>
Admin Only: {1}
Version: {2}
Note: information on IATA codes can be found at the following links
    http://www.iata.org/publications/Pages/code-search.aspx
    http://en.wikipedia.org/wiki/International_Air_Transport_Association_airport_code
""".format(commandName, adminOnly, version)

def execModule(channel, message, nick, botSelf):
    retCommands = []
    iata_format = "^([A-Z])[A-Z][A-Z]$"
    if len(message.split()) == 1:
        pass
    elif len(message.split()) == 2:
        location = message.split()[1]
        weatherInfo = []
        if len(location) == 3 and re.match(iata_format, location.upper()):
            requestData = urllib.urlopen('http://services.faa.gov/airport/status/{0}?format=json'.format(location))
            try:
                data = json.load(requestData)
                weatherInfo.append('{0} ({1}) in {2}, {3}'.format(data['name'], data['IATA'], data['city'], data['state']))
                weatherInfo.append('Temperature: {0}'.format(data['weather']['temp']))
                weatherInfo.append('Visibility: {0}m'.format(data['weather']['visibility']))
                weatherInfo.append('Sky: {0}'.format(data['weather']['weather']))
                weatherInfo.append('Wind: {0}'.format(data['weather']['wind']))
                weatherInfo.append('Last updated: {0}'.format(data['weather']['meta']['updated']))
            except ValueError, e:
                botSelf.bot.sendMessage(channel, 'Please use a valid IATA airport code (http://www.iata.org/publications/Pages/code-search.aspx or Wikipidia)')
        if weatherInfo != []:
            for line in weatherInfo:
                botSelf.bot.sendMessage(channel, line)

    return retCommands

if __name__ == "__main__":
    filename = __file__.split('.')[0]
    help(filename)
@@ -943,31 +943,25 @@
     
+re_
 iata
-_format
  = %22%5E
-(
 %5BA-Z%5D
-)
 %5BA-Z
@@ -969,16 +969,64 @@
 %5BA-Z%5D$%22%0A
+    re_zipCode = %22%5E%5B0-9%5D%5B0-9%5D%5B0-9%5D%5B0-9%5D%5B0-9%5D$%22%0A%0A
     if l
@@ -1159,24 +1159,99 @@
 erInfo = %5B%5D%0A
+        self.log(%22Getting weather info for %7B0%7D%22.format(location.upper()))%0A%0A
         if l
@@ -1285,19 +1285,15 @@
 tch(
+re_
 iata
-_format
 , lo
@@ -2049,24 +2049,107 @@
 updated'%5D))%0A
+                self.log(%22Got weather info for IATA %7B0%7D%22.format(location.upper()))%0A
             
@@ -2330,16 +2330,282 @@
 idia)')%0A
+                self.log(%22Failed to get weather info for IATA %7B0%7D%22.format(location.upper()))%0A%0A        elif len(location) == 5 and re.match(re_zipCode, location):%0A            botSelf.bot.sendMessage(channel, 'Zip codes are not supported yet.  Sorry :(')            %0A%0A
         

add zip code format, add logging