github.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/users/DrkSephy'
  11. #API_USERS_URL = API_BASE_URL + 'users/DrkSephy' + '?client_id=2404a1e21aebd902f6db' + '&client_secret=3da44769d4b7c9465fa4c812669148a163607c23'
  12. def getUserData(clientID, clientSecret):
  13. '''Get generic Github User data.'''
  14. url = API_BASE_URL + clientID + clientSecret
  15. req = requests.get(url)
  16. jsonList = []
  17. jsonList.append(json.loads(req.content))
  18. parsedData = []
  19. userData = {}
  20. for data in jsonList:
  21. userData['name'] = data['name']
  22. userData['blog'] = data['blog']
  23. userData['email'] = data['email']
  24. userData['public_gists'] = data['public_gists']
  25. userData['public_repos'] = data['public_repos']
  26. userData['avatar_url'] = data['avatar_url']
  27. userData['followers'] = data['followers']
  28. userData['following'] = data['following']
  29. parsedData.append(userData)
  30. return parsedData
  31. def getUserRepositories(clientID, clientSecret):
  32. '''Get a list of all repositories owned by a User.'''
  33. # Which page number of data are we looking at?
  34. pageNumber = 1
  35. # List of all our json
  36. jsonList = []
  37. # List of all repositories
  38. repositories = []
  39. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  40. # If it does, then we have iterated over all the data. Time to parse it.
  41. while True:
  42. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  43. jsonList.append(json.loads(req.content))
  44. if len(json.loads(req.content)) < 30:
  45. break
  46. elif len(json.loads(req.content)) >= 30:
  47. pageNumber += 1
  48. # Loop over our data and extract all of the repository names
  49. for data in jsonList:
  50. for datum in data:
  51. repositories.append(datum['name'])
  52. return repositories
  53. def getForkedRepositories(clientID, clientSecret):
  54. '''Get a list of all forked repositories by a user.'''
  55. # Which page number of data are we looking at?
  56. pageNumber = 1
  57. # List of all our json
  58. jsonList = []
  59. # List of all repositories
  60. forkedRepositories = []
  61. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  62. # If it does, then we have iterated over all the data. Time to parse it.
  63. while True:
  64. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  65. jsonList.append(json.loads(req.content))
  66. if len(json.loads(req.content)) < 30:
  67. break
  68. elif len(json.loads(req.content)) >= 30:
  69. pageNumber += 1
  70. # Loop over our data and extract all of the repository names
  71. forkedRepos = {}
  72. for data in jsonList:
  73. for datum in data:
  74. if datum['fork'] == True:
  75. #print datum['name']
  76. forkedRepos['name'] = datum['name']
  77. forkedRepositories.append(forkedRepos)
  78. forkedRepos = {}
  79. return forkedRepositories
  80. def getTopContributedRepositories(repos, clientID, clientSecret):
  81. '''Get a list of all commits for each repository owned.'''
  82. jsonList = []
  83. for repo in repos:
  84. # print repo
  85. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + clientID + clientSecret)
  86. jsonList.append(json.loads(req.content))
  87. parsedData = []
  88. # Keep track of which JSON set we are processing to get the repo name
  89. indexNumber = -1
  90. for item in jsonList:
  91. indexNumber += 1
  92. commits = {}
  93. for data in item:
  94. if data['author']['login'] == 'DrkSephy':
  95. commits['author'] = data['author']['login']
  96. commits['total'] = data['total']
  97. commits['repo_name'] = repos[indexNumber]
  98. parsedData.append(commits)
  99. return parsedData
  100. def filterCommits(data):
  101. '''Returns the top 10 committed repositories.'''
  102. maxCommits = []
  103. for i in range(1, 10):
  104. maxCommitedRepo = max(data, key=lambda x:x['total'])
  105. maxCommits.append(maxCommitedRepo)
  106. index = data.index(maxCommitedRepo)
  107. data.pop(index)
  108. return maxCommits
  109. def getStarGazerCount(clientID, clientSecret):
  110. '''Get Stargazer counts for all repositories.'''
  111. # Which page number of data are we looking at?
  112. pageNumber = 1
  113. # List of all our json
  114. jsonList = []
  115. # List of all repositories
  116. stargazers = []
  117. # IDEA: Repeatedly loop over urls and check if the content has less than 30 entries.
  118. # If it does, then we have iterated over all the data. Time to parse it.
  119. while True:
  120. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&client_id=2404a1e21aebd902f6db&client_secret=3da44769d4b7c9465fa4c812669148a163607c23')
  121. jsonList.append(json.loads(req.content))
  122. if len(json.loads(req.content)) < 30:
  123. break
  124. elif len(json.loads(req.content)) >= 30:
  125. pageNumber += 1
  126. # Loop over our data and extract all of the repository names
  127. for data in jsonList:
  128. for datum in data:
  129. starData = {}
  130. starData['stargazers_count'] = datum['stargazers_count']
  131. starData['name'] = datum['name']
  132. stargazers.append(starData)
  133. return stargazers
  134. def filterStarGazerCount(data):
  135. '''Return top 10 starred repositories.'''
  136. maxStars= []
  137. for i in range(1, 10):
  138. maxStarGazers = max(data, key=lambda x:x['stargazers_count'])
  139. maxStars.append(maxStarGazers)
  140. index = data.index(maxStarGazers)
  141. data.pop(index)
  142. return maxStars