github.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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=' \
  81. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  82. jsonList.append(json.loads(req.content))
  83. if len(json.loads(req.content)) < 30:
  84. break
  85. elif len(json.loads(req.content)) >= 30:
  86. pageNumber += 1
  87. for data in jsonList:
  88. for datum in data:
  89. repositories.append(datum['name'])
  90. return repositories
  91. def getForkedRepositories(clientID, clientSecret):
  92. '''
  93. Returns a list of all the public forked repositories
  94. owned by a User.
  95. Parameters:
  96. clientID: String
  97. - The clientID from registering this application
  98. on Github.
  99. clientSecret: String.
  100. - The clientSecret from registering this application
  101. on Github.
  102. Returns:
  103. forkedRepositories: List
  104. - A list containing all forked repository names
  105. belonging to a user.
  106. '''
  107. pageNumber = 1
  108. jsonList = []
  109. forkedRepositories = []
  110. while True:
  111. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  112. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  113. jsonList.append(json.loads(req.content))
  114. if len(json.loads(req.content)) < 30:
  115. break
  116. elif len(json.loads(req.content)) >= 30:
  117. pageNumber += 1
  118. forkedRepos = {}
  119. for data in jsonList:
  120. for datum in data:
  121. if datum['fork'] == True:
  122. forkedRepos['name'] = datum['name']
  123. forkedRepositories.append(forkedRepos)
  124. forkedRepos = {}
  125. return forkedRepositories
  126. def getTopContributedRepositories(repos, clientID, clientSecret):
  127. '''
  128. Returns a list containing the commit totals for all
  129. repositories owned by a user.
  130. Parameters:
  131. clientID: String
  132. - The clientID from registering this application
  133. on Github.
  134. clientSecret: String
  135. - The clientSecret from registering this application
  136. on Github.
  137. Returns:
  138. parsedData: Dictionary
  139. - A dictionary containing the following data:
  140. - commits['author']
  141. - The name of the committer
  142. - commits['total']
  143. - Total commit count for a user in a repository
  144. - commits['repo_name']
  145. - The name of the repository being tallied
  146. '''
  147. jsonList = []
  148. for repo in repos:
  149. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo \
  150. + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
  151. jsonList.append(json.loads(req.content))
  152. parsedData = []
  153. indexNumber = -1
  154. for item in jsonList:
  155. indexNumber += 1
  156. commits = {}
  157. for data in item:
  158. if data['author']['login'] == 'DrkSephy':
  159. commits['author'] = data['author']['login']
  160. commits['total'] = data['total']
  161. commits['repo_name'] = repos[indexNumber]
  162. parsedData.append(commits)
  163. return parsedData
  164. def filterCommits(data):
  165. '''
  166. Returns the top 10 committed repositories.
  167. Parameters:
  168. data: List
  169. - A list containing commit counts for all
  170. of a user's public repositories
  171. Returns:
  172. maxCommits: List
  173. - A list containing the top ten repositories
  174. with the maximum number of commits by a user
  175. '''
  176. maxCommits = []
  177. i = 0
  178. while i < 10:
  179. maxCommitedRepo = max(data, key=lambda x: x['total'])
  180. maxCommits.append(maxCommitedRepo)
  181. index = data.index(maxCommitedRepo)
  182. data.pop(index)
  183. i += 1
  184. return maxCommits
  185. def getStarGazerCount(clientID, clientSecret):
  186. '''
  187. Returns a list number of stargazers for each
  188. of a user's public repositories.
  189. Parameters:
  190. clientID: String
  191. - The clientID from registering this application
  192. on Github.
  193. clientSecret: String
  194. - The clientSecret from registering this application
  195. on Github.
  196. Returns:
  197. stargazers: Dictionary
  198. - A dictionary containing the following data:
  199. - starData['stargazers_count']
  200. - The number of stargazers for a given repository
  201. - starData['name']
  202. - The name of the repository being observed
  203. '''
  204. pageNumber = 1
  205. jsonList = []
  206. stargazers = []
  207. while True:
  208. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  209. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  210. jsonList.append(json.loads(req.content))
  211. if len(json.loads(req.content)) < 30:
  212. break
  213. elif len(json.loads(req.content)) >= 30:
  214. pageNumber += 1
  215. for data in jsonList:
  216. for datum in data:
  217. starData = {}
  218. starData['stargazers_count'] = datum['stargazers_count']
  219. starData['name'] = datum['name']
  220. stargazers.append(starData)
  221. return stargazers
  222. def filterStarGazerCount(data):
  223. '''
  224. Returns the top 10 stargazed repositories.
  225. Parameters:
  226. data: List
  227. - A list containing stargazer counts for all
  228. of a user's public repositories
  229. Returns:
  230. maxStars: List
  231. - A list containing the top ten repositories
  232. with the maximum number of stargazers
  233. '''
  234. maxStars = []
  235. i = 0
  236. while i < 10:
  237. maxStarGazers = max(data, key=lambda x: x['stargazers_count'])
  238. maxStars.append(maxStarGazers)
  239. index = data.index(maxStarGazers)
  240. data.pop(index)
  241. i += 1
  242. return maxStars