github.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. import heapq
  8. ########################
  9. # GITHUB API CONSTANTS #
  10. ########################
  11. API_BASE_URL = 'https://api.github.com/'
  12. API_USERS_URL = API_BASE_URL + 'users/DrkSephy' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23'
  13. def getUserData():
  14. req = requests.get(API_USERS_URL)
  15. jsonList = []
  16. jsonList.append(json.loads(req.content))
  17. parsedData = []
  18. userData = {}
  19. for data in jsonList:
  20. userData['name'] = data['name']
  21. userData['blog'] = data['blog']
  22. userData['email'] = data['email']
  23. userData['public_gists'] = data['public_gists']
  24. userData['public_repos'] = data['public_repos']
  25. userData['avatar_url'] = data['avatar_url']
  26. userData['followers'] = data['followers']
  27. userData['following'] = data['following']
  28. parsedData.append(userData)
  29. return parsedData
  30. def getUserRepositories():
  31. # Which page number of data are we looking at?
  32. pageNumber = 1
  33. # List of all our json
  34. jsonList = []
  35. # List of all repositories
  36. repositories = []
  37. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  38. # If it does, then we have iterated over all the data. Time to parse it.
  39. while True:
  40. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  41. jsonList.append(json.loads(req.content))
  42. if len(json.loads(req.content)) < 30:
  43. break
  44. elif len(json.loads(req.content)) >= 30:
  45. pageNumber += 1
  46. # Loop over our data and extract all of the repository names
  47. for data in jsonList:
  48. for datum in data:
  49. repositories.append(datum['name'])
  50. return repositories
  51. def getTopContributedRepositories(repos):
  52. jsonList = []
  53. for repo in repos:
  54. # print repo
  55. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  56. jsonList.append(json.loads(req.content))
  57. parsedData = []
  58. # Keep track of which JSON set we are processing to get the repo name
  59. indexNumber = -1
  60. for item in jsonList:
  61. indexNumber += 1
  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. commits['repo_name'] = repos[indexNumber]
  68. parsedData.append(commits)
  69. return parsedData
  70. def filterCommits(data):
  71. maxCommits = []
  72. for i in range(1, 5):
  73. maxCommitedRepo = max(data, key=lambda x:x['total'])
  74. maxCommits.append(maxCommitedRepo)
  75. index = data.index(maxCommitedRepo)
  76. data.pop(index)
  77. return maxCommits