Skip to content

Instantly share code, notes, and snippets.

@researcx
Last active September 27, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save researcx/783ab2eb5a0edcc3971274ec5cf2d991 to your computer and use it in GitHub Desktop.
Save researcx/783ab2eb5a0edcc3971274ec5cf2d991 to your computer and use it in GitHub Desktop.
periodically make a html page of your github repos (requires github cli tools (gh command))
import json, humanize, os, html
from datetime import datetime, timedelta
file = '/home/keira/Repositories/Public/github.html'
github_json_file = '/home/keira/Resources/github.json'
# cron every 30 minutes:
# */30 * * * * /usr/bin/gh repo list researcx -L 400 --visibility public --json name,owner,pushedAt,isFork,diskUsage,description > /home/keira/Resources/github.json
# */31 * * * * /usr/bin/python3 /home/keira/Scripts/githubrepos.py
# demo:
# https://xch.fairuse.org/~devk/list/static/files/Repositories/github.html
def human_size(filesize):
return humanize.naturalsize(filesize)
def recent_date(unixtime):
dt = datetime.fromtimestamp(unixtime)
today = datetime.now()
today_start = datetime(today.year, today.month, today.day)
yesterday_start = datetime.now() - timedelta(days=1)
def day_in_this_week(date):
startday = datetime.now() - timedelta(days=today.weekday())
if(date >= startday):
return True
else:
return False
timeformat = '%b %d, %Y'
if day_in_this_week(dt):
timeformat = '%A at %H:%M'
if(dt >= yesterday_start):
timeformat = 'Yesterday at %H:%M'
if(dt >= today_start):
timeformat = 'Today at %H:%M'
return(dt.strftime(timeformat))
def shorten_text(s, n):
if len(s) <= n:
# string is already short-enough
return s
# half of the size, minus the 3 .'s
n_2 = int(n) / 2 - 3
# whatever's left
n_1 = n - n_2 - 3
return '{0}...{1}'.format(s[:int(n_1)], s[-int(n_2):])
cmd = 'gh repo list researcx -L 400 --visibility public --json name,owner,pushedAt,isFork,diskUsage,description > github.json'
repo_list = ""
last_updated = ""
i = 0
with open(github_json_file, 'r') as gh_repos:
repos = json.load(gh_repos)
# print(gh_repos)
for repo in repos:
i = 1 if i == 0 else 0
#print(repo)
iso_date = datetime.strptime(repo['pushedAt'], '%Y-%m-%dT%H:%M:%SZ')
timestamp = int((iso_date - datetime(1970, 1, 1)).total_seconds())
date_time = recent_date(timestamp)
str_repo = str(repo['name'])
path_repo = str(repo['owner']['login']) + "/" + html.escape(str(repo['name']))
str_desc = "<br/><span class='description' title='" + html.escape(str(repo['description'])) + "'>" + shorten_text(html.escape(str(repo['description'])), 150) + "</span>"
link_repo = '<a href="https://github.com/'+path_repo+'" class="user" target="_blank">'+str(repo['owner']['login']) + "/"+'</a><a href="https://github.com/'+path_repo+'" target="_blank">'+str_repo+'</a>'
disk_usage = " " + human_size(repo['diskUsage'] * 1024) + " &nbsp; "
is_fork = "<span title='Fork'>[F]</span> &nbsp; " if repo['isFork'] == True else ""
repo_list += """
<div class="list v"""+str(i)+""""><span style="float:right;">""" + is_fork + disk_usage + str(date_time) + """</span>""" + link_repo + str_desc + """</div>"""
if last_updated == "":
last_updated = timestamp
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>repositories</title>
<style>
body, html {
padding: 0px;
margin: 0px;
color: #dc7224;
font-family: JetBrainsMono, Consolas, "Helvetica Neue", Helvetica, Arial, sans-serif
}
a {
color: #dc7224 !important;
text-decoration: none;
}
a:hover {
color: #efefef !important;
tect-decoration: underline;
}
a.user {
color: #aa7735 !important;
text-decoration: none;
}
span {
font-size: 14pt;
margin-top: 2px;
color: #555;
}
.list {
font-size: 16pt;
padding: 4px 8px;
border-bottom: 1px dashed #101010;
background: #000;
}
.list.v0 {
background: #0f0f0f;
}
.list.v1 {
background: #121212;
}
.description{
font-size: 10pt;
color: #555;
}
</style>
</head>
<body>"""+repo_list+"""
<div class="list" style="text-align: right;">
<span style="text-style: italic; font-size: 9pt;">generated by <a href="https://gist.github.com/researcx">githubrepos.py</a></span>
</div>
</body>
</html>
"""
f = open(file, "w+")
f.write(html)
f.close()
os.utime(file, (last_updated, last_updated))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment