github.py 11 KB

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