github.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. import urllib, urllib2, urlparse
  8. ########################
  9. # GITHUB API CONSTANTS #
  10. ########################
  11. API_BASE_URL = 'https://api.github.com/users/DrkSephy'
  12. AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'
  13. ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
  14. class GithubOauthClient(object):
  15. access_token = None
  16. token_type = None
  17. def __init__(self, client_id, client_secret):
  18. self.client_id = client_id
  19. self.client_secret = client_secret
  20. def get_authorize_url(self):
  21. auth_setting = {'client_id': self.client_id,
  22. 'redirect_uri': 'http://127.0.0.1:8000/hackathon/',
  23. 'scope': 'user, public_repo, repo, repo_deployment, notifications, gist'}
  24. params = urllib.urlencode(auth_setting)
  25. auth_link = AUTHORIZE_URL + '?' + params
  26. print auth_link
  27. return auth_link
  28. def get_access_token(self, code):
  29. settings = {'client_id': self.client_id,
  30. 'client_secret': self.client_secret,
  31. 'code': code,
  32. 'redirect_uri': 'http://127.0.0.1:8000/hackathon/'}
  33. params = urllib.urlencode(settings)
  34. access_link = ACCESS_TOKEN_URL + '?' + params
  35. req = requests.get(access_link)
  36. if int(req.status_code) != 200:
  37. raise Exception('Invalid response %s' %req.status_code)
  38. content = urlparse.parse_qs(req.content)
  39. self.access_token = content['access_token'][0]
  40. self.token_type = content['token_type'][0]
  41. print self.access_token
  42. def getUserData(clientID, clientSecret):
  43. '''
  44. Returns data found on a Github User's public profile.
  45. This includes information such as number of followers,
  46. e-mail, number of repositories and more.
  47. Parameters:
  48. clientID: String
  49. - The clientID from registering this application
  50. on Github.
  51. clientSecret: String
  52. - The clientSecret from registering this application
  53. on Github.
  54. Returns:
  55. parsedData: Dictionary
  56. - A dictionary containing the following data:
  57. - userData['name']
  58. - The user's public name on Github
  59. - userData['blog']
  60. - Link to the user's blog on Github
  61. - userData['email']
  62. - The user's public e-mail on Github
  63. - userData['public_gists']
  64. - The number of the user's public gists
  65. - userData['public_repos']
  66. - The number of public repositories owned
  67. - userData['avatar_url']
  68. - Link to user's public avatar
  69. - userData['followers']
  70. - Number of followers
  71. - userData['following']
  72. - Number of users being followed
  73. '''
  74. url = API_BASE_URL + '?' + clientID + '&' + clientSecret
  75. req = requests.get(url)
  76. jsonList = []
  77. jsonList.append(json.loads(req.content))
  78. parsedData = []
  79. userData = {}
  80. for data in jsonList:
  81. userData['name'] = data['name']
  82. userData['blog'] = data['blog']
  83. userData['email'] = data['email']
  84. userData['public_gists'] = data['public_gists']
  85. userData['public_repos'] = data['public_repos']
  86. userData['avatar_url'] = data['avatar_url']
  87. userData['followers'] = data['followers']
  88. userData['following'] = data['following']
  89. parsedData.append(userData)
  90. return parsedData
  91. def getUserRepositories(clientID, clientSecret):
  92. '''
  93. Returns a list of all the public 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. repositories: List
  104. - A list containing all public repository names
  105. belonging to a user.
  106. '''
  107. pageNumber = 1
  108. jsonList = []
  109. repositories = []
  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. for data in jsonList:
  119. for datum in data:
  120. repositories.append(datum['name'])
  121. return repositories
  122. def getForkedRepositories(clientID, clientSecret):
  123. '''
  124. Returns a list of all the public forked repositories
  125. owned by a User.
  126. Parameters:
  127. clientID: String
  128. - The clientID from registering this application
  129. on Github.
  130. clientSecret: String.
  131. - The clientSecret from registering this application
  132. on Github.
  133. Returns:
  134. forkedRepositories: List
  135. - A list containing all forked repository names
  136. belonging to a user.
  137. '''
  138. pageNumber = 1
  139. jsonList = []
  140. forkedRepositories = []
  141. while True:
  142. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  143. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  144. jsonList.append(json.loads(req.content))
  145. if len(json.loads(req.content)) < 30:
  146. break
  147. elif len(json.loads(req.content)) >= 30:
  148. pageNumber += 1
  149. forkedRepos = {}
  150. for data in jsonList:
  151. for datum in data:
  152. if datum['fork'] == True:
  153. forkedRepos['name'] = datum['name']
  154. forkedRepositories.append(forkedRepos)
  155. forkedRepos = {}
  156. return forkedRepositories
  157. def getTopContributedRepositories(repos, clientID, clientSecret):
  158. '''
  159. Returns a list containing the commit totals for all
  160. repositories owned by a user.
  161. Parameters:
  162. clientID: String
  163. - The clientID from registering this application
  164. on Github.
  165. clientSecret: String
  166. - The clientSecret from registering this application
  167. on Github.
  168. Returns:
  169. parsedData: Dictionary
  170. - A dictionary containing the following data:
  171. - commits['author']
  172. - The name of the committer
  173. - commits['total']
  174. - Total commit count for a user in a repository
  175. - commits['repo_name']
  176. - The name of the repository being tallied
  177. '''
  178. jsonList = []
  179. for repo in repos:
  180. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo \
  181. + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
  182. jsonList.append(json.loads(req.content))
  183. parsedData = []
  184. indexNumber = -1
  185. for item in jsonList:
  186. indexNumber += 1
  187. commits = {}
  188. for data in item:
  189. if data['author']['login'] == 'DrkSephy':
  190. commits['author'] = data['author']['login']
  191. commits['total'] = data['total']
  192. commits['repo_name'] = repos[indexNumber]
  193. parsedData.append(commits)
  194. return parsedData
  195. def filterCommits(data):
  196. '''
  197. Returns the top 10 committed repositories.
  198. Parameters:
  199. data: List
  200. - A list containing commit counts for all
  201. of a user's public repositories
  202. Returns:
  203. maxCommits: List
  204. - A list containing the top ten repositories
  205. with the maximum number of commits by a user
  206. '''
  207. maxCommits = []
  208. i = 0
  209. while i < 10:
  210. maxCommitedRepo = max(data, key=lambda x: x['total'])
  211. maxCommits.append(maxCommitedRepo)
  212. index = data.index(maxCommitedRepo)
  213. data.pop(index)
  214. i += 1
  215. return maxCommits
  216. def getStarGazerCount(clientID, clientSecret):
  217. '''
  218. Returns a list number of stargazers for each
  219. of a user's public repositories.
  220. Parameters:
  221. clientID: String
  222. - The clientID from registering this application
  223. on Github.
  224. clientSecret: String
  225. - The clientSecret from registering this application
  226. on Github.
  227. Returns:
  228. stargazers: Dictionary
  229. - A dictionary containing the following data:
  230. - starData['stargazers_count']
  231. - The number of stargazers for a given repository
  232. - starData['name']
  233. - The name of the repository being observed
  234. '''
  235. pageNumber = 1
  236. jsonList = []
  237. stargazers = []
  238. while True:
  239. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  240. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  241. jsonList.append(json.loads(req.content))
  242. if len(json.loads(req.content)) < 30:
  243. break
  244. elif len(json.loads(req.content)) >= 30:
  245. pageNumber += 1
  246. for data in jsonList:
  247. for datum in data:
  248. starData = {}
  249. starData['stargazers_count'] = datum['stargazers_count']
  250. starData['name'] = datum['name']
  251. stargazers.append(starData)
  252. return stargazers
  253. def filterStarGazerCount(data):
  254. '''
  255. Returns the top 10 stargazed repositories.
  256. Parameters:
  257. data: List
  258. - A list containing stargazer counts for all
  259. of a user's public repositories
  260. Returns:
  261. maxStars: List
  262. - A list containing the top ten repositories
  263. with the maximum number of stargazers
  264. '''
  265. maxStars = []
  266. i = 0
  267. while i < 10:
  268. maxStarGazers = max(data, key=lambda x: x['stargazers_count'])
  269. maxStars.append(maxStarGazers)
  270. index = data.index(maxStarGazers)
  271. data.pop(index)
  272. i += 1
  273. return maxStars