Last active
July 23, 2020 21:02
-
-
Save joedf/84279ff7647d554a9d15f280a209c6b7 to your computer and use it in GitHub Desktop.
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 os | |
import subprocess | |
import datetime | |
def file2str(path): | |
with open(path, 'r') as file: | |
return file.read().strip() | |
def update_badge_html(): | |
command = 'python3 /home/path/to/github-badge-2/src/generate_badge.py' | |
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | |
process.wait() | |
def application(environ, start_response): | |
if environ.get('PATH_INFO') == '/': | |
status = '200 OK' | |
b_path = "/home/path/to/github-badge-2/src/badge.html" | |
if os.path.exists(b_path): | |
b_age = datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(b_path)) | |
# update/regenerate if a 1day+ old | |
if (b_age.days >= 1): | |
update_badge_html() | |
else: | |
# non existant yet | |
update_badge_html() | |
badge_html = file2str(b_path) | |
content = badge_html | |
else: | |
status = '404 NOT FOUND' | |
content = 'Page not found.' | |
response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))] | |
start_response(status, response_headers) | |
yield content.encode('utf8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment