Created
December 19, 2015 20:42
-
-
Save kmonsoor/1d42b44b5a4f84ad973f to your computer and use it in GitHub Desktop.
Find a Github user's email address
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
#!/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