github.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. '''
  2. Module github.py contains a handful of methods
  3. for interacting with Github data.
  4. '''
  5. import requests
  6. import simplejson as json
  7. ########################
  8. # GITHUB API CONSTANTS #
  9. ########################
  10. API_BASE_URL = 'https://api.github.com/users/DrkSephy'
  11. def getUserData(clientID, clientSecret):
  12. '''Get generic Github User data.'''
  13. url = API_BASE_URL + '?' + clientID + '&' + clientSecret
  14. req = requests.get(url)
  15. jsonList = []
  16. jsonList.append(json.loads(req.content))
  17. parsedData = []
  18. userData = {}
  19. for data in jsonList:
  20. userData['name'] = data['name']
  21. userData['blog'] = data['blog']
  22. userData['email'] = data['email']
  23. userData['public_gists'] = data['public_gists']
  24. userData['public_repos'] = data['public_repos']
  25. userData['avatar_url'] = data['avatar_url']
  26. userData['followers'] = data['followers']
  27. userData['following'] = data['following']
  28. parsedData.append(userData)
  29. return parsedData
  30. def getUserRepositories(clientID, clientSecret):
  31. '''Get a list of all repositories owned by a User.'''
  32. pageNumber = 1
  33. jsonList = []
  34. repositories = []
  35. while True:
  36. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  37. jsonList.append(json.loads(req.content))
  38. if len(json.loads(req.content)) < 30:
  39. break
  40. elif len(json.loads(req.content)) >= 30:
  41. pageNumber += 1
  42. for data in jsonList:
  43. for datum in data:
  44. repositories.append(datum['name'])
  45. return repositories
  46. def getForkedRepositories(clientID, clientSecret):
  47. '''Get a list of all forked repositories by a user.'''
  48. pageNumber = 1
  49. jsonList = []
  50. forkedRepositories = []
  51. while True:
  52. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  53. jsonList.append(json.loads(req.content))
  54. if len(json.loads(req.content)) < 30:
  55. break
  56. elif len(json.loads(req.content)) >= 30:
  57. pageNumber += 1
  58. forkedRepos = {}
  59. for data in jsonList:
  60. for datum in data:
  61. if datum['fork'] == True:
  62. forkedRepos['name'] = datum['name']
  63. forkedRepositories.append(forkedRepos)
  64. forkedRepos = {}
  65. return forkedRepositories
  66. def getTopContributedRepositories(repos, clientID, clientSecret):
  67. '''Get a list of all commits for each repository owned.'''
  68. jsonList = []
  69. for repo in repos:
  70. req = requests.get('https://api.github.com/repos/DrkSephy/' + repo + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
  71. jsonList.append(json.loads(req.content))
  72. parsedData = []
  73. indexNumber = -1
  74. for item in jsonList:
  75. indexNumber += 1
  76. commits = {}
  77. for data in item:
  78. if data['author']['login'] == 'DrkSephy':
  79. commits['author'] = data['author']['login']
  80. commits['total'] = data['total']
  81. commits['repo_name'] = repos[indexNumber]
  82. parsedData.append(commits)
  83. return parsedData
  84. def filterCommits(data):
  85. '''Returns the top 10 committed repositories.'''
  86. maxCommits = []
  87. for i in range(1, 10):
  88. maxCommitedRepo = max(data, key=lambda x:x['total'])
  89. maxCommits.append(maxCommitedRepo)
  90. index = data.index(maxCommitedRepo)
  91. data.pop(index)
  92. return maxCommits
  93. def getStarGazerCount(clientID, clientSecret):
  94. '''Get Stargazer counts for all repositories.'''
  95. pageNumber = 1
  96. jsonList = []
  97. stargazers = []
  98. while True:
  99. req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' + str(pageNumber) + '&' + clientID + '&' + clientSecret)
  100. jsonList.append(json.loads(req.content))
  101. if len(json.loads(req.content)) < 30:
  102. break
  103. elif len(json.loads(req.content)) >= 30:
  104. pageNumber += 1
  105. for data in jsonList:
  106. for datum in data:
  107. starData = {}
  108. starData['stargazers_count'] = datum['stargazers_count']
  109. starData['name'] = datum['name']
  110. stargazers.append(starData)
  111. return stargazers
  112. def filterStarGazerCount(data):
  113. '''Return top 10 starred repositories.'''
  114. maxStars= []
  115. for i in range(1, 10):
  116. maxStarGazers = max(data, key=lambda x:x['stargazers_count'])
  117. maxStars.append(maxStarGazers)
  118. index = data.index(maxStarGazers)
  119. data.pop(index)
  120. return maxStars