Last active
December 20, 2022 23:11
-
-
Save sandyjmacdonald/3e8c341248295eda291f16348ce30354 to your computer and use it in GitHub Desktop.
Display a GitHub user commit graph on the Pimoroni Galactic Unicorn LED matrix
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
# Pop this on another machine, and run with: | |
# python3 github-graph-api.py --host 0.0.0.0 | |
# | |
# Take note of this machine's IP address, as you'll | |
# need to add it in the GU code. | |
import requests | |
from bs4 import BeautifulSoup, SoupStrainer | |
from flask import Flask, jsonify, make_response | |
app = Flask(__name__) | |
def get_commit_graph(username): | |
base_url = "https://github.com/" + username | |
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} | |
page = requests.get(base_url, headers=headers) | |
soup = BeautifulSoup(page.text, 'html.parser') | |
contribs_div = soup.find_all("svg", {"class": "js-calendar-graph-svg"}) | |
weeks = contribs_div[0].find_all("g") | |
weeks = [w.find_all("rect") for w in weeks[1:]] | |
contribs = [] | |
for w in weeks: | |
week = [] | |
for d in w: | |
val = d["data-count"] | |
week.append(int(val)) | |
contribs.append(week) | |
contribs_all = [x for y in contribs for x in y] | |
max_val = max(contribs_all) | |
contribs = [[float(v / max_val) for v in c] for c in contribs] | |
return contribs | |
@app.route('/github_graph/api/v1.0/<username>', methods=['GET']) | |
def return_graph(username): | |
return str(get_commit_graph(username)) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', debug=True, port=8080) |
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
# Pop this on your Galactic Unicorn! | |
# | |
# You'll need to change the "flask_api_ip_addr" to the IP address | |
# of the machine running the Flask API and the GitHub "username". | |
import gc | |
import time | |
import uasyncio | |
import ujson | |
import math | |
import WIFI_CONFIG | |
from urllib import urequest | |
from network_manager import NetworkManager | |
from galactic import GalacticUnicorn | |
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY | |
gu = GalacticUnicorn() | |
graphics = PicoGraphics(DISPLAY) | |
@micropython.native # noqa: F821 | |
def from_hsv(h, s, v): | |
i = math.floor(h * 6.0) | |
f = h * 6.0 - i | |
v *= 255.0 | |
p = v * (1.0 - s) | |
q = v * (1.0 - f * s) | |
t = v * (1.0 - (1.0 - f) * s) | |
i = int(i) % 6 | |
if i == 0: | |
return int(v), int(t), int(p) | |
if i == 1: | |
return int(q), int(v), int(p) | |
if i == 2: | |
return int(p), int(v), int(t) | |
if i == 3: | |
return int(p), int(q), int(v) | |
if i == 4: | |
return int(t), int(p), int(v) | |
if i == 5: | |
return int(v), int(p), int(q) | |
def status_handler(mode, status, ip): | |
print(mode, status, ip) | |
@micropython.native # noqa: F821 | |
def show_graph(graph): | |
for w in range(len(graph)): | |
for d in range(len(graph[w])): | |
h = 0.33 | |
s = 1.0 | |
v = float(graph[w][d]) | |
r, g, b = from_hsv(h, s, v) | |
graphics.set_pen(graphics.create_pen(r, g, b)) | |
graphics.pixel(w, d + 2) | |
gu.update(graphics) | |
gc.collect() | |
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) | |
username = "kevinmcaleer" | |
flask_api_ip_addr = "192.168.0.152" | |
url = f"http://{flask_api_ip_addr}:8080/github_graph/api/v1.0/{username}" | |
refresh_time = 60 | |
while True: | |
gc.collect() | |
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK)) | |
socket = urequest.urlopen(url) | |
graph = ujson.load(socket) | |
graph = list(graph) | |
show_graph(graph) | |
socket.close() | |
time.sleep(refresh_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment