Skip to content

Instantly share code, notes, and snippets.

@pagenoare
Created July 1, 2009 14:08
Show Gist options
  • Save pagenoare/138796 to your computer and use it in GitHub Desktop.
Save pagenoare/138796 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import random
import urllib2
from urllib import urlencode
import irssi
agents = (
'Mozilla/5.0',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072612 GranParadiso/3.0.1',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9) Gecko/2008061015 Firefox/3.0',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15',
'Opera/9.51 (Windows NT 6.0; U; pl)',
'Opera/9.51 (X11; Linux i686; U; pl)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a1) Gecko/2008072306 Shiretoko/3.1a1',
'Mozilla/5.0 (Windows; U; Windows NT 5.2; pl; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1',
'Opera/9.52 (Windows NT 5.1; U; pl)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.16) Gecko/20080703 Mandriva/2.0.0.16-1.1mdv2008.1 (2008.1) Firefox/2.0.0.16',
)
replacements = (('<b>', '*'), ('</b>', '*'), ('<em>', '*'), ('</em>', '*'), ('&#39;', '\''), ('&amp;', '&'), ('&quot;', '"'))
pattern = re.compile('<h3 class=r><a href="(.*?)" class=l.*?>(.*?)</a></h3>', re.DOTALL)
def search(data, server, witem):
global formated
query = data
if not query:
return
params = urlencode({'hl' : 'pl', 'q' : query})
url = 'http://www.google.pl/search?%s'
request = urllib2.Request(url % params)
request.add_header('User-Agent', random.choice(agents))
try:
opener = urllib2.build_opener().open(request)
content = opener.read()
results = pattern.findall(content)
opener.close()
except:
print "Brak wynikow"
return
if not results:
print "Brak wynikow"
return
def r(txt):
for what, to in replacements:
txt = txt.replace(what, to)
return txt
i = 1
for url, title in results:
formated[i] = "(%i) %s <%s>" % (i, r(title), url)
i += 1
print " | ".join(formated.values())
def send(data, server, witem):
global formated
if not formated:
return
if not data.isdigit():
return
if not data:
return
if not witem:
return
witem.command('msg %s %s' % (witem.name, formated.get(int(data))))
formated = {}
irssi.command_bind('sr', search)
irssi.command_bind('g', send)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment