From 8b209db20e1c877f5fdbe9e3784eb5409ff9d687 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Thu, 6 Feb 2020 17:57:50 +0100 Subject: [PATCH] generate color server-side --- color.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ models.py | 3 +++ web/DetailView.vue | 25 ++----------------------- web/color.js | 3 +++ 4 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 color.py create mode 100644 web/color.js diff --git a/color.py b/color.py new file mode 100644 index 0000000..d51e2fb --- /dev/null +++ b/color.py @@ -0,0 +1,46 @@ +import json +import sys +import tempfile +from pathlib import Path +from subprocess import run + +import requests +import sentry_sdk + +import config +from models import * + + +def to_rgb_string(r: float, g: float, b: float) -> str: + r, g, b = map(int, [r, g, b]) + return "{0:02x}{1:02x}{2:02x}".format(r, g, b) + + +colorjs = Path("./web/color.js") + +if config.sentryDSN: + client = sentry_sdk.init(dsn=config.sentryDSN) + +if len(sys.argv) > 1: + limit = int(sys.argv[1]) +else: + limit = 50 + +query = Song.select().where((Song.show == 0) & (Song.image_large.is_null(False)) & (Song.background_color.is_null())) +for song in query.limit(limit): + url = song.image_large + + r = requests.get(url) + with tempfile.TemporaryDirectory() as tmpdirname: + tmpdir = Path(tmpdirname) + image = tmpdir / "image.jpg" + with open(image, 'wb') as fd: + for chunk in r.iter_content(chunk_size=128): + fd.write(chunk) + output = run(["node", colorjs, image], capture_output=True) + data = json.loads(output.stdout) + print(data) + song.background_color = to_rgb_string(*data["LightVibrant"]["rgb"]) if "LightVibrant" in data else None + song.alternative_color = to_rgb_string(*data["DarkVibrant"]["rgb"]) if "DarkVibrant" in data else None + song.text_color = to_rgb_string(*data["DarkMuted"]["rgb"]) if "DarkMuted" in data else None + song.save() diff --git a/models.py b/models.py index a039338..4f97427 100644 --- a/models.py +++ b/models.py @@ -21,6 +21,9 @@ class Song(BaseModel): spotify_url = CharField(null=True) image_small = CharField(null=True) image_large = CharField(null=True) + background_color = CharField(null=True, max_length=6) + alternative_color = CharField(null=True, max_length=6) + text_color = CharField(null=True, max_length=6) class Meta: indexes = ((("artist", "title"), True),) diff --git a/web/DetailView.vue b/web/DetailView.vue index 25deb09..865159b 100644 --- a/web/DetailView.vue +++ b/web/DetailView.vue @@ -1,5 +1,5 @@