GitHub users search
This file contains 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
""" A simple script to search for GitHub users. """ | |
from __future__ import print_function | |
import json | |
from os.path import abspath, dirname, exists, join | |
import requests | |
from requests.utils import quote | |
HERE = dirname(abspath(__file__)) | |
HEADERS = { 'Content-Type': 'application/vnd.github.v3+json' } | |
SEARCH_URL = 'https://api.github.com/search/users' | |
USERS_URL = 'https://api.github.com/users/%s' | |
def search_users(query): | |
next_url = '%s?q=%s&per_page=100' % (SEARCH_URL, quote(query, safe=':> ')) | |
users = [] | |
while next_url is not None: | |
print('Searching %s...' % next_url[len(SEARCH_URL):]) | |
response = github_request(next_url) | |
users.extend(response.json().get('items', [])) | |
next_url = response.links.get('next', {}).get('url') | |
return users | |
def get_user_email(username): | |
url = USERS_URL % username | |
response = github_request(url) | |
return response.json().get('email') | |
def get_user_name(username): | |
url = USERS_URL % username | |
response = github_request(url) | |
return response.json().get('name') | |
def github_request(url, payload=None): | |
if payload is None: | |
payload = {} | |
global USERNAME | |
global PASSWORD | |
global URL_QUERY_CACHE | |
if url in URL_QUERY_CACHE: | |
response = URL_QUERY_CACHE[url] | |
else: | |
response = requests.get( | |
url, | |
params={}, | |
auth=(USERNAME, PASSWORD), | |
headers=HEADERS | |
) | |
URL_QUERY_CACHE[url] = response | |
return response | |
def aggregate_info(): | |
cache_file = join(HERE, 'cache.json') | |
if exists(cache_file): | |
with open(cache_file) as f: | |
return json.load(f) | |
users = search_users(QUERY) | |
print('Found total %d users' % len(users)) | |
info = {} | |
for i, user in enumerate(users): | |
login = user.get('login') | |
print('Fetching info for [%d] %s ...' % (i, login)) | |
info[login] = user | |
user['email'] = get_user_email(login) | |
user['name'] = get_user_name(login) | |
with open(cache_file, 'w') as f: | |
json.dump(info, f) | |
return info | |
if __name__ == '__main__': | |
from getpass import getpass | |
USERNAME = 'punchagan' | |
PASSWORD = getpass() | |
QUERY = 'type:user repos:>5 location:Hyderabad' | |
if 'URL_QUERY_CACHE' not in globals(): | |
URL_QUERY_CACHE = {} | |
info = aggregate_info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment