github.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. '''
  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(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 + '?' + clientID + '&' + clientSecret
  115. req = requests.get(url)
  116. jsonList = []
  117. jsonList.append(json.loads(req.content))
  118. parsedData = []
  119. userData = {}
  120. for data in jsonList:
  121. userData['name'] = data['name']
  122. userData['blog'] = data['blog']
  123. userData['email'] = data['email']
  124. userData['public_gists'] = data['public_gists']
  125. userData['public_repos'] = data['public_repos']
  126. userData['avatar_url'] = data['avatar_url']
  127. userData['followers'] = data['followers']
  128. userData['following'] = data['following']
  129. parsedData.append(userData)
  130. return parsedData
  131. def getUserRepositories(clientID, clientSecret):
  132. '''
  133. Returns a list of all the public repositories
  134. owned by a User.
  135. Parameters:
  136. clientID: String
  137. - The clientID from registering this application
  138. on Github.
  139. clientSecret: String.
  140. - The clientSecret from registering this application
  141. on Github.
  142. Returns:
  143. repositories: List
  144. - A list containing all public repository names
  145. belonging to a user.
  146. '''
  147. pageNumber = 1
  148. jsonList = []
  149. repositories = []
  150. while True:
  151. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  152. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  153. jsonList.append(json.loads(req.content))
  154. if len(json.loads(req.content)) < 30:
  155. break
  156. elif len(json.loads(req.content)) >= 30:
  157. pageNumber += 1
  158. for data in jsonList:
  159. for datum in data:
  160. repositories.append(datum['name'])
  161. return repositories
  162. def getForkedRepositories(clientID, clientSecret):
  163. '''
  164. Returns a list of all the public forked repositories
  165. owned by a User.
  166. Parameters:
  167. clientID: String
  168. - The clientID from registering this application
  169. on Github.
  170. clientSecret: String.
  171. - The clientSecret from registering this application
  172. on Github.
  173. Returns:
  174. forkedRepositories: List
  175. - A list containing all forked repository names
  176. belonging to a user.
  177. '''
  178. pageNumber = 1
  179. jsonList = []
  180. forkedRepositories = []
  181. while True:
  182. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  183. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  184. jsonList.append(json.loads(req.content))
  185. if len(json.loads(req.content)) < 30:
  186. break
  187. elif len(json.loads(req.content)) >= 30:
  188. pageNumber += 1
  189. forkedRepos = {}
  190. for data in jsonList:
  191. for datum in data:
  192. if datum['fork'] == True:
  193. forkedRepos['name'] = datum['name']
  194. forkedRepositories.append(forkedRepos)
  195. forkedRepos = {}
  196. return forkedRepositories
  197. def getTopContributedRepositories(repos, clientID, clientSecret):
  198. '''
  199. Returns a list containing the commit totals for all
  200. repositories owned by a user.
  201. Parameters:
  202. clientID: String
  203. - The clientID from registering this application
  204. on Github.
  205. clientSecret: String
  206. - The clientSecret from registering this application
  207. on Github.
  208. Returns:
  209. parsedData: Dictionary
  210. - A dictionary containing the following data:
  211. - commits['author']
  212. - The name of the committer
  213. - commits['total']
  214. - Total commit count for a user in a repository
  215. - commits['repo_name']
  216. - The name of the repository being tallied
  217. '''
  218. jsonList = []
  219. for repo in repos:
  220. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo \
  221. + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
  222. jsonList.append(json.loads(req.content))
  223. parsedData = []
  224. indexNumber = -1
  225. for item in jsonList:
  226. indexNumber += 1
  227. commits = {}
  228. for data in item:
  229. if data['author']['login'] == 'DrkSephy':
  230. commits['author'] = data['author']['login']
  231. commits['total'] = data['total']
  232. commits['repo_name'] = repos[indexNumber]
  233. parsedData.append(commits)
  234. return parsedData
  235. def filterCommits(data):
  236. '''
  237. Returns the top 10 committed repositories.
  238. Parameters:
  239. data: List
  240. - A list containing commit counts for all
  241. of a user's public repositories
  242. Returns:
  243. maxCommits: List
  244. - A list containing the top ten repositories
  245. with the maximum number of commits by a user
  246. '''
  247. maxCommits = []
  248. i = 0
  249. while i < 10:
  250. maxCommitedRepo = max(data, key=lambda x: x['total'])
  251. maxCommits.append(maxCommitedRepo)
  252. index = data.index(maxCommitedRepo)
  253. data.pop(index)
  254. i += 1
  255. return maxCommits
  256. def getStarGazerCount(clientID, clientSecret):
  257. '''
  258. Returns a list number of stargazers for each
  259. of a user's public repositories.
  260. Parameters:
  261. clientID: String
  262. - The clientID from registering this application
  263. on Github.
  264. clientSecret: String
  265. - The clientSecret from registering this application
  266. on Github.
  267. Returns:
  268. stargazers: Dictionary
  269. - A dictionary containing the following data:
  270. - starData['stargazers_count']
  271. - The number of stargazers for a given repository
  272. - starData['name']
  273. - The name of the repository being observed
  274. '''
  275. pageNumber = 1
  276. jsonList = []
  277. stargazers = []
  278. while True:
  279. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
  280. + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  281. jsonList.append(json.loads(req.content))
  282. if len(json.loads(req.content)) < 30:
  283. break
  284. elif len(json.loads(req.content)) >= 30:
  285. pageNumber += 1
  286. for data in jsonList:
  287. for datum in data:
  288. starData = {}
  289. starData['stargazers_count'] = datum['stargazers_count']
  290. starData['name'] = datum['name']
  291. stargazers.append(starData)
  292. return stargazers
  293. def filterStarGazerCount(data):
  294. '''
  295. Returns the top 10 stargazed repositories.
  296. Parameters:
  297. data: List
  298. - A list containing stargazer counts for all
  299. of a user's public repositories
  300. Returns:
  301. maxStars: List
  302. - A list containing the top ten repositories
  303. with the maximum number of stargazers
  304. '''
  305. maxStars = []
  306. i = 0
  307. while i < 10:
  308. maxStarGazers = max(data, key=lambda x: x['stargazers_count'])
  309. maxStars.append(maxStarGazers)
  310. index = data.index(maxStarGazers)
  311. data.pop(index)
  312. i += 1
  313. return maxStars