Created
October 2, 2018 16:51
-
-
Save makkoncept/a4761815c67dfca5e6f109c11a9564bd to your computer and use it in GitHub Desktop.
A python script to parse out github links of projects from a markdown file and then print the number of months from the last commit. Enter the access token for the github api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mistune | |
import requests | |
from datetime import datetime | |
from bs4 import BeautifulSoup | |
# import time | |
f = open("<markdown file in the same directory>", "r") # enter the markdown file name | |
markdown_to_html = mistune.markdown(f.read()) | |
soup = BeautifulSoup(markdown_to_html, "html.parser") | |
links = [] | |
for link in soup.findAll('a'): | |
link_split = link.get('href').split('/') | |
if 'github.com' in link_split: | |
links.append(link.get('href')) | |
print(links) | |
date_format = "%Y-%m-%dT%H:%M:%SZ" | |
for url in links: | |
url_split = url.split('/') | |
owner = url_split[len(url_split)-2] | |
repo = url_split[len(url_split)-1] | |
repo = repo.split('#')[0] | |
print(owner) | |
print(repo) | |
try: | |
github_api_url = f"https://api.github.com/repos/{owner}/{repo}/git/refs/heads/master?access_token=<enter your access token>" #fetch reference object | |
new_url = requests.get(github_api_url).json().get('object').get('url')+'?access_token=<enter your access token>' # fetch commit object | |
print(new_url) | |
last_commit_date = requests.get(new_url).json().get('author').get('date') | |
last_commit_date_parse = datetime.strptime(last_commit_date, date_format) | |
except: | |
print('CHECK FOR THIS URL MANUALLY', url) | |
continue | |
current_date = datetime.utcnow() | |
delta = current_date - last_commit_date_parse | |
print(url, delta.days/30) # number of months from the last commit | |
# time.sleep(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment