Last active
August 15, 2024 17:21
-
-
Save asfaltboy/6163129 to your computer and use it in GitHub Desktop.
Search for possible hire candidates from github profiles
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
| """ | |
| Welcome to github candidate finder! | |
| Note: Current github api hardcode limit for searches is 1000 results | |
| Valid arguments are either: | |
| a. set the CREDS tuple in the code or, | |
| b. pass one of the following: | |
| * A github OAUTH_TOKEN | |
| * A valid set of credentials (<Username>, <Password>) with which we'll | |
| obtain a token | |
| """ | |
| import sys | |
| from requests.auth import HTTPBasicAuth | |
| import requests | |
| import ujson as json | |
| PROVIDER_PREFIX = 'https://api.github.com' | |
| SEARCH_ONE_URL = PROVIDER_PREFIX + '/users/' | |
| AUTH_URL = PROVIDER_PREFIX + '/authorizations' | |
| SEARCH_URL = PROVIDER_PREFIX + '/search/users' | |
| GENERIC_HEADER = {"User-Agent": 'Find Some Candidates'} | |
| PREVIEW_HEADER = {"Accept": 'application/vnd.github.preview'} | |
| PREVIEW_HEADER.update(GENERIC_HEADER) | |
| SEARCH_QUERY = 'repos:>=2 location:israel language:python' | |
| # enter github user/pass here for hardcoded basic auth | |
| # CREDS = ('user', 'password') | |
| def get_access_token(username, password): | |
| auth_payload = { | |
| 'client_id': '<add client id>', | |
| 'client_secret': '<add client secret>', | |
| 'scopes': 'user' | |
| } | |
| auth_res = requests.post( | |
| AUTH_URL, data=json.dumps(auth_payload), | |
| auth=HTTPBasicAuth(username, password), | |
| headers=GENERIC_HEADER) | |
| if auth_res.status_code in [200, 201]: | |
| return auth_res.json()['token'] | |
| return None | |
| def search_for_users(query, token): | |
| payload = { | |
| 'q': query, | |
| 'access_token': token, | |
| 'sort': 'followers', | |
| 'per_page': 100, | |
| } | |
| first_res = requests.get(SEARCH_URL, params=payload, | |
| headers=PREVIEW_HEADER) | |
| results = first_res.json() | |
| users = results.get('items', []) | |
| total_count = results.get('total_count', 0) | |
| print "found total of {results} results matching the query {query}".format( | |
| results=total_count, query=query) | |
| if total_count > 100: | |
| pages = results['total_count'] // 100 | |
| for i in range(2, int(pages + 2)): | |
| payload['page'] = i | |
| res = requests.get(SEARCH_URL, params=payload, | |
| headers=PREVIEW_HEADER) | |
| results = res.json() | |
| users.extend(results.get('items', [])) | |
| return users | |
| def get_user_details(user, token): | |
| payload = { | |
| 'access_token': token, | |
| } | |
| res = requests.get(SEARCH_ONE_URL + user['login'], params=payload) | |
| if res.status_code == 200: | |
| user['details'] = res.json() | |
| if __name__ == '__main__': | |
| if 'CREDS' not in globals(): | |
| if len(sys.argv) > 1: | |
| if len(sys.argv) == 2: | |
| # use token for authentication | |
| access_token = sys.argv[1] | |
| if len(sys.argv) > 2: | |
| access_token = get_access_token(*sys.argv[1:3]) | |
| else: | |
| sys.exit(__doc__) | |
| else: | |
| access_token = get_access_token(*globals()['CREDS']) | |
| users = search_for_users(SEARCH_QUERY, access_token) | |
| for user in users: | |
| get_user_details(user, access_token) | |
| f = open('candidates_dump.json', 'w') | |
| f.write(json.dumps(users)) | |
| f.close() | |
| hirable = [u for u in users if u.get('details', {}).get('hireable', False)] | |
| f = open('hirable_dump.json', 'w') | |
| f.write(json.dumps(hirable)) | |
| f.close() | |
| print "Results were written to candidates_dump.json and hirable_dump.json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment