github.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. '''
  2. Module github.py contains a handful of methods
  3. for interacting with Github data.
  4. '''
  5. import requests
  6. import simplejson as json
  7. ########################
  8. # GITHUB API CONSTANTS #
  9. ########################
  10. API_BASE_URL = 'https://api.github.com/'
  11. API_USERS_URL = API_BASE_URL + 'users/DrkSephy' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23'
  12. # Endpoint to get statistics in a repository
  13. # https://api.github.com/repos/DrkSephy/WaterEmblem/stats/contributors
  14. # https://api.github.com/repos/:user/:repo/stats/contributors
  15. # 'https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23'
  16. def getUserData():
  17. req = requests.get(API_USERS_URL)
  18. jsonList = []
  19. jsonList.append(json.loads(req.content))
  20. parsedData = []
  21. userData = {}
  22. for data in jsonList:
  23. userData['name'] = data['name']
  24. userData['blog'] = data['blog']
  25. userData['email'] = data['email']
  26. userData['public_gists'] = data['public_gists']
  27. userData['public_repos'] = data['public_repos']
  28. userData['avatar_url'] = data['avatar_url']
  29. userData['followers'] = data['followers']
  30. userData['following'] = data['following']
  31. parsedData.append(userData)
  32. return parsedData
  33. def getUserRepositories():
  34. # Which page number of data are we looking at?
  35. pageNumber = 1
  36. # List of all our json
  37. jsonList = []
  38. # List of all repositories
  39. repositories = []
  40. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  41. # If it does, then we have iterated over all the data. Time to parse it.
  42. while True:
  43. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  44. jsonList.append(json.loads(req.content))
  45. if len(json.loads(req.content)) < 30:
  46. break
  47. elif len(json.loads(req.content)) >= 30:
  48. pageNumber += 1
  49. # Loop over our data and extract all of the repository names
  50. for data in jsonList:
  51. for datum in data:
  52. repositories.append(datum['name'])
  53. return repositories
  54. def getTopContributedRepositories(repos):
  55. jsonList = []
  56. for repo in repos:
  57. # print repo
  58. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  59. jsonList.append(json.loads(req.content))
  60. parsedData = []
  61. for item in jsonList:
  62. commits = {}
  63. for data in item:
  64. if data['author']['login'] == 'DrkSephy':
  65. commits['author'] = data['author']['login']
  66. commits['total'] = data['total']
  67. parsedData.append(commits)
  68. print parsedData
  69. '''
  70. data = {}
  71. if datum['author']['login'] == 'DrkSephy':
  72. data['author'] = datum['author']['login']
  73. data['total'] = datum['total']
  74. parsedData.append(data)
  75. '''
  76. #print len(jsonList)
  77. #print parsedData