github.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. '''Get generic Github User data.'''
  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. '''Get a list of all repositories owned by a User.'''
  32. # Which page number of data are we looking at?
  33. pageNumber = 1
  34. # List of all our json
  35. jsonList = []
  36. # List of all repositories
  37. repositories = []
  38. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  39. # If it does, then we have iterated over all the data. Time to parse it.
  40. while True:
  41. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  42. jsonList.append(json.loads(req.content))
  43. if len(json.loads(req.content)) < 30:
  44. break
  45. elif len(json.loads(req.content)) >= 30:
  46. pageNumber += 1
  47. # Loop over our data and extract all of the repository names
  48. for data in jsonList:
  49. for datum in data:
  50. repositories.append(datum['name'])
  51. return repositories
  52. def getTopContributedRepositories(repos):
  53. '''Get a list of all commits for each repository owned.'''
  54. jsonList = []
  55. for repo in repos:
  56. # print repo
  57. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  58. jsonList.append(json.loads(req.content))
  59. parsedData = []
  60. # Keep track of which JSON set we are processing to get the repo name
  61. indexNumber = -1
  62. for item in jsonList:
  63. indexNumber += 1
  64. commits = {}
  65. for data in item:
  66. if data['author']['login'] == 'DrkSephy':
  67. commits['author'] = data['author']['login']
  68. commits['total'] = data['total']
  69. commits['repo_name'] = repos[indexNumber]
  70. parsedData.append(commits)
  71. return parsedData
  72. def filterCommits(data):
  73. '''Returns the top 10 committed repositories.'''
  74. maxCommits = []
  75. for i in range(1, 10):
  76. maxCommitedRepo = max(data, key=lambda x:x['total'])
  77. maxCommits.append(maxCommitedRepo)
  78. index = data.index(maxCommitedRepo)
  79. data.pop(index)
  80. return maxCommits
  81. def getStarGazerCount():
  82. '''Get Stargazer counts for all repositories.'''
  83. # Which page number of data are we looking at?
  84. pageNumber = 1
  85. # List of all our json
  86. jsonList = []
  87. # List of all repositories
  88. stargazers = []
  89. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  90. # If it does, then we have iterated over all the data. Time to parse it.
  91. while True:
  92. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  93. jsonList.append(json.loads(req.content))
  94. if len(json.loads(req.content)) < 30:
  95. break
  96. elif len(json.loads(req.content)) >= 30:
  97. pageNumber += 1
  98. # Loop over our data and extract all of the repository names
  99. for data in jsonList:
  100. for datum in data:
  101. starData = {}
  102. starData['stargazers_count'] = datum['stargazers_count']
  103. starData['name'] = datum['name']
  104. stargazers.append(starData)
  105. return stargazers
  106. def filterStarGazerCount(data):
  107. '''Return top 10 starred repositories.'''
  108. maxStars= []
  109. for i in range(1, 10):
  110. maxStarGazers = max(data, key=lambda x:x['stargazers_count'])
  111. maxStars.append(maxStarGazers)
  112. index = data.index(maxStarGazers)
  113. data.pop(index)
  114. return maxStars