github.py 10 KB

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