<s> import math <EOL> import random <EOL> import logging <EOL> import time <EOL> import urllib <EOL> import atom . service <EOL> import gdata . auth <EOL> import gdata . service <EOL> import gdata . alt . appengine <EOL> import simplejson <EOL> from django . conf import settings <EOL> from common import exception <EOL> CONTACTS_RESOURCE = "<STR_LIT>" <EOL> GROUPS_RESOURCE = "<STR_LIT>" <EOL> SCOPE_URL = '<STR_LIT>' <EOL> converter = lambda x : x <EOL> def client ( ) : <EOL> additional_headers = { '<STR_LIT>' : '<STR_LIT:2>' } <EOL> client = gdata . service . GDataService ( additional_headers = additional_headers ) <EOL> gdata . alt . appengine . run_on_appengine ( client ) <EOL> return client <EOL> def auth_sub_url ( next_url ) : <EOL> c = client ( ) <EOL> params = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True } <EOL> if settings . EMAIL_LIMIT_DOMAIN : <EOL> params [ '<STR_LIT>' ] = settings . EMAIL_LIMIT_DOMAIN <EOL> auth_url = c . GenerateAuthSubURL ( next_url , <EOL> ( SCOPE_URL , ) , <EOL> ** params ) <EOL> return auth_url <EOL> def auth_sub_token_from_request ( request ) : <EOL> return gdata . auth . extract_auth_sub_token_from_url ( request . get_full_path ( ) ) <EOL> def upgrade_to_session_token ( token ) : <EOL> c = client ( ) <EOL> return c . upgrade_to_session_token ( token ) <EOL> def get_groups ( token ) : <EOL> c = client ( ) <EOL> c . override_token = token <EOL> resource = GROUPS_RESOURCE <EOL> try : <EOL> feed_json = c . Get ( resource , converter = converter ) <EOL> rv = simplejson . loads ( feed_json ) <EOL> except gdata . service . RequestError , e : <EOL> raise <EOL> if e . args and '<STR_LIT:status>' in e . args [ <NUM_LIT:0> ] and e . args [ <NUM_LIT:0> ] [ '<STR_LIT:status>' ] == <NUM_LIT> : <EOL> raise exception . ServiceError ( '<STR_LIT>' ) <EOL> raise <EOL> return rv [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def get_system_group ( token , group = "<STR_LIT>" ) : <EOL> groups = get_groups ( token ) <EOL> for group in groups : <EOL> if group . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:id>' ) == '<STR_LIT>' : <EOL> return group [ '<STR_LIT:id>' ] [ '<STR_LIT>' ] <EOL> return None <EOL> def get_contacts ( token , group = None , index = <NUM_LIT:1> , max = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> c = client ( ) <EOL> c . override_token = token <EOL> logging . info ( "<STR_LIT>" , token ) <EOL> resource = CONTACTS_RESOURCE % ( index , max + <NUM_LIT:1> ) <EOL> if group : <EOL> resource += "<STR_LIT:&>" + urllib . urlencode ( { '<STR_LIT>' : group } ) <EOL> logging . info ( "<STR_LIT>" , resource ) <EOL> more = False <EOL> try : <EOL> feed_json = c . Get ( resource , converter = converter ) <EOL> rv = simplejson . loads ( feed_json ) <EOL> if len ( rv [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) > max : <EOL> more = index + max <EOL> except gdata . service . RequestError , e : <EOL> raise <EOL> if e . args and '<STR_LIT:status>' in e . args [ <NUM_LIT:0> ] and e . args [ <NUM_LIT:0> ] [ '<STR_LIT:status>' ] == <NUM_LIT> : <EOL> raise exception . ServiceError ( '<STR_LIT>' ) <EOL> raise <EOL> return rv [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ : max ] , more <EOL> def get_contacts_emails ( token , group = None , index = <NUM_LIT:1> , max = <NUM_LIT:100> ) : <EOL> raw_contacts , more = get_contacts ( token , group , index , max ) <EOL> contacts = [ ] <EOL> for c in raw_contacts : <EOL> name = c [ '<STR_LIT:title>' ] [ '<STR_LIT>' ] <EOL> for e in c . get ( '<STR_LIT>' , [ ] ) : <EOL> contacts . append ( ( name , e [ '<STR_LIT:address>' ] ) ) <EOL> return contacts , more </s>
