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

import urllib2
import json
import re

from plugins.plugin import Plugin
from bs4            import BeautifulSoup
from twisted.python import log

from clarifai.client import ClarifaiApi

from bytebot_config import BYTEBOT_HTTP_TIMEOUT, BYTEBOT_HTTP_MAXSIZE
from bytebot_config import BYTEBOT_PLUGIN_CONFIG

class shorturl(Plugin):
    def googl(self, url):
        log.msg("Shortening URL %s" % url)

        post_url = 'https://www.googleapis.com/urlshortener/v1/url'
        postdata = {'longUrl': url}
        headers = {'Content-Type':'application/json'}

        req = urllib2.Request(
            post_url,
            json.dumps(postdata),
            headers
        )
        data = urllib2.urlopen(url=req, timeout=BYTEBOT_HTTP_TIMEOUT).read(BYTEBOT_HTTP_MAXSIZE)
        ret = json.loads(data)[u'id'].encode('ascii', 'ignore')

        return ret

    def krzus(self, url):
        log.msg("Shortening URL %s" % url)

        post_url = 'http://krz.us/api/v1/short'
        postdata = {
            'url': url,
            'key': BYTEBOT_PLUGIN_CONFIG['shorturl']['api_key']
        }
        headers = {'Content-Type':'application/json'}

        req = urllib2.Request(
            post_url,
            json.dumps(postdata),
            headers
        )
        data = urllib2.urlopen(url=req, timeout=BYTEBOT_HTTP_TIMEOUT).read(BYTEBOT_HTTP_MAXSIZE)
        ret = json.loads(data)[u'url_short'].encode('ascii', 'ignore')

        return ret

    def getTitle(self, url):
        data = urllib2.urlopen(url=url, timeout=BYTEBOT_HTTP_TIMEOUT)
        soup = BeautifulSoup(data)
        return soup.title.getText().encode('utf-8')[:60]
    
    def getTags(self, url):
        ret = ''
        api = ClarifaiApi(BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_id'], BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_secret'])
        tags = api.tag_image_urls(url)
        
        if(tags[u'status_code'] == "OK"):
            ret = ', '.join(tags[u'results'][0][u'result'][u'tag'][u'classes'])

print ret

    def onPrivmsg(self, irc, msg, channel, user):

        try:
            url = re.findall('http[s]?://(?:[^\s]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)[0]
            try:
                short_function = getattr(self, BYTEBOT_PLUGIN_CONFIG['shorturl']['shortener'])
                shorturl = short_function(url)
            except NameError:
                log.msg('Could not load shortener function ' + BYTEBOT_PLUGIN_CONFIG)
        except Exception as e:
            log.msg(e)
            return

        if(url[-4:]=='.jpg'):
            try:
                desc = 'Tags : ' + self.getTags(url)
            except Exception as e:
                desc = ''
        else
            try:
                desc = 'Title : ' + self.getTitle(url)
            except Exception as e:
                desc = ''

        if desc != '':
            irc.msg(channel, desc)
            irc.msg(channel, '\tURL: %s' % shorturl)
        else:
            irc.msg(channel, "URL: %s" % shorturl)
@@ -122,27 +122,16 @@
 rom bs4 
-           
 import B
@@ -336,16 +336,17 @@
 CONFIG%0A%0A
+%0A
 class sh
@@ -361,16 +361,17 @@
 lugin):%0A
+%0A
     def 
@@ -562,32 +562,33 @@
 %7B'Content-Type':
+ 
 'application/jso
@@ -776,32 +776,45 @@
 P_TIMEOUT).read(
+%0A            
 BYTEBOT_HTTP_MAX
@@ -1175,16 +1175,17 @@
 t-Type':
+ 
 'applica
@@ -1389,16 +1389,29 @@
 T).read(
+%0A            
 BYTEBOT_
@@ -1707,20 +1707,16 @@
 ')%5B:60%5D%0A
-    
 %0A    def
@@ -1805,32 +1805,59 @@
 FIG%5B'shorturl'%5D%5B
+%0A                          
 'clarifai_app_id
@@ -1957,24 +1957,16 @@
 ls(url)%0A
-        
 %0A       
@@ -2185,16 +2185,33 @@
 findall(
+%0A                
 'http%5Bs%5D
@@ -2367,32 +2367,74 @@
 T_PLUGIN_CONFIG%5B
+%0A                                         
 'shorturl'%5D%5B'sho
@@ -2582,16 +2582,40 @@
 tion ' +
+%0A                       
  BYTEBOT
@@ -2723,18 +2723,20 @@
 url%5B-4:%5D
+ 
 ==
+ 
 '.jpg'):
@@ -3177,20 +3177,21 @@
 URL: %25s%22 %25 shorturl)
+%0A

format changed to PEP 0008