2

I want to get the github users and their location. I know there is Github apI(GET /users) which can provide me the list of user. Currently I am using PyGithub to access the github but seems this library hasn't implemented this API. Can anyone please suggest me how to get the github users and their location using any github API library?

EDIT1: I updated the code which is as following. But some how I am not able to get the email-Id and location.

import github3
from datetime import datetime

def main():
        g = github3.login(username="rakeshcusat", password="mypassword")
    for user in g.iter_emails():
        print user
    current_time = datetime.now()   
    fhandler = open("githubuser_"+current_time.strftime("%d-%m-%y-%H:%M:%S"), "w")

    for user in g.iter_all_users():
            fhandler.write(" user: {0}, email: {1}, location: {2}\n".format(user, user.email, user.location))
            #fhandler.flush()

    fhandler.close()        

if __name__ == "__main__":
    main()

Sample output

 user: andywatts, email: None, location: 
 user: mueller, email: None, location: 
 user: cp, email: None, location: 
 user: davea, email: None, location: 
 user: vrieskist, email: None, location: 
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
Rakesh
  • 3,987
  • 10
  • 43
  • 68

1 Answers1

2

The github3 library supports iteration over all users:

import github3

for user in github3.iter_all_users():
    user.refresh()
    print user.location

github3.iter_all_users() produces User objects.

You need the .refresh() call here, because the /users endpoint only returns a smaller subset of user information, and location isn't included in that. This requires another API request, so you may want to pace your script to avoid hitting the GitHub rate limits.

A future version of github3 (newer than 0.7.0) adds support to specify the page (batch) size to reduce the number of API requests you need to make; the GitHub API defaults to 30 results per page but lets you load up to 100 results per page instead:

for user in github3.iter_all_users(per_page=100):
    user.refresh()
    print user.location
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for your response. I used this library couple of times but I am getting this error 'github3.models.GitHubError: 403 API Rate Limit Exceeded for XYZ.XYZ.XYZ.XYZ'. Any idea how to fix this? – Rakesh Jun 15 '13 at 23:39
  • 1
    You are hitting the rate limit for your ip address, see http://developer.github.com/v3/#rate-limiting. You'll be allowed to make more requests if you log in first. – Martijn Pieters Jun 15 '13 at 23:41
  • 1
    Thanks for your valuable comments. Now I logged in and trying, its going on. Hope this time it will download the list. – Rakesh Jun 15 '13 at 23:56
  • one more followup question, how would I get the user email id. Will user.email work? – Rakesh Jun 16 '13 at 00:12
  • @Rakesh it will. Also, if you're willing to work from the develop branch, I just added the ability to pass an integer so you get more users per request and use your ratelimit much better. It's over on https://github.com/sigmavirus24/github3.py – Ian Stapleton Cordasco Jun 16 '13 at 00:14
  • No problem @Rakesh. I just realized I never actually added those. They need to be added to other methods too actually. Just need to go back and find them. – Ian Stapleton Cordasco Jun 16 '13 at 00:38
  • @sigmavirus24, I updated the code but somehow I am not able to get the location and email id. Am I doing something wrong? you can find the code in the above question. – Rakesh Jun 16 '13 at 03:00
  • 2
    @Rakesh for one thing, I forgot that they give you a short amount of the user's info. You can call the refresh method on that User object to get EVERYTHING, but that's another API call. Also, plenty of users don't have their location set. – Ian Stapleton Cordasco Jun 16 '13 at 04:09
  • @sigmavirus24 thanks, now it is working and I am able to see email and location. yes indeed some of the users don't have their location and email set. – Rakesh Jun 16 '13 at 04:38
  • @sigmavirus24: I've updated the answer to incorporate your updates. – Martijn Pieters Jun 16 '13 at 08:08
  • @MartijnPieters you sir are a gentleman and a scholar. If only I could upvote an answer more than once. :-P – Ian Stapleton Cordasco Jun 16 '13 at 17:51