import requests
import json

from hamper.interfaces import ChatPlugin


class Timez(ChatPlugin):
    name = 'timez'
    priority = 2

    long_desc = short_desc = (
        "Look up time for [ZIP code | City, State (US Only) | "
        "City Name, State, Country | City Name, Country | "
        "Airport Code | IP "
    )

    def setup(self, loader):
        try:
            self.api_key = loader.config['timez']['api-key']
        except (KeyError, TypeError):
            self.api_key = None

        api_url = "http://api.worldweatheronline.com/free/v1/tz.ashx"
        self.api_url = "%s?key=%s&q=%%s&format=json" % (api_url, self.api_key)

    def message(self, bot, comm):
        if not self.api_key:
            bot.reply(
                comm, "This plugin is missconfigured. Its missing an API key. "
                "Go register one at "
                "http://developer.worldweatheronline.com/apps/register"
            )
            return

        query = comm['message'].strip('timez ')
        resp = requests.get(self.api_url % query)
        if resp.status_code != 200:
            bot.reply(comm, "Error: A non 200 status code was returned")
        jresp = json.loads(resp.text)
        try:
            tz = jresp['data']['time_zone'][0]
            bot.reply(
                comm,
                "For %s, local time is %s at UTC offset %s" % (
                    query, tz['localtime'], tz['utcOffset']
                )
            )
        except KeyError:
            bot.reply(
                comm, "Sorry, the internet didn't understand your request."
            )
        # Always let the other plugins run
        return False


timez = Timez()

@@ -56,22 +56,38 @@
 ort Chat
-Plugin
+CommandPlugin, Command
 %0A%0A%0Aclass
@@ -97,16 +97,23 @@
 mez(Chat
+Command
 Plugin):
@@ -154,199 +154,8 @@
  2%0A%0A
-    long_desc = short_desc = (%0A        %22Look up time for %5BZIP code %7C City, State (US Only) %7C %22%0A        %22City Name, State, Country %7C City Name, Country %7C %22%0A        %22Airport Code %7C IP %22%0A    )%0A%0A
     
@@ -477,24 +477,376 @@
 ey)%0A
-%0A    def message
+        super(Timez, self).setup(loader)%0A%0A    class Timez(Command):%0A        ''' '''%0A        name = 'timez'%0A        regex = '%5Etimez (.*)'%0A%0A        long_desc = short_desc = (%0A            %22Look up time for %5BZIP code %7C City, State (US Only) %7C %22%0A            %22City Name, State, Country %7C City Name, Country %7C %22%0A            %22Airport Code %7C IP %22%0A        )%0A%0A        def command
 (sel
@@ -857,19 +857,31 @@
 ot, comm
-):%0A
+, groups):%0A    
         
@@ -888,24 +888,31 @@
 if not self.
+plugin.
 api_key:%0A   
@@ -908,16 +908,20 @@
 pi_key:%0A
+    
         
@@ -927,32 +927,36 @@
      bot.reply(%0A
+    
                 
@@ -1015,13 +1015,8 @@
  API
- key.
  %22%0A 
@@ -1030,17 +1030,26 @@
         
-%22
+    %22key. 
 Go regis
@@ -1061,16 +1061,20 @@
 ne at %22%0A
+    
         
@@ -1141,34 +1141,42 @@
 er%22%0A            
+  
-)%0A
+  )%0A    
             retu
@@ -1183,24 +1183,28 @@
 rn%0A%0A        
+  
+  
 query = comm
@@ -1239,16 +1239,20 @@
         
+    
 resp = r
@@ -1264,24 +1264,31 @@
 ts.get(self.
+plugin.
 api_url %25 qu
@@ -1292,16 +1292,20 @@
  query)%0A
+    
         
@@ -1332,16 +1332,20 @@
 != 200:%0A
+    
         
@@ -1409,24 +1409,26 @@
 urned%22)%0A
+%0A 
         
 jresp = 
@@ -1419,16 +1419,19 @@
         
+   
 jresp = 
@@ -1452,16 +1452,21 @@
 p.text)%0A
+%0A    
         
@@ -1482,16 +1482,20 @@
         
+    
 tz = jre
@@ -1521,16 +1521,20 @@
 ne'%5D%5B0%5D%0A
+    
         
@@ -1568,14 +1568,22 @@
     
+    
 comm,%0A
+    
     
@@ -1658,24 +1658,28 @@
             
+    
 query, tz%5B'l
@@ -1714,34 +1714,42 @@
                 
+   
+ 
 )%0A
+    
             )%0A  
@@ -1750,24 +1750,28 @@
   )%0A        
+   
+ 
 except KeyEr
@@ -1779,32 +1779,36 @@
 or:%0A            
+    
 bot.reply(%0A     
@@ -1814,24 +1814,28 @@
             
+  
+  
 comm, %22Sorry
@@ -1886,34 +1886,43 @@
 t.%22%0A            
-)%0A
+    )%0A%0A    
         # Always
@@ -1948,16 +1948,20 @@
 ins run%0A
+    
         

make timez a ChatCommandPlugin/Command plugin