Skip to content

Instantly share code, notes, and snippets.

@kmonsoor
Created December 19, 2015 20:42
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 kmonsoor/1d42b44b5a4f84ad973f to your computer and use it in GitHub Desktop.
Save kmonsoor/1d42b44b5a4f84ad973f to your computer and use it in GitHub Desktop.
Find a Github user's email address
#!/usr/bin/python
__author__ = 'Khaled Monsoor <k@kmonsoor.com>'
__license__ = 'The MIT License: <http://kmonsoor.mit-license.org/>'
import requests
import json
def github_username_to_email(username):
'''
Given a valid Github username, this function returns the users email address.
If `username` is invalid on Github.com, it will raise ValueError.
If `username` is valid but the user's email-address is not public,
an empty string('') will be returned.
'''
api_url = 'https://api.github.com/users/'
hit_url = api_url + str(username)
response = requests.get(hit_url)
if response.status_code != 200:
raise ValueError('Invalid username: {}'.format(username))
user = json.loads(response.content)
return user['email'] if user['email'] else ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment