Change bar colors with album cover in the most retarded way possible

This commit is contained in:
Wizzard 2023-10-16 20:21:13 -04:00
parent 79dcfa7b83
commit 62285d572b
4 changed files with 76 additions and 36 deletions

View File

@ -1 +1,3 @@
npm install axios sharp get-image-colors pm2
pip3 install libqtile --break-system-packages
pip3 install pyautogui --break-system-packages

76
main.js
View File

@ -1,7 +1,6 @@
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');
@ -18,10 +17,6 @@ async function processAlbumCover(url) {
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
@ -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);
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 set using feh.");
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;
@ -105,26 +115,30 @@ async function fetchCurrentScrobble(user) {
}
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 () => {
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();

View File

@ -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"
}

23
tap_in.py Normal file
View File

@ -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 <color>")
else:
color = sys.argv[1]
write_color_to_file(color)
simulate_f7_keypress()