From 7532a93397ca609e02019ebea36fdefa490e50e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=88=9A=28noham=29=C2=B2?= Date: Thu, 14 Mar 2024 21:16:45 +0100 Subject: [PATCH] add dominant color --- user/export.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/user/export.py b/user/export.py index 4b4cd7b..7c854b9 100644 --- a/user/export.py +++ b/user/export.py @@ -6,6 +6,8 @@ import json import requests from pprint import pprint import os +from colorthief import ColorThief +from io import BytesIO from dotenv import load_dotenv load_dotenv() USER = os.getenv("USER") @@ -53,6 +55,13 @@ def get_track_extras(song, artist, album): return (artwork_url, itunes_url, artist_url) +def get_dominant_color_from_url(url): + response = requests.get(url) + image = BytesIO(response.content) + color_thief = ColorThief(image) + dominant_color = color_thief.get_color(quality=1) + return dominant_color + def post(currentsong): currentsong['user'] = USER currentsong['password'] = PASSWORD @@ -81,6 +90,7 @@ def main(): persistendId = currentsong['persistent ID'] currentsong['timestamp'] = time.time() (currentsong['artwork_url'], currentsong['itunes_url'], currentsong['artist_url']) = get_track_extras(currentsong['name'], currentsong['artist'], currentsong['album']) + currentsong['dominantcolor'] = get_dominant_color_from_url(currentsong['artwork_url']) printout(f"{post(currentsong)}") timets = float(currentsong['duration'].replace(",", "."))-float(currentsong['pPosition'].replace(",", ".")) + 3 prevstatus = 'playing'