123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- '''
- github.py contains a handful of methods for interacting
- with Github data and returning the responses as JSON.
- '''
- import requests
- import simplejson as json
- ########################
- # GITHUB API CONSTANTS #
- ########################
- API_BASE_URL = 'https://api.github.com/users/DrkSephy'
- def getUserData(clientID, clientSecret):
- '''
- Returns data found on a Github User's public profile.
- This includes information such as number of followers,
- e-mail, number of repositories and more.
- Parameters:
- clientID: String
- - The clientID from registering this application
- on Github.
- clientSecret: String
- - The clientSecret from registering this application
- on Github.
- Returns:
- parsedData: Dictionary
- - A dictionary containing the following data:
- - userData['name']
- - The user's public name on Github
- - userData['blog']
- - Link to the user's blog on Github
- - userData['email']
- - The user's public e-mail on Github
- - userData['public_gists']
- - The number of the user's public gists
- - userData['public_repos']
- - The number of public repositories owned
- - userData['avatar_url']
- - Link to user's public avatar
- - userData['followers']
- - Number of followers
- - userData['following']
- - Number of users being followed
- '''
- url = API_BASE_URL + '?' + clientID + '&' + clientSecret
- req = requests.get(url)
- jsonList = []
- jsonList.append(json.loads(req.content))
- parsedData = []
- userData = {}
- for data in jsonList:
- userData['name'] = data['name']
- userData['blog'] = data['blog']
- userData['email'] = data['email']
- userData['public_gists'] = data['public_gists']
- userData['public_repos'] = data['public_repos']
- userData['avatar_url'] = data['avatar_url']
- userData['followers'] = data['followers']
- userData['following'] = data['following']
- parsedData.append(userData)
- return parsedData
- def getUserRepositories(clientID, clientSecret):
- '''
- Returns a list of all the public repositories
- owned by a User.
- Parameters:
- clientID: String
- - The clientID from registering this application
- on Github.
- clientSecret: String.
- - The clientSecret from registering this application
- on Github.
- Returns:
- repositories: List
- - A list containing all public repository names
- belonging to a user.
- '''
- pageNumber = 1
- jsonList = []
- repositories = []
- while True:
- req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
- + str(pageNumber) + '&' + clientID + '&' + clientSecret)
- jsonList.append(json.loads(req.content))
- if len(json.loads(req.content)) < 30:
- break
- elif len(json.loads(req.content)) >= 30:
- pageNumber += 1
- for data in jsonList:
- for datum in data:
- repositories.append(datum['name'])
- return repositories
- def getForkedRepositories(clientID, clientSecret):
- '''
- Returns a list of all the public forked repositories
- owned by a User.
- Parameters:
- clientID: String
- - The clientID from registering this application
- on Github.
- clientSecret: String.
- - The clientSecret from registering this application
- on Github.
- Returns:
- forkedRepositories: List
- - A list containing all forked repository names
- belonging to a user.
- '''
- pageNumber = 1
- jsonList = []
- forkedRepositories = []
- while True:
- req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
- + str(pageNumber) + '&' + clientID + '&' + clientSecret)
- jsonList.append(json.loads(req.content))
- if len(json.loads(req.content)) < 30:
- break
- elif len(json.loads(req.content)) >= 30:
- pageNumber += 1
- forkedRepos = {}
- for data in jsonList:
- for datum in data:
- if datum['fork'] == True:
- forkedRepos['name'] = datum['name']
- forkedRepositories.append(forkedRepos)
- forkedRepos = {}
- return forkedRepositories
- def getTopContributedRepositories(repos, clientID, clientSecret):
- '''
- Returns a list containing the commit totals for all
- repositories owned by a user.
- Parameters:
- clientID: String
- - The clientID from registering this application
- on Github.
- clientSecret: String
- - The clientSecret from registering this application
- on Github.
- Returns:
- parsedData: Dictionary
- - A dictionary containing the following data:
- - commits['author']
- - The name of the committer
- - commits['total']
- - Total commit count for a user in a repository
- - commits['repo_name']
- - The name of the repository being tallied
- '''
- jsonList = []
- for repo in repos:
- req = requests.get('https://api.github.com/repos/DrkSephy/' + repo \
- + '/stats/contributors' + '?' + clientID + '&' + clientSecret)
- jsonList.append(json.loads(req.content))
- parsedData = []
- indexNumber = -1
- for item in jsonList:
- indexNumber += 1
- commits = {}
- for data in item:
- if data['author']['login'] == 'DrkSephy':
- commits['author'] = data['author']['login']
- commits['total'] = data['total']
- commits['repo_name'] = repos[indexNumber]
- parsedData.append(commits)
- return parsedData
- def filterCommits(data):
- '''
- Returns the top 10 committed repositories.
- Parameters:
- data: List
- - A list containing commit counts for all
- of a user's public repositories
- Returns:
- maxCommits: List
- - A list containing the top ten repositories
- with the maximum number of commits by a user
- '''
- maxCommits = []
- i = 0
- while i < 10:
- maxCommitedRepo = max(data, key=lambda x: x['total'])
- maxCommits.append(maxCommitedRepo)
- index = data.index(maxCommitedRepo)
- data.pop(index)
- i += 1
- return maxCommits
- def getStarGazerCount(clientID, clientSecret):
- '''
- Returns a list number of stargazers for each
- of a user's public repositories.
- Parameters:
- clientID: String
- - The clientID from registering this application
- on Github.
- clientSecret: String
- - The clientSecret from registering this application
- on Github.
- Returns:
- stargazers: Dictionary
- - A dictionary containing the following data:
- - starData['stargazers_count']
- - The number of stargazers for a given repository
- - starData['name']
- - The name of the repository being observed
- '''
- pageNumber = 1
- jsonList = []
- stargazers = []
- while True:
- req = requests.get('https://api.github.com/users/DrkSephy/repos?page=' \
- + str(pageNumber) + '&' + clientID + '&' + clientSecret)
- jsonList.append(json.loads(req.content))
- if len(json.loads(req.content)) < 30:
- break
- elif len(json.loads(req.content)) >= 30:
- pageNumber += 1
- for data in jsonList:
- for datum in data:
- starData = {}
- starData['stargazers_count'] = datum['stargazers_count']
- starData['name'] = datum['name']
- stargazers.append(starData)
- return stargazers
- def filterStarGazerCount(data):
- '''
- Returns the top 10 stargazed repositories.
- Parameters:
- data: List
- - A list containing stargazer counts for all
- of a user's public repositories
- Returns:
- maxStars: List
- - A list containing the top ten repositories
- with the maximum number of stargazers
- '''
- maxStars = []
- i = 0
- while i < 10:
- maxStarGazers = max(data, key=lambda x: x['stargazers_count'])
- maxStars.append(maxStarGazers)
- index = data.index(maxStarGazers)
- data.pop(index)
- i += 1
- return maxStars
|