Last active
June 3, 2022 20:36
-
-
Save sbassett29/f260244a4b1eb69dc123ed2227b5f100 to your computer and use it in GitHub Desktop.
Some python to access Profiles from Namely's API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" Search for people in a Namely API | |
Doc: https://developers.namely.com/docs/namely-api/ | |
Author: sbassett29 | |
License: CC0 | |
""" | |
import json | |
import math | |
import os | |
import requests | |
""" vars """ | |
namely_api_base_url = os.getenv('NAMELY_API_BASE_URL') | |
if namely_api_base_url is None: | |
raise ValueError("Namely API base URL not set within environment!") | |
""" | |
likely wants: _namely_session3, _session_id, company. | |
or a PAT. PATs are nice. | |
""" | |
namely_api_cookies = os.getenv('NAMELY_API_COOKIES') | |
if namely_api_cookies is None: | |
raise ValueError("Namely API cookies not set within environment!") | |
else: | |
cookies = dict(x.split("=") for x in namely_api_cookies.split(",")) | |
namely_api_endpoint = 'profiles.json' | |
namely_api_filter = 'filter[user_status]=active' | |
namely_api_per_page = 50 | |
namely_groups = [ | |
'Technology', | |
'Product', | |
'Legal' | |
] | |
namely_url = ''.join([namely_api_base_url, | |
namely_api_endpoint, | |
'?', | |
namely_api_filter, | |
'&', | |
str(namely_api_per_page)]) | |
""" make request """ | |
resp_json = '' | |
resp_json_profiles = '' | |
resp_json = requests.get(namely_url, cookies=cookies).json() | |
total_count = resp_json['meta']['total_count'] | |
total_loop = math.ceil(total_count / namely_api_per_page) | |
resp_json_profiles = resp_json['profiles'] | |
for i in range(2, total_loop): | |
namely_url = ''.join([namely_url, '&page=', str(i)]) | |
d = requests.get(namely_url, cookies=cookies).json()['profiles'] | |
resp_json_profiles = resp_json_profiles + d | |
""" process all request(s) data """ | |
person_data = [] | |
for person in resp_json_profiles: | |
try: | |
for group_data in person['links']['groups']: | |
if group_data['name'] in namely_groups: | |
person_data.append(person['email']) | |
except Exception: | |
print(json.dumps(person, indent=4, sort_keys=True)) | |
print("\n") | |
""" print person data """ | |
person_data.sort() | |
print("\n".join(person_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment