<commit_before>'''Google Maps V3 geocoder.

Largely adapted from the existing v2 geocoder with modifications made where
possible to support the v3 api as well as to clean up the class without
breaking its compatibility or diverging its api too far from the rest of the
geocoder classes.
'''

import base64
import hashlib
import hmac

from urllib2 import urlopen
import sys

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        from django.utils import simplejson as json

try:
    from django.utils.http import urlencode
except ImportError:
    from urllib import urlencode

from geopy.geocoders.base import Geocoder, GeocoderResultError
from geopy import util



class GoogleV3(Geocoder):
    '''Geocoder using the Google Maps v3 API.'''

    def __init__(self, domain='maps.googleapis.com', protocol='http',
                 client_id=None, secret_key=None):
        '''Initialize a customized Google geocoder.

        API authentication is only required for Google Maps Premier customers.

        ``domain`` should be the localized Google Maps domain to connect to. The default
        is 'maps.google.com', but if you're geocoding address in the UK (for
        example), you may want to set it to 'maps.google.co.uk' to properly bias results.

        ``protocol`` http or https.

        ``client_id`` Premier account client id.

        ``secret_key`` Premier account secret key.
        '''
        super(GoogleV3, self).__init__()

        if protocol not in ('http', 'https'):
            raise ValueError, 'Supported protocols are http and https.'
        if client_id and not secret_key:
            raise ValueError, 'Must provide secret_key with client_id.'
        if secret_key and not client_id:
            raise ValueError, 'Must provide client_id with secret_key.'

        self.domain = domain.strip('/')
        self.protocol = protocol
        self.doc = {}
        self.url = None

        if client_id and secret_key:
            self.client_id = client_id
            self.secret_key = secret_key
            self.premier = True
        else:
            self.premier = False

    def get_signed_url(self, params):
        '''Returns a Premier account signed url.'''
        params['client'] = self.client_id
        url_params = {'protocol': self.protocol, 'domain': self.domain,
                      'params': urlencode(params)}
        secret = base64.urlsafe_b64decode(self.secret_key)
        url_params['url_part'] = (
            '/maps/api/geocode/json?%(params)s' % url_params)
        signature = hmac.new(secret, url_params['url_part'], hashlib.sha1)
        url_params['signature'] = base64.urlsafe_b64encode(signature.digest())

        return ('%(protocol)s://%(domain)s%(url_part)s'
                '&signature=%(signature)s' % url_params)

    def get_url(self, params):
        '''Returns a standard geocoding api url.'''
        return 'http://%(domain)s/maps/api/geocode/json?%(params)s' % (
            {'domain': self.domain, 'params': urlencode(params)})

    def geocode_url(self, url, exactly_one=True, timeout=None):
        '''Fetches the url and returns the result.'''
        util.logger.debug("Fetching %s..." % url)

        kwargs = dict(url=url)
        if timeout and sys.version_info > (2,6,0): kwargs['timeout'] = timeout
        page = urlopen(**kwargs)
<commit_msg>Google Maps - Updated site to look at API KEY<commit_after>'''Google Maps V3 geocoder.

Largely adapted from the existing v2 geocoder with modifications made where
possible to support the v3 api as well as to clean up the class without
breaking its compatibility or diverging its api too far from the rest of the
geocoder classes.
'''

import base64
import hashlib
import hmac

from urllib2 import urlopen
import sys

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        from django.utils import simplejson as json

try:
    from django.utils.http import urlencode
except ImportError:
    from urllib import urlencode

from geopy.geocoders.base import Geocoder, GeocoderResultError
from geopy import util



class GoogleV3(Geocoder):
    '''Geocoder using the Google Maps v3 API.'''

    def __init__(self, domain='maps.googleapis.com', protocol='http',
                 client_id=None, secret_key=None, api_key=None):
        '''Initialize a customized Google geocoder.

        API authentication is only required for Google Maps Premier customers.

        ``domain`` should be the localized Google Maps domain to connect to. The default
        is 'maps.google.com', but if you're geocoding address in the UK (for
        example), you may want to set it to 'maps.google.co.uk' to properly bias results.

        ``protocol`` http or https.

        ``client_id`` Premier account client id.

        ``secret_key`` Premier account secret key.
        '''
        super(GoogleV3, self).__init__()

        if protocol not in ('http', 'https'):
            raise ValueError, 'Supported protocols are http and https.'
        if client_id and not secret_key:
            raise ValueError, 'Must provide secret_key with client_id.'
        if secret_key and not client_id:
            raise ValueError, 'Must provide client_id with secret_key.'
        if not secret_key and not client_id and not api_key:
            raise ValueError, 'Must provide at least an api_key'

        self.domain = domain.strip('/')
        self.protocol = protocol
        self.doc = {}
        self.url = None

        if client_id and secret_key:
            self.client_id = client_id
            self.secret_key = secret_key
            self.premier = True
        else:
            self.api_key = api_key
            self.premier = False

    def get_signed_url(self, params):
        '''Returns a Premier account signed url.'''
        params['client'] = self.client_id
        url_params = {'protocol': self.protocol, 'domain': self.domain,
                      'params': urlencode(params)}
        secret = base64.urlsafe_b64decode(self.secret_key)
        url_params['url_part'] = (
            '/maps/api/geocode/json?%(params)s' % url_params)
        signature = hmac.new(secret, url_params['url_part'], hashlib.sha1)
        url_params['signature'] = base64.urlsafe_b64encode(signature.digest())

        return ('%(protocol)s://%(domain)s%(url_part)s'
                '&signature=%(signature)s' % url_params)

    def get_url(self, params):
        '''Returns a standard geocoding api url.'''
        params['key']=self.api_key
        params['sensor'] = params.get('sensor', False)
        return 'http://%(domain)s/maps/api/geocode/json?%(params)s' % (
            {'domain': self.domain, 'params': urlencode(params)})

    def geocode_url(self, url, exactly_one=True, timeout=None):
        '''Fetches the url and returns the result.'''
        util.logger.debug("Fetching %s..." % url)

        kwargs = dict(url=url)
        if timeout and sys.version_info > (2,6,0): kwargs['timeout'] = timeout
        page = urlopen(**kwargs)

{'fraction_non_alphanumeric': 0.09964714184897672, 'fraction_numerical': 0.0059280169371912494, 'mean_word_length': 2.6791277258566977, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6583472', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1977, 'n_words': 654}