github.py 2.7 KB

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