From 62285d572b2b8c9c1a1cad23fa798e0ee5193ac1 Mon Sep 17 00:00:00 2001 From: Wizzard <25581244+Wizzard@users.noreply.toomuchslop.com> Date: Mon, 16 Oct 2023 20:21:13 -0400 Subject: [PATCH] Change bar colors with album cover in the most retarded way possible --- deps.sh | 2 ++ main.js | 86 ++++++++++++++++++++++++++++++---------------------- package.json | 1 + tap_in.py | 23 ++++++++++++++ 4 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 tap_in.py diff --git a/deps.sh b/deps.sh index 365ac7a..cd27782 100755 --- a/deps.sh +++ b/deps.sh @@ -1 +1,3 @@ npm install axios sharp get-image-colors pm2 +pip3 install libqtile --break-system-packages +pip3 install pyautogui --break-system-packages diff --git a/main.js b/main.js index b96e336..9a73c26 100644 --- a/main.js +++ b/main.js @@ -1,27 +1,22 @@ const axios = require('axios'); const sharp = require('sharp'); -const getColors = require('get-image-colors'); -const rp = require("request-promise"); +const rp = require('request-promise'); const fs = require('fs'); const { exec } = require('child_process'); const config = JSON.parse(fs.readFileSync("config.json")); -const updateInterval = 5000; +const updateInterval = 5000; async function processAlbumCover(url) { try { console.log(`Processing album cover from URL: ${url}`); - + const response = await axios.get(url, { responseType: 'arraybuffer' }); const imageBuffer = Buffer.from(response.data, 'binary'); const imageSharp = sharp(imageBuffer); const metadata = await imageSharp.metadata(); - if (metadata.format !== 'jpeg' && metadata.format !== 'jpg' && metadata.format !== 'png') { - throw new Error('Not a supported image format'); - } - const dominantColor = await getDominantColor(imageBuffer); const resizedBuffer = await imageSharp @@ -29,7 +24,7 @@ async function processAlbumCover(url) { .toBuffer(); const resizedMetadata = await sharp(resizedBuffer).metadata(); - + const extendByX = Math.round((1920 - resizedMetadata.width) / 2); const extendByY = Math.round((1080 - resizedMetadata.height) / 2); @@ -43,10 +38,12 @@ async function processAlbumCover(url) { }) .toBuffer(); - return outputBuffer; + console.log('Processed album cover successfully.'); + return { outputBuffer, dominantColor }; } catch (error) { - console.error(`Error processing album cover from URL: ${url}`, error); + console.error(`Failed to process album cover from URL: ${url}`, error); + throw error; } } @@ -58,6 +55,7 @@ async function getDominantColor(imageBuffer) { const greenAvg = channels[1].mean; const blueAvg = channels[2].mean; + console.log(`Determined dominant color: r=${Math.round(redAvg)}, g=${Math.round(greenAvg)}, b=${Math.round(blueAvg)}`); return { r: Math.round(redAvg), g: Math.round(greenAvg), @@ -65,34 +63,46 @@ async function getDominantColor(imageBuffer) { }; } -async function setAsWallpaper(buffer) { +async function setAsWallpaper(buffer, dominantColor) { try { - await fs.promises.writeFile('/tmp/current_album_cover.png', buffer); - exec('feh --bg-center /tmp/current_album_cover.png'); - console.log("Wallpaper set using feh."); + await fs.promises.writeFile('/tmp/current_album_cover.png', buffer, 'binary'); + + const colorString = `#${dominantColor.r.toString(16).padStart(2, '0')}${dominantColor.g.toString(16).padStart(2, '0')}${dominantColor.b.toString(16).padStart(2, '0')}`; + + console.log(`Sending color string to Python script: ${colorString}`); + + const command = `python3 ./tap_in.py '${colorString}'`; + console.log("Running command:", command); + + exec(command, (error, stdout, stderr) => { + if (error) { + console.error(`Error running the command: ${error}`); + } + console.log(`Python stdout: ${stdout}`); + console.log(`Python stderr: ${stderr}`); + }); + + exec('feh --bg-center /tmp/current_album_cover.png'); + + console.log('Wallpaper and Qtile colors successfully updated.'); } catch (error) { - console.error("Error setting wallpaper:", error); + console.error("Failed to set wallpaper and update Qtile colors:", error); + throw error; } } async function fetchCurrentScrobble(user) { - let lastTrackName; - let lastArtist; try { + console.log("Fetching current scrobble..."); + const optionsGetTrack = { uri: `http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${user}&api_key=${config.apiKey}&format=json&limit=1`, json: true }; const lastTrack = await rp(optionsGetTrack); - - if (!lastTrack.recenttracks || !lastTrack.recenttracks.track || !lastTrack.recenttracks.track[0]) { - console.error("No valid track data in recenttracks"); - return null; - } - - lastArtist = lastTrack.recenttracks.track[0].artist["#text"]; - lastTrackName = lastTrack.recenttracks.track[0].name; + const lastArtist = lastTrack.recenttracks.track[0].artist["#text"]; + const lastTrackName = lastTrack.recenttracks.track[0].name; const images = lastTrack.recenttracks.track[0].image; @@ -103,28 +113,32 @@ async function fetchCurrentScrobble(user) { break; } } - - if (coverURL) { - coverURL = coverURL.replace('300x300', '1000x1000'); - } if (coverURL) { - const processedCover = await processAlbumCover(coverURL); - await setAsWallpaper(processedCover); - console.log("Wallpaper updated to album cover of: " + lastTrackName); + const { outputBuffer, dominantColor } = await processAlbumCover(coverURL); + await setAsWallpaper(outputBuffer, dominantColor); + console.log("Successfully fetched current scrobble."); + return { outputBuffer, dominantColor }; } else { console.error(`Cover URL not found for track: ${lastTrackName}`); + throw new Error('Cover URL not found'); } } catch (error) { - console.error(`Failed to fetch current scrobble`, error); + console.error(`Failed to fetch current scrobble:`, error); + throw error; } } function startFetching() { setInterval(async () => { - await fetchCurrentScrobble(config.username); + try { + console.log('Initiating fetch sequence.'); + await fetchCurrentScrobble(config.username); + console.log('Fetch sequence completed.'); + } catch (error) { + console.error(`Failed in startFetching:`, error); + } }, updateInterval); } startFetching(); - diff --git a/package.json b/package.json index adaee65..ad9e8f6 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "axios": "^1.5.1", "file-type": "^18.5.0", "get-image-colors": "^4.0.1", + "pm2": "^5.3.0", "request-promise": "^4.2.6", "sharp": "^0.32.6" } diff --git a/tap_in.py b/tap_in.py new file mode 100644 index 0000000..bc7e054 --- /dev/null +++ b/tap_in.py @@ -0,0 +1,23 @@ +import sys +import time +import pyautogui + +print("Arguments received:", sys.argv) + +def write_color_to_file(color): + with open("/tmp/bar_color.txt", "w") as f: + f.write(color) + print(f"Successfully wrote color {color} to /tmp/bar_color.txt") + +def simulate_f7_keypress(): + time.sleep(1) # Give a little time for the file to be written + pyautogui.press('f7') + print("Simulated F7 keypress") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python tap_in.py ") + else: + color = sys.argv[1] + write_color_to_file(color) + simulate_f7_keypress()