github.py 10 KB

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