#!/usr/bin/env python # # Google # Copyright (C) 2005-2007 Petko D. Petkov (GNUCITIZEN) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA __version__ = '2.0' __author__ = 'Petko D. Petkov; pdp (architect)' __doc__ = """ Originally google.py Google (GNUCITIZEN) http://www.gnucitizen.org by Petko D. Petkov; pdp (arhictect) for Python 2.5 Name changed to usernameGen.py and modified by Jason Wood http://www.jwnetworkconsulting.com Changes: URL was changed to use mobile search Regex was tweaked so it would work again Limited the response to only the title of each result. Added a new class and method which: - takes the results of the title for the result and extracts the persons first and last name - then makes usernames with the first initial & last name and first name & last initial """ import re import urllib import urllib2 import logging class Get(object): """ Get The power of python in a single object """ def __init__(self): self.user_agent = 'User-Agent: Mozilla/5.0 ' \ + '(Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.4) ' \ + 'Gecko/20070515 Firefox/2.0.0.4' def get(self, url): """ get(url) -> Response Open given url and return response. """ try: request = urllib2.Request(url) request.add_header('User-Agent', self.user_agent) logging.debug('Get.get - getting url ' + url) result = urllib2.urlopen(request) except: raise RuntimeError('unable to open url') return result class Search(Get): """ Search The power of Google in a single object """ def search(self, q, start = 0, num = 10): """ search(q, start = 0, num = 10) -> generator Do google web search. """ url = 'http://www.google.com/m/search?' query = urllib.urlencode({'q':q, 'start':start, 'num':num}) result = self.get(url + query) content = result.read() tokens = re.findall( '