从 PyGithub 高效地获取最新提交 URL
Get Latest Commit URL from PyGithub Efficiently
我正在使用此函数获取最新提交 url 使用 PyGithub:
from github import Github
def getLastCommitURL():
encrypted = 'mypassword'
# naiveDecrypt defined elsewhere
g = Github('myusername', naiveDecrypt(encrypted))
org = g.get_organization('mycompany')
code = org.get_repo('therepo')
commits = code.get_commits()
last = commits[0]
return last.html_url
它有效,但它似乎让 Github 对我的 IP 地址不满意,并让我对结果 url 的响应很慢。有没有更有效的方法让我做到这一点?
如果您在过去 24 小时内没有提交,这将不起作用。但是如果你这样做,它似乎 return 更快并且会请求更少的提交,根据 Github API documentation:
from datetime import datetime, timedelta
def getLastCommitURL():
encrypted = 'mypassword'
g = Github('myusername', naiveDecrypt(encrypted))
org = g.get_organization('mycompany')
code = org.get_repo('therepo')
# limit to commits in past 24 hours
since = datetime.now() - timedelta(days=1)
commits = code.get_commits(since=since)
last = commits[0]
return last.html_url
您可以直接向 api 提出请求。
from urllib.request import urlopen
import json
def get_latest_commit(owner, repo):
url = 'https://api.github.com/repos/{owner}/{repo}/commits?per_page=1'.format(owner=owner, repo=repo)
response = urlopen(url).read()
data = json.loads(response.decode())
return data[0]
if __name__ == '__main__':
commit = get_latest_commit('mycompany', 'therepo')
print(commit['html_url'])
在这种情况下,您只会向 api 发出一个请求,而不是 3 个,并且您只会获得最后一次提交,而不是所有提交。应该也更快。
我正在使用此函数获取最新提交 url 使用 PyGithub:
from github import Github
def getLastCommitURL():
encrypted = 'mypassword'
# naiveDecrypt defined elsewhere
g = Github('myusername', naiveDecrypt(encrypted))
org = g.get_organization('mycompany')
code = org.get_repo('therepo')
commits = code.get_commits()
last = commits[0]
return last.html_url
它有效,但它似乎让 Github 对我的 IP 地址不满意,并让我对结果 url 的响应很慢。有没有更有效的方法让我做到这一点?
如果您在过去 24 小时内没有提交,这将不起作用。但是如果你这样做,它似乎 return 更快并且会请求更少的提交,根据 Github API documentation:
from datetime import datetime, timedelta
def getLastCommitURL():
encrypted = 'mypassword'
g = Github('myusername', naiveDecrypt(encrypted))
org = g.get_organization('mycompany')
code = org.get_repo('therepo')
# limit to commits in past 24 hours
since = datetime.now() - timedelta(days=1)
commits = code.get_commits(since=since)
last = commits[0]
return last.html_url
您可以直接向 api 提出请求。
from urllib.request import urlopen
import json
def get_latest_commit(owner, repo):
url = 'https://api.github.com/repos/{owner}/{repo}/commits?per_page=1'.format(owner=owner, repo=repo)
response = urlopen(url).read()
data = json.loads(response.decode())
return data[0]
if __name__ == '__main__':
commit = get_latest_commit('mycompany', 'therepo')
print(commit['html_url'])
在这种情况下,您只会向 api 发出一个请求,而不是 3 个,并且您只会获得最后一次提交,而不是所有提交。应该也更快。