Skip to content

Instantly share code, notes, and snippets.

@ghandic
Last active February 16, 2018 19:11
Show Gist options
  • Save ghandic/0ab1a05c50fa8c212fff8f3039383c54 to your computer and use it in GitHub Desktop.
Save ghandic/0ab1a05c50fa8c212fff8f3039383c54 to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
from datetime import datetime
def get_most_recent_git_change_for_file(url):
"""
Parameters:
----------
url: str, the url to where the github file is stored
Example:
--------
get_most_recent_git_change_for_file('https://github.com/ghandic/confluenceapi/blob/master/README.md')
"""
assert isinstance(url, str), 'url should be the path to where a github file is stored'
url_list = url.split('/')
url_list[5] = 'blame'
new_url = '/'.join(url_list)
r = requests.get(url=new_url)
soup = BeautifulSoup(r.text)
all_blame_times = [datetime.strptime(t.get('datetime'), "%Y-%m-%dT%H:%M:%SZ") for t in soup.find_all('time-ago')]
return max(all_blame_times).strftime('%d-%m-%Y %H:%M:%S')
# Or get by using the API
import requests
import json
import dateutil.parser
def get_last_update_time(url):
info = {
'repo':url.split('/')[4],
'owner':url.split('/')[3],
'path': '/'.join(url.split('/')[7:] if isinstance(url.split('/')[7:], list) else url.split('/')[7]),
'branch':url.split('/')[6]
}
r = requests.get('https://api.github.com/repos/{owner}/{repo}/commits/{branch}?path={path}&page=1'.format(**info))
if r.status_code == 200:
last_updated = dateutil.parser.parse(json.loads(r.text)['commit']['committer']['date'])
return last_updated.strftime('%d/%m/%Y %H:%M:%S')
else:
return 'Failed to load'
get_last_update_time('https://github.com/plotly/dash-recipes/blob/master/downloads/NYC-download.xlsx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment