Skip to content

Commit

Permalink
🎄
Browse files Browse the repository at this point in the history
  • Loading branch information
mezotv committed Jan 8, 2023
1 parent 705f168 commit 097c498
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 51 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"discord-canva"
],
"author": "Dominik",
"license": "MIT"
"license": "MIT",
"dependencies": {
"@napi-rs/canvas": "^0.1.32"
}
}
Binary file added src/fonts/AvenirNextLTPro-Bold.otf
Binary file not shown.
Binary file added src/fonts/AvenirNextLTPro-Regular.otf
Binary file not shown.
8 changes: 2 additions & 6 deletions src/functions/fetchSpotifyColor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const canvas = require("@napi-rs/canvas");

const getMiddleColor = async (imagePath) => {
async function getSpotifyColor(imagePath) {

const canvasObject = canvas.createCanvas(640, 640);
const ctx = canvasObject.getContext('2d');
Expand All @@ -23,12 +23,8 @@ const getMiddleColor = async (imagePath) => {
const g2 = data2[1];
const b2 = data2[2];
let color2 = `#${r2.toString(16).padStart(2, '0')}${g2.toString(16).padStart(2, '0')}${b2.toString(16).padStart(2, '0')}`;


if(isLight(color)) { color = "#0F0F0F" }
if(isLight(color2)) { color2 = "#0F0F0F" }

return [color, color2];
}

export default getMiddleColor;
module.exports = {getSpotifyColor};
9 changes: 4 additions & 5 deletions src/functions/isLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const isLight = (color) => {
color_match = color.match(
/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
);
r = color_match[1];
g = color_match[2];
b = color_match[3];
r = +color_match[1];
g = +color_match[2];
b = +color_match[3];
} else {
color_match = +(
"0x" + color.slice(1).replace(color.length < 5 && /./g, "$&$&")
Expand All @@ -19,5 +19,4 @@ const isLight = (color) => {

return hsp > 127.5;
};

export default isLight;
module.exports = {isLight};
156 changes: 117 additions & 39 deletions src/plugins/Spotify.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,134 @@
const { isLight } = require("../functions/isLight");
const { getSpotifyColor } = require("../functions/fetchSpotifyColor");
const canvas = require("@napi-rs/canvas");
const { join } = require('path')

class Spotify {
constructor(song, artist, album, duration, albumArt) {
this.song = song;
this.artist = artist;
this.album = album;
this.duration = duration;
this.albumArt = albumArt;
constructor(song, artist, album, duration, albumArt) {
this.song = song;
this.artist = artist;
this.album = album;
this.duration = duration;
this.albumArt = albumArt;

if(typeof this.song === "undefined" || typeof this.song === "null") {
throw new Error("Spotify Song can not be an undefined or null!");
}
if (typeof this.song === "undefined" || this.song === null) {
throw new Error("Spotify Song can not be an undefined or null!");
}

if(typeof this.song !== "string") {
throw new TypeError("Spotify Song needs to be a string!");
}
if (typeof this.song !== "string") {
throw new TypeError("Spotify Song needs to be a string!");
}

if(typeof this.artist === "undefined" || typeof this.artist === "null") {
throw new Error("Spotify Artist can not be an undefined or null!");
}
if (typeof this.artist === "undefined" || this.artist === null) {
throw new Error("Spotify Artist can not be an undefined or null!");
}

if(typeof this.artist !== "string") {
throw new TypeError("Spotify Artist needs to be a string!");
}
if (typeof this.artist !== "string") {
throw new TypeError("Spotify Artist needs to be a string!");
}

if(typeof this.album === "undefined" || typeof this.album === "null") {
throw new Error("Spotify Album can not be an undefined or null!");
}
if (typeof this.album === "undefined" || this.album === null) {
throw new Error("Spotify Album can not be an undefined or null!");
}

if(typeof this.album !== "string") {
throw new TypeError("Spotify Album needs to be a string!");
}
if (typeof this.album !== "string") {
throw new TypeError("Spotify Album needs to be a string!");
}

if(typeof this.duration === "undefined" || typeof this.duration === "null") {
throw new Error("Spotify Song can not be an undefined or null!");
}
if (typeof this.duration === "undefined" || this.duration === null) {
throw new Error("Spotify Song can not be an undefined or null!");
}

if(typeof this.duration !== "number") {
throw new TypeError("Spotify Song needs to be a number!");
}
if (typeof this.duration !== "number") {
throw new TypeError("Spotify Song needs to be a number!");
}

if(typeof this.albumArt === "undefined" || typeof this.albumArt === "null") {
throw new Error("Spotify Album Art can not be an undefined or null!");
}
if (typeof this.albumArt === "undefined" || this.albumArt === null) {
throw new Error("Spotify Album Art can not be an undefined or null!");
}

if(typeof this.albumArt !== "string") {
throw new TypeError("Spotify Album Art needs to be a string!");
}
if (typeof this.albumArt !== "string") {
throw new TypeError("Spotify Album Art needs to be a string!");
}

// add a tiny async function

this.generate = async () => {

console.log(albumArt)

const color = await getSpotifyColor(albumArt)

if(isLight(color[0])) { color[0] = "#0F0F0F" }
if(isLight(color[1])) { color[1] = "#0F0F0F" }

console.log(color)
canvas.GlobalFonts.registerFromPath(join(__dirname, '..', '/', 'fonts', 'AvenirNextLTPro-Bold.otf'), 'FontBold')
canvas.GlobalFonts.registerFromPath(join(__dirname, '..', '/', 'fonts', 'AvenirNextLTPro-Regular.otf'), 'FontRegular')
const canvasObject = canvas.createCanvas(428, 926);
const ctx = canvasObject.getContext('2d');

const gradient = ctx.createLinearGradient(0, 0, 428, 926);

gradient.addColorStop(0, color[0]);
gradient.addColorStop(1, color[1]);

ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 428, 926);

// draw the album art image onto the canvas

const logo = canvas.loadImage(albumArt);
ctx.drawImage(logo, 26, 160, 380, 380);

ctx.font = '14px FontBold'
ctx.fillStyle = "#ffffff"
ctx.textAlign = "center"; // center the text horizontally
ctx.textBaseline = "top"; // align the text to the top of the canvas
ctx.fillText(album, 214, 55)

let cutstring;
if (song.length > 33) { cutstring = song.substring(0, 33) + '...' } else { cutstring = song }
ctx.font = '22px FontBold'
ctx.fillStyle = "#ffffff"
ctx.textAlign = "left"; // center the text horizontally
ctx.textBaseline = "top"; // align the text to the top of the canvas
ctx.fillText(cutstring, 26, 607)

ctx.font = '16px FontBold'
ctx.fillStyle = "#B3B3B3"
ctx.textAlign = "left"; // center the text horizontally
ctx.textBaseline = "top"; // align the text to the top of the canvas
ctx.fillText(artist, 26, 641)

let time = Math.round(duration / 60)
time = time.toLocaleString().replace(",", ":")
time = time.substring(0, time.length - 1)

ctx.font = '10px FontBold'
ctx.fillStyle = "#B3B3B3"
ctx.textAlign = "left"; // center the text horizontally
ctx.textBaseline = "top"; // align the text to the top of the canvas
ctx.fillText(time, 382, 694)

ctx.font = '10px FontBold'
ctx.fillStyle = "#B3B3B3"
ctx.textAlign = "left"; // center the text horizontally
ctx.textBaseline = "top"; // align the text to the top of the canvas
ctx.fillText("0:00", 20, 694)

const image = canvas.loadImage("https://cdn.discordapp.com/attachments/1047187283234795580/1053677610367975624/Track_View.png");
ctx.drawImage(image, 0, 0, 428, 926);
}
this.generate()
}
}

module.exports = Spotify;


const spotify = new Spotify("albumart", "song", "song", 12, "song");

console.log(spotify)
const spotify = new Spotify("albumart", "song", "song", 12999, "https://cdn.discordapp.com/attachments/1007751044463345784/1053673397319630878/spotify-card.png");

module.exports = Spotify;

console.log(spotify);
63 changes: 63 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@napi-rs/canvas-android-arm64@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-android-arm64/-/canvas-android-arm64-0.1.32.tgz#003128ced4fe570c29a9601b88131c729dc9cb78"
integrity sha512-WvfDvaJCQlMA4eUZnAEmn6efrtyD7qraBK0pq4A+GQPKccFUO16lxnLlq/CKvlKmphYjxGN5CB+Gfd4ZKWEfzQ==

"@napi-rs/canvas-darwin-arm64@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-darwin-arm64/-/canvas-darwin-arm64-0.1.32.tgz#e24b13a327379c1b3c8677d0f1ece6ef65e8abaf"
integrity sha512-Fm6U9IcXHin6hipLyFlcEGyFFBhrjjjqdspmftNBSBl9hkJQPHtPjSboaT33YZBNH/2gfjiH36oBafr7ZR+Ozg==

"@napi-rs/canvas-darwin-x64@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-darwin-x64/-/canvas-darwin-x64-0.1.32.tgz#0fa2bb0c13406e7770bcbcc3626b5b212e37cd28"
integrity sha512-DMQl4VCuIQCoURWzkXQA82BELsmJjkghGbO/AOOUNrFjk03VBkEzwTVz/mHEuBV8QN6mGOMmfKeCIGYo/QE9yg==

"@napi-rs/canvas-linux-arm-gnueabihf@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-linux-arm-gnueabihf/-/canvas-linux-arm-gnueabihf-0.1.32.tgz#fbf9096a856a7ff4125c8ded2fc94772173525ea"
integrity sha512-eZ/g6i09AYI+t2eyJKSbPwPCan65NmHZONJxXU/t6BAptdVqq9FosrEwOcpBVldkU1IO4nLO5C7fCkOJQzKrBw==

"@napi-rs/canvas-linux-arm64-gnu@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-linux-arm64-gnu/-/canvas-linux-arm64-gnu-0.1.32.tgz#570d48b0a571ead116583ef32089f77672a058fb"
integrity sha512-q4cnQHMj/tjl/8nzxYcrbAgM4JB80xRD+Frfysv75D3SUBe+B1a3+YWHmWZVqLPiiavv0n9GcEoh1XdjFf/URw==

"@napi-rs/canvas-linux-arm64-musl@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-linux-arm64-musl/-/canvas-linux-arm64-musl-0.1.32.tgz#677c31a2ae39d2b8ac121ace6f41eeaaf6cac351"
integrity sha512-VOARylcuDcmIQpVO39xbnnzetPqubAh8s8GG/0frzhs8vERB5Wgh59cp5Nr23MKtdWLwzgzcVVyah4v4IopCYQ==

"@napi-rs/canvas-linux-x64-gnu@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-linux-x64-gnu/-/canvas-linux-x64-gnu-0.1.32.tgz#765cbdc4356fbbba9246b6e5f6af417d9fd0b8fc"
integrity sha512-G90Bdszk+U/7SlvrVSj2IXn87PHWTbLjTN2J+9hoyxU/b5lEr4Oe4aB0Cs3K5m6SVTvjf9GzRU0SceJzJlI/3g==

"@napi-rs/canvas-linux-x64-musl@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-linux-x64-musl/-/canvas-linux-x64-musl-0.1.32.tgz#db24fde1e49e1974d2747060bf80fb4d4fbb786a"
integrity sha512-ZDvqQA+z1CJbAhujOJT5qc9iwo1z9o3G7Rho8ZfzUwSvgyC0YqbCDM4tzf7iRQzg07caxRZq3kAnDrR/MgkGRg==

"@napi-rs/canvas-win32-x64-msvc@0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas-win32-x64-msvc/-/canvas-win32-x64-msvc-0.1.32.tgz#fb02d2e80330d2a80a4c9a7a75fc915623e8d41a"
integrity sha512-WxI8EokD+hIlcw1DDXN+88u+vsBtAeuy08/7Z1JNSu8cLi59ogTy9bfXI2796hW3x8WSF51Bqd8SUJ9lluQUuA==

"@napi-rs/canvas@^0.1.32":
version "0.1.32"
resolved "https://registry.yarnpkg.com/@napi-rs/canvas/-/canvas-0.1.32.tgz#13a23184f8a5aaadaf47ae2242726587b2bb2bcc"
integrity sha512-mStiUFKNL/Hs3FuD84lfnVlIoIWvQ60ubDoJ6FY0rFsXSYWSxp3KVXwVe+7e41rYxj6J7U2/vPL5bUoCWxO4bQ==
optionalDependencies:
"@napi-rs/canvas-android-arm64" "0.1.32"
"@napi-rs/canvas-darwin-arm64" "0.1.32"
"@napi-rs/canvas-darwin-x64" "0.1.32"
"@napi-rs/canvas-linux-arm-gnueabihf" "0.1.32"
"@napi-rs/canvas-linux-arm64-gnu" "0.1.32"
"@napi-rs/canvas-linux-arm64-musl" "0.1.32"
"@napi-rs/canvas-linux-x64-gnu" "0.1.32"
"@napi-rs/canvas-linux-x64-musl" "0.1.32"
"@napi-rs/canvas-win32-x64-msvc" "0.1.32"

0 comments on commit 097c498

Please sign in to comment.