multi-scrobbler-now-playing/index.js

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-01-10 21:26:15 +00:00
import fetch from 'node-fetch';
2025-01-11 23:37:07 +00:00
import { TelegramClient, Api as TgApi } from 'telegram';
import { StringSession } from 'telegram/sessions/index.js';
import 'dotenv/config';
2025-01-11 23:37:07 +00:00
import prompt from 'prompt';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2025-01-10 21:26:15 +00:00
2025-01-15 21:10:23 +00:00
const mdBaseUrl = process.env.MD_BASE_URL;
2025-01-11 23:37:07 +00:00
const apiId = parseInt(process.env.API_ID);
const apiHash = process.env.API_HASH;
2025-01-11 23:37:07 +00:00
const songPrefix = process.env.SONG_PREFIX || "";
const sessionFile = path.join(__dirname, 'tg_session', 'session');
let sessionStore = new StringSession("");
try {
const readSession = await fs.readFile(sessionFile, "utf8");
if (readSession) {
sessionStore = new StringSession(readSession);
}
} catch (e) {
console.error(e);
}
async function getCurrentSong() {
2025-01-15 21:10:23 +00:00
return fetch(mdBaseUrl + '/api/status').then((data) => {
2025-01-10 21:26:15 +00:00
return data.json();
}).then(data => {
const players = [];
// get all players of all sources
data.sources.forEach(source => {
if (!source.players) return;
const sourcePlayers = Object.values(source.players);
sourcePlayers.forEach(player => {
players.push(player);
});
});
// sort players in order of most recent new song
const sortedPlayers = players.sort((a, b) => new Date(a.playFirstSeenAt).getTime() - new Date(b.playFirstSeenAt).getTime());
// default string
2025-01-15 01:52:54 +00:00
let nowPlaying = "Nothing Playing";
2025-01-10 21:26:15 +00:00
// if a player exists, get it's song
if (sortedPlayers.length !== 0) {
const playData = sortedPlayers[0]?.play?.data;
if (!playData) return "";
2025-01-10 21:26:15 +00:00
nowPlaying = `${playData.artists.join(', ')} - ${playData.track}`;
}
return nowPlaying;
});
}
async function initTelegram() {
2025-01-11 23:37:07 +00:00
// check if we have what we need for telegram
if (!apiId) { console.error("missing env var API_ID"); return; };
if (!apiHash) { console.error("missing env var API_HASH"); return; };
2025-01-11 23:37:07 +00:00
// setup client
const client = new TelegramClient(sessionStore, apiId, apiHash, {
connectionRetries: 5
});
2025-01-11 23:37:07 +00:00
prompt.start();
// start it, prompt for auth if needed
await client.start({
2025-01-11 23:37:07 +00:00
phoneNumber: async () => {
const res = await prompt.get({ description: 'phone number', type: 'string', required: true, hidden: false });
return res.question;
},
password: async () => {
const res = await prompt.get({ description: 'password', type: 'string', required: true, hidden: true });
return res.question;
},
phoneCode: async () => {
const res = await prompt.get({ description: 'phone code', type: 'string', required: true, hidden: false });
return res.question;
},
onError: (err) => console.error(err)
});
2025-01-11 23:37:07 +00:00
// save the session so we don't have to log in every time
client.session.save();
await fs.writeFile(sessionFile, sessionStore.save());
2025-01-11 23:37:07 +00:00
return client;
}
2025-01-10 21:26:15 +00:00
2025-01-11 23:37:07 +00:00
// store client here
const tg = await initTelegram();
// do we actually have a client?
if (!!tg) {
let song = "";
setInterval(async () => {
const newSong = await getCurrentSong();
if (newSong === song) return; // don't do API calls if the song is the same as it was
song = newSong;
console.log(`new song: ${song}`);
// update bio with new song
tg.invoke(new TgApi.account.UpdateProfile({
about: songPrefix + song
}));
}, 5000);
}