]> jfr.im git - irc/rizon/acid.git/commitdiff
Merge remote-tracking branch 'gl/milos/pyva+internets+add_google_image_search'
authorAdam <redacted>
Thu, 30 Oct 2014 06:19:17 +0000 (02:19 -0400)
committerAdam <redacted>
Thu, 30 Oct 2014 06:19:17 +0000 (02:19 -0400)
pyva/pyva/src/main/python/internets/api/google.py
pyva/pyva/src/main/python/internets/cmd_user.py

index f0433db2a89bde1ea5768a25b846148946830333..24fb58c827a0954beffca4c50c65e656c91b6096 100644 (file)
@@ -41,6 +41,15 @@ class Google(object):
                json = get_json(url)
                return json
        
+       def image_search(self, query, userip=None):
+               url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&'
+               parameters = {'q': query}
+               if userip:
+                       parameters['userip'] = userip
+               url += urlencode(parameters)
+               json = get_json(url)
+               return json
+
        def yt_search(self, query, num=1):
                url = 'https://gdata.youtube.com/feeds/api/videos?v=2&max-results=5&'
                url += urlencode({'q': query})
index 748ac563f816a8b3481be2649e0f7a78cfdb64a1..b4b1a008db0b5a1920e1e70829246041f82d1804 100644 (file)
@@ -170,12 +170,43 @@ def command_google_search(self, manager, opts, arg, channel, sender, userinfo):
        
        json = result[0]
        self.msg(channel, '[Google] @b%(title)s@b <@u%(url)s@u>' % {
-                                       'title': unescape(json['titleNoFormatting']), 
+                                       'title': unescape(json['titleNoFormatting']),
                                        'url': json['unescapedUrl']})
 
        if json['content'] != '':
                self.msg(channel, '[Google] @bDescription@b: %s' % unescape(json['content']).replace('<b>', '@b').replace('</b>', '@b'))
 
+def command_google_image_search(self, manager, opts, arg, channel, sender, userinfo):
+       try:
+               result = self.google.image_search(arg, userinfo['ip'] if userinfo['ip'] != '0' else '255.255.255.255')
+       except FeedError, e:
+               self.errormsg(channel, e.msg)
+               return
+       
+       if result['responseStatus'] == 403:
+               self.elog.warning('WARNING: Google Image Search failed: %s' % result['responseDetails'] if 'responseDetails' in result else 'unknown error')
+               self.notice(sender, 'Google Search is temporarily unavailable. Try again later.')
+               return
+       
+       result = result['responseData']['results']
+       if not result:
+               self.msg(channel, '[Google Image] No results found')
+               return
+       
+       json = result[0]
+       self.msg(channel, '[Google Image] @b%(title)s@b <@u%(url)s@u>' % {
+                                       'title': unescape(json['titleNoFormatting']),
+                                       'width': json['width'],
+                                       'height': json['height'],
+                                       'url': json['unescapedUrl']})
+
+       self.msg(channel, '[Google Image] @bSize@b: %(width)sx%(height)spx%(desc)s' % {
+
+                                       'width': json['width'],
+                                       'height': json['height'],
+                                       'desc': (' - @bDescription@b: %s' % unescape(json['content']).replace('<b>', '@b').replace('</b>', '@b')) if json['content'] else ''})
+
+
 def command_calc(self, manager, opts, arg, channel, sender, userinfo):
        try:  # local calculation using PyParsing
                result = self.nsp.eval(arg)
@@ -553,6 +584,9 @@ class UserCommandManager(CommandManager):
                        'g': 'google',
                        'google': (command_google_search, ARG_YES, 'Search for something on Google', [], 'google_search'),
                        
+                       'gi': 'google_image',
+                       'google_image': (command_google_image_search, ARG_YES, 'Search for images via Google Image', [], 'google_image_search'),
+
                        't': 'translate',
                        'translate': (command_bing_translate, ARG_YES, 'Translate something from a language to another', [], 'from to text'),