multi-scrobbler-now-playing/index.js

108 lines
2.8 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 { StoreSession } from 'telegram/sessions/index.js';
import 'dotenv/config';
2025-01-11 23:37:07 +00:00
import prompt from 'prompt';
2025-01-10 21:26:15 +00:00
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 || "";
2025-01-11 23:37:07 +00:00
console.log(process.env);
const sessionStore = new StoreSession("tg_session");
async function getCurrentSong() {
2025-01-10 21:26:15 +00:00
return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => {
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
let nowPlaying = "";
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;
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();
return client;
}
2025-01-10 21:26:15 +00:00
2025-01-11 23:37:07 +00:00
prompt.start();
// 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);
}