#!/usr/bin/env python # # UsernameGen # Copyright (C) 2009 Jason Wood (JW Network Consulting) # # 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.1.1' __author__ = 'Jason Wood - aka Tadaka' __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: 6/28/2009 - Added "paddbottom" tag fix by Robin Wood for a new tag to match on - Added fix for the regex by Robin Wood - Added scheme for lastname + first initial usernames 6/16/2009 - Added handling for when there are middle names. creates usernames in the format of first initial, middle initial, last name 6/12/2009 - 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 ' \ + '(Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.11) ' \ + 'Gecko/2009060214 Firefox/3.0.11' 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() # print content tokens = re.findall( # '