Skip to content

Instantly share code, notes, and snippets.

@dlenski
Last active April 7, 2016 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlenski/742fb573cc6e647c3c36b81b2d25b209 to your computer and use it in GitHub Desktop.
Save dlenski/742fb573cc6e647c3c36b81b2d25b209 to your computer and use it in GitHub Desktop.
"""Example of using hangups to lookup contacts by id."""
import sys
import asyncio
import hangups
# Path where OAuth refresh token is saved, allowing hangups to remember your
# credentials.
REFRESH_TOKEN_PATH = 'refresh_token.txt'
def main():
"""Main entry point."""
# Obtain hangups authentication cookies, prompting for username and
# password from standard in if necessary.
cookies = hangups.auth.get_auth_stdin(REFRESH_TOKEN_PATH)
# Instantiate hangups Client instance.
client = hangups.Client(cookies)
# Add an observer to the on_connect event to run the
# retrieve_suggested_contacts coroutine when hangups has finished
# connecting.
client.on_connect.add_observer(lambda: asyncio.async(
lookup_entities(client, sys.argv[1:])
))
# Start an asyncio event loop by running Client.connect. This will not
# return until Client.disconnect is called, or hangups becomes
# disconnected.
loop = asyncio.get_event_loop()
loop.run_until_complete(client.connect())
@asyncio.coroutine
def lookup_entities(client, identifiers):
"""Search for entities by phone number, email, or gaia_id."""
# Instantiate a GetSuggestedEntitiesRequest Protocol Buffer message
# describing the request.
lookup_dicts = []
for id in identifiers:
if id.startswith('+'): k='phone'
elif '@' in id: k='email'
else: k='gaia_id'
lookup_dicts.append({k:id,'unknown':True})
lookup_dicts.append({k:id,'unknown':False})
request = hangups.hangouts_pb2.GetEntityByIdRequest(
request_header=client.get_request_header(),
batch_lookup_spec = [ hangups.hangouts_pb2.EntityLookupSpec(**d) for d in lookup_dicts ],
)
try:
# Make the request to the Hangouts API.
res = yield from client.get_entity_by_id(request)
# Print the list of entities in the response.
for lookup_dict, entity in zip(lookup_dicts, res.entity):
print('Searched for {}:'.format(lookup_dict))
print('{} ({})\n\tEmail addresses: {}\n\tPhone numbers: {}'.format(entity.properties.display_name, entity.id.gaia_id, entity.properties.email, entity.properties.phone))
print(entity)
finally:
# Disconnect the hangups Client to make client.connect return.
yield from client.disconnect()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment