~stefanor/ibid/translation-334764

« back to all changes in this revision

Viewing changes to ibid/plugins/google.py

  • Committer: Stefano Rivera
  • Date: 2009-03-08 13:16:28 UTC
  • Revision ID: stefano@rivera.za.net-20090308131628-nx1cbjo2xzjwomhs
Migrated plugins to new addresponse() signature

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
            results.append(u'"%s" %s' % (de_entity(title), item["unescapedUrl"]))
62
62
            
63
63
        if results:
64
 
            event.addresponse(u', '.join(results))
 
64
            event.addresponse(u'%s', u', '.join(results))
65
65
        else:
66
 
            event.addresponse(u"Wow! Google couldn't find anything.")
 
66
            event.addresponse(u"Wow! Google couldn't find anything")
67
67
 
68
68
    @match(r'^(?:rank|(?:google(?:fight|compare|cmp)))\s+(?:for\s+)?(.+?)\s+and\s+(.+?)$')
69
69
    def googlefight(self, event, term1, term2):
70
70
        count1 = int(self._google_api_search(term1, "small")["responseData"]["cursor"].get("estimatedResultCount", 0))
71
71
        count2 = int(self._google_api_search(term2, "small")["responseData"]["cursor"].get("estimatedResultCount", 0))
72
 
        event.addresponse(u'%s wins with %i hits, %s had %i hits' % 
73
 
            (count1 > count2 and (term1, count1, term2, count2) or (term2, count2, term1, count1))
74
 
        )
 
72
        event.addresponse(u'%(firstterm)s wins with %(firsthits)i hits, %(secondterm)s had %(secondhits)i hits',
 
73
            (count1 > count2 and {
 
74
                'firstterm':  term1,
 
75
                'firsthits':  count1,
 
76
                'secondterm': term2,
 
77
                'secondhits': count2,
 
78
            } or {
 
79
                'firstterm':  term2,
 
80
                'firsthits':  count2,
 
81
                'secondterm': term1,
 
82
                'secondhits': count1,
 
83
            }))
75
84
 
76
85
# Unfortunatly google API search doesn't support all of google search's
77
86
# features.
103
112
        if not font:
104
113
            event.addresponse(u'No result')
105
114
        else:
106
 
            event.addresponse(font.b.string)
 
115
            event.addresponse(u'%s', font.b.string)
107
116
 
108
117
    @match(r'^gdefine\s+(.+)$')
109
118
    def define(self, event, term):
114
123
            definitions.append(de_entity(li.contents[0].strip()))
115
124
 
116
125
        if definitions:
117
 
            event.addresponse(u' :: '.join(definitions))
 
126
            event.addresponse(u'%s', u' :: '.join(definitions))
118
127
        else:
119
 
            event.addresponse(u"Are you making up words again?")
 
128
            event.addresponse(u'Are you making up words again?')
120
129
 
121
130
    # Not supported by Google API: http://code.google.com/p/google-ajax-apis/issues/detail?id=24
122
131
    @match(r'^google(?:\.com?)?\.([a-z]{2})(?:\s+for)?\s+(.*)$')
138
147
                break
139
148
 
140
149
        if results:
141
 
            event.addresponse(u", ".join(results))
 
150
            event.addresponse('%s', u', '.join(results))
142
151
        else:
143
 
            event.addresponse(u"Wow! Google couldn't find anything.")
 
152
            event.addresponse(u"Wow! Google couldn't find anything")
144
153
 
145
154
# vi: set et sta sw=4 ts=4: