1
0
Fork 0
mirror of https://github.com/Findus23/RadioStats.git synced 2024-09-19 16:03:48 +02:00
RadioStats/color.py

62 lines
1.8 KiB
Python
Raw Normal View History

2020-02-06 17:57:50 +01:00
import json
import sys
import tempfile
from pathlib import Path
from subprocess import run
2020-02-06 18:34:06 +01:00
from time import sleep
2020-02-06 17:57:50 +01:00
import requests
import sentry_sdk
2020-09-28 21:44:06 +02:00
from peewee import fn
2020-02-06 17:57:50 +01:00
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)
2020-02-07 18:48:19 +01:00
cwd = Path(__file__).resolve().parent
colorjs = cwd / Path("./web/color.js")
2020-02-06 17:57:50 +01:00
if config.sentryDSN:
client = sentry_sdk.init(dsn=config.sentryDSN)
if len(sys.argv) > 1:
limit = int(sys.argv[1])
else:
limit = 50
2021-05-12 21:12:37 +02:00
query = Song.select().where(
(Song.show == 0) & (Song.spotify_data == 1) & (Song.image_large.is_null(False)) & (Song.background_color.is_null()))
2020-02-14 16:49:15 +01:00
for song in query.order_by(fn.Rand()).limit(limit):
2020-02-06 18:49:20 +01:00
print(song.title)
2020-02-06 17:57:50 +01:00
url = song.image_large
2020-02-06 18:49:20 +01:00
print(url)
2020-02-06 18:28:43 +01:00
if not url:
continue
2020-02-06 17:57:50 +01:00
r = requests.get(url)
2020-02-14 16:53:07 +01:00
if r.status_code == 404:
song.spotify_data = None
2020-02-14 16:49:15 +01:00
song.save()
continue
2020-02-06 17:57:50 +01:00
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)
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()
2020-02-06 18:49:20 +01:00
print(song.background_color)
print(song.alternative_color)
print(song.text_color)
2020-02-06 18:34:06 +01:00
sleep(5)