github.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. '''
  2. github.py contains a handful of methods for interacting
  3. with Github data and returning the responses as JSON.
  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. def getUserData(clientID, clientSecret):
  12. '''
  13. Returns data found on a Github User's public profile.
  14. This includes information such as number of followers,
  15. e-mail, number of repositories and more.
  16. Parameters:
  17. clientID: String
  18. - The clientID from registering this application
  19. on Github.
  20. clientSecret: String
  21. - The clientSecret from registering this application
  22. on Github.
  23. Returns:
  24. parsedData: Dictionary
  25. - A dictionary containing the following data:
  26. - userData['name']
  27. - The user's public name on Github
  28. - userData['blog']
  29. - Link to the user's blog on Github
  30. - userData['email']
  31. - The user's public e-mail on Github
  32. - userData['public_gists']
  33. - The number of the user's public gists
  34. - userData['public_repos']
  35. - The number of public repositories owned
  36. - userData['avatar_url']
  37. - Link to user's public avatar
  38. - userData['followers']
  39. - Number of followers
  40. - userData['following']
  41. - Number of users being followed
  42. '''
  43. url = API_BASE_URL + '?' + clientID + '&' + clientSecret
  44. req = requests.get(url)
  45. jsonList = []
  46. jsonList.append(json.loads(req.content))
  47. parsedData = []
  48. userData = {}
  49. for data in jsonList:
  50. userData['name'] = data['name']
  51. userData['blog'] = data['blog']
  52. userData['email'] = data['email']
  53. userData['public_gists'] = data['public_gists']
  54. userData['public_repos'] = data['public_repos']
  55. userData['avatar_url'] = data['avatar_url']
  56. userData['followers'] = data['followers']
  57. userData['following'] = data['following']
  58. parsedData.append(userData)
  59. return parsedData
  60. def getUserRepositories(clientID, clientSecret):
  61. '''
  62. Returns a list of all the public repositories
  63. owned by a User.
  64. Parameters:
  65. clientID: String
  66. - The clientID from registering this application
  67. on Github.
  68. clientSecret: String.
  69. - The clientSecret from registering this application
  70. on Github.
  71. Returns:
  72. repositories: List
  73. - A list containing all public repository names
  74. belonging to a user.
  75. '''
  76. pageNumber = 1
  77. jsonList = []
  78. repositories = []
  79. while True:
  80. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  81. jsonList.append(json.loads(req.content))
  82. if len(json.loads(req.content)) < 30:
  83. break
  84. elif len(json.loads(req.content)) >= 30:
  85. pageNumber += 1
  86. for data in jsonList:
  87. for datum in data:
  88. repositories.append(datum['name'])
  89. return repositories
  90. def getForkedRepositories(clientID, clientSecret):
  91. '''
  92. Returns a list of all the public forked repositories
  93. owned by a User.
  94. Parameters:
  95. clientID: String
  96. - The clientID from registering this application
  97. on Github.
  98. clientSecret: String.
  99. - The clientSecret from registering this application
  100. on Github.
  101. Returns:
  102. forkedRepositories: List
  103. - A list containing all forked repository names
  104. belonging to a user.
  105. '''
  106. pageNumber = 1
  107. jsonList = []
  108. forkedRepositories = []
  109. while True:
  110. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  111. jsonList.append(json.loads(req.content))
  112. if len(json.loads(req.content)) < 30:
  113. break
  114. elif len(json.loads(req.content)) >= 30:
  115. pageNumber += 1
  116. forkedRepos = {}
  117. for data in jsonList:
  118. for datum in data:
  119. if datum['fork'] == True:
  120. forkedRepos['name'] = datum['name']
  121. forkedRepositories.append(forkedRepos)
  122. forkedRepos = {}
  123. return forkedRepositories
  124. def getTopContributedRepositories(repos, clientID, clientSecret):
  125. '''
  126. Returns a list containing the commit totals for all
  127. repositories owned by a user.
  128. Parameters:
  129. clientID: String
  130. - The clientID from registering this application
  131. on Github.
  132. clientSecret: String
  133. - The clientSecret from registering this application
  134. on Github.
  135. Returns:
  136. parsedData: Dictionary
  137. - A dictionary containing the following data:
  138. - commits['author']
  139. - The name of the committer
  140. - commits['total']
  141. - Total commit count for a user in a repository
  142. - commits['repo_name']
  143. - The name of the repository being tallied
  144. '''
  145. jsonList = []
  146. for repo in repos:
  147. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
  148. jsonList.append(json.loads(req.content))
  149. parsedData = []
  150. indexNumber = -1
  151. for item in jsonList:
  152. indexNumber += 1
  153. commits = {}
  154. for data in item:
  155. if data['author']['login'] == 'DrkSephy':
  156. commits['author'] = data['author']['login']
  157. commits['total'] = data['total']
  158. commits['repo_name'] = repos[indexNumber]
  159. parsedData.append(commits)
  160. return parsedData
  161. def filterCommits(data):
  162. '''
  163. Returns the top 10 committed repositories.
  164. Parameters:
  165. data: List
  166. - A list containing commit counts for all
  167. of a user's public repositories
  168. Returns:
  169. maxCommits: List
  170. - A list containing the top ten repositories
  171. with the maximum number of commits by a user
  172. '''
  173. maxCommits = []
  174. for i in range(1, 10):
  175. maxCommitedRepo = max(data, key=lambda x:x['total'])
  176. maxCommits.append(maxCommitedRepo)
  177. index = data.index(maxCommitedRepo)
  178. data.pop(index)
  179. return maxCommits
  180. def getStarGazerCount(clientID, clientSecret):
  181. '''
  182. Returns a list number of stargazers for each
  183. of a user's public repositories.
  184. Parameters:
  185. clientID: String
  186. - The clientID from registering this application
  187. on Github.
  188. clientSecret: String
  189. - The clientSecret from registering this application
  190. on Github.
  191. Returns:
  192. stargazers: Dictionary
  193. - A dictionary containing the following data:
  194. - starData['stargazers_count']
  195. - The number of stargazers for a given repository
  196. - starData['name']
  197. - The name of the repository being observed
  198. '''
  199. pageNumber = 1
  200. jsonList = []
  201. stargazers = []
  202. while True:
  203. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  204. jsonList.append(json.loads(req.content))
  205. if len(json.loads(req.content)) < 30:
  206. break
  207. elif len(json.loads(req.content)) >= 30:
  208. pageNumber += 1
  209. for data in jsonList:
  210. for datum in data:
  211. starData = {}
  212. starData['stargazers_count'] = datum['stargazers_count']
  213. starData['name'] = datum['name']
  214. stargazers.append(starData)
  215. return stargazers
  216. def filterStarGazerCount(data):
  217. '''
  218. Returns the top 10 stargazed repositories.
  219. Parameters:
  220. data: List
  221. - A list containing stargazer counts for all
  222. of a user's public repositories
  223. Returns:
  224. maxStars: List
  225. - A list containing the top ten repositories
  226. with the maximum number of stargazers
  227. '''
  228. maxStars= []
  229. for i in range(1, 10):
  230. maxStarGazers = max(data, key=lambda x:x['stargazers_count'])
  231. maxStars.append(maxStarGazers)
  232. index = data.index(maxStarGazers)
  233. data.pop(index)
  234. return maxStars