def fetch_contacts(): email = request.args.get('email') access_token = request.args.get('access_token') user = User.get_by_id(email) headers = {'Authorization': 'Bearer {}'.format(access_token)} req_uri = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=25000&v=3.0' r = urlfetch.fetch(req_uri, headers=headers, method='GET') data = json.loads(r.content) contact_list = data.get('feed', {}).get('entry', []) # List ndb.delete_multi( Contacts.query(Contacts.owner == email).fetch(keys_only=True)) for contact in contact_list: if ('gd$phoneNumber' not in contact): continue name = contact.get('gd$name', {}) \ .get('gd$fullName', {}) \ .get('$t', '') numbers = [ number.get('$t', '') for number in contact.get('gd$phoneNumber', []) ] Contacts.add_contact(email, name, numbers) user.import_status = "imported" user.put() return "", 200
def list_contacts(): session_id = request.cookies.get('sessionID') if not session_id or session_id == '': return jsonify(dict(success=False, error='unauthorized')) user = Session.get_by_id(session_id) if not user: return jsonify(dict(success=False, error='unauthorized')) email = user.email # logging.info('email : {}'.format(email)) cursor = request.args.get('cursor') if not cursor: cursor = None cursor = Cursor(urlsafe=cursor) query = Contacts.query(Contacts.owner == email).order(Contacts.name_lc) contacts, next_cursor, more = query.fetch_page(10, start_cursor=cursor) # logging.info('cursor: {} more: {}'.format(next_cursor, more)) if next_cursor: cursor = next_cursor.urlsafe() else: cursor = None data = [contact.to_dict() for contact in contacts] return jsonify({ 'cursor': cursor, 'more': more, 'contacts': data, 'success': True })