Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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