Add command arguments for manual "scrobbling"

This commit is contained in:
MeowcaTheoRange 2024-02-21 09:13:31 -06:00
parent 7955fc4459
commit e70ce4a9ca
2 changed files with 29 additions and 5 deletions

View file

@ -8,6 +8,7 @@ const { createClient } = require("fix-esm").require("webdav");
const path = require("path"); const path = require("path");
const { readFileSync, writeFileSync } = require("fs"); const { readFileSync, writeFileSync } = require("fs");
const io = require('socket.io-client'); const io = require('socket.io-client');
const { program } = require('commander');
function pathGenerator({ url, name, channelName }) { function pathGenerator({ url, name, channelName }) {
return path.join(process.env.NEXTCLOUD_FOLDER, `${url}-${skewered(`${name} - ${channelName}`)}.mp3`) return path.join(process.env.NEXTCLOUD_FOLDER, `${url}-${skewered(`${name} - ${channelName}`)}.mp3`)
@ -25,10 +26,21 @@ function writeLastSong({ name, channelName }) {
return null; return null;
} }
async function getVideo() { async function getVideo({ msName, msArtist }) {
const trackData = await fetch( let trackData;
`https://${process.env.LASTFM_INSTANCE}/2.0/?method=user.getrecenttracks&user=${process.env.LASTFM_USERNAME}&api_key=${process.env.LASTFM_API_KEY}&format=json&limit=1&extended=1`
).then(x => x.json()).then(data => data.recenttracks.track[0]); if (msName != null && msArtist != null) {
trackData = {
name: msName,
artist: {
name: msArtist
}
};
} else {
trackData = await fetch(
`https://${process.env.LASTFM_INSTANCE}/2.0/?method=user.getrecenttracks&user=${process.env.LASTFM_USERNAME}&api_key=${process.env.LASTFM_API_KEY}&format=json&limit=1&extended=1`
).then(x => x.json()).then(data => data.recenttracks.track[0]);
}
if (checkLastSong({ if (checkLastSong({
name: trackData.name, name: trackData.name,
@ -152,13 +164,24 @@ async function uploadToNextcloud({ fileStream, url, name, channelName }, nextclo
} }
async function main() { async function main() {
program
.option('-s, --song <value>', 'Look up a song', null)
.option('-a, --artist <value>', 'Look up an artist', null);
program.parse(process.argv);
const options = program.opts();
const socket = io.connect(`http://localhost:${process.env.NOTIFICATION_SERVER_PORT}`, {reconnect: true}); const socket = io.connect(`http://localhost:${process.env.NOTIFICATION_SERVER_PORT}`, {reconnect: true});
socket.on('connect', function (s) { socket.on('connect', function (s) {
console.log('Successfully connected to notification server'); console.log('Successfully connected to notification server');
}); });
const video = await getVideo(); const video = await getVideo({
msName: options.song,
msArtist: options.artist
});
console.log(video); console.log(video);
if (video == null) return dismantle(socket); if (video == null) return dismantle(socket);

View file

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"commander": "^12.0.0",
"dotenv": "^16.4.4", "dotenv": "^16.4.4",
"express": "^4.18.2", "express": "^4.18.2",
"fix-esm": "^1.0.1", "fix-esm": "^1.0.1",