-
-
Save DenverCoder1/955096c0d458e63016fc397c3e8d0edf to your computer and use it in GitHub Desktop.
Get GitHub streak stats as png
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 requests | |
import re | |
import pyvips | |
# remove animation from streak stats svg | |
def remove_animation(svg): | |
# remove style tags | |
svg = re.sub(r"<style>.*?</style>", "", svg, flags=re.DOTALL) | |
# replace animations with final keyframe state | |
svg = re.sub(r"animation: fadein.*?;", "opacity: 1;", svg, flags=re.DOTALL) | |
return re.sub(r"animation: currentstreak.*?;", "font-size: 28px;", svg, flags=re.DOTALL) | |
# read url | |
def read_url(url): | |
r = requests.get(url) | |
return r.text | |
# write to file | |
def write_to_file(file_name, text): | |
f = open(file_name, "w") | |
f.write(text) | |
f.close() | |
# convert svg to png | |
def convert_svg_to_png(filename): | |
return pyvips.Image.new_from_file(filename, dpi=300) | |
# download streak stats png | |
def download_streak_stats_png(username): | |
filename = "file.svg" | |
# get streak stats url | |
url = f"https://github-readme-streak-stats.herokuapp.com/?user={username}" | |
# read streak stats and remove animation | |
svg = read_url(url) | |
# save svg without animation | |
write_to_file(filename, remove_animation(svg)) | |
# convert image to png | |
image = convert_svg_to_png(filename) | |
# save png | |
image.write_to_file(filename.replace(".svg", ".png")) | |
if __name__ == "__main__": | |
download_streak_stats_png("DenverCoder1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment