github.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. for i in range(1, 10):
  178. maxCommitedRepo = max(data, key=lambda x:x['total'])
  179. maxCommits.append(maxCommitedRepo)
  180. index = data.index(maxCommitedRepo)
  181. data.pop(index)
  182. return maxCommits
  183. def getStarGazerCount(clientID, clientSecret):
  184. '''
  185. Returns a list number of stargazers for each
  186. of a user's public repositories.
  187. Parameters:
  188. clientID: String
  189. - The clientID from registering this application
  190. on Github.
  191. clientSecret: String
  192. - The clientSecret from registering this application
  193. on Github.
  194. Returns:
  195. stargazers: Dictionary
  196. - A dictionary containing the following data:
  197. - starData['stargazers_count']
  198. - The number of stargazers for a given repository
  199. - starData['name']
  200. - The name of the repository being observed
  201. '''
  202. pageNumber = 1
  203. jsonList = []
  204. stargazers = []
  205. while True:
  206. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  207. jsonList.append(json.loads(req.content))
  208. if len(json.loads(req.content)) < 30:
  209. break
  210. elif len(json.loads(req.content)) >= 30:
  211. pageNumber += 1
  212. for data in jsonList:
  213. for datum in data:
  214. starData = {}
  215. starData['stargazers_count'] = datum['stargazers_count']
  216. starData['name'] = datum['name']
  217. stargazers.append(starData)
  218. return stargazers
  219. def filterStarGazerCount(data):
  220. '''
  221. Returns the top 10 stargazed repositories.
  222. Parameters:
  223. data: List
  224. - A list containing stargazer counts for all
  225. of a user's public repositories
  226. Returns:
  227. maxStars: List
  228. - A list containing the top ten repositories
  229. with the maximum number of stargazers
  230. '''
  231. maxStars= []
  232. for i in range(1, 10):
  233. maxStarGazers = max(data, key=lambda x:x['stargazers_count'])
  234. maxStars.append(maxStarGazers)
  235. index = data.index(maxStarGazers)
  236. data.pop(index)
  237. return maxStars