2024-09-25 00:02:41 -03:00
|
|
|
const fs = require('fs');
|
2024-09-07 12:57:20 -03:00
|
|
|
const axios = require('axios');
|
|
|
|
const Config = require('../props/config.json');
|
|
|
|
const { getStrings } = require('../plugins/checklang.js');
|
|
|
|
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
|
|
|
|
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
|
|
|
|
|
2024-09-07 20:48:16 -03:00
|
|
|
const scrobbler_url = 'http://ws.audioscrobbler.com/2.0/';
|
|
|
|
const api_key = Config.lastKey;
|
|
|
|
|
2024-09-25 00:02:41 -03:00
|
|
|
const dbFile = 'props/lastfm.json';
|
|
|
|
let users = {};
|
|
|
|
|
|
|
|
function loadUsers() {
|
|
|
|
if (!fs.existsSync(dbFile)) {
|
|
|
|
console.log(`WARN: Last.fm user database ${dbFile} not found. Creating a new one.`);
|
|
|
|
saveUsers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = fs.readFileSync(dbFile, 'utf-8');
|
|
|
|
users = JSON.parse(data);
|
|
|
|
} catch (err) {
|
|
|
|
console.log("WARN: Error loading the Last.fm user database:", err);
|
|
|
|
users = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveUsers() {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(dbFile, JSON.stringify(users, null, 2), 'utf-8');
|
|
|
|
} catch (err) {
|
|
|
|
console.error("WARN: Error saving Last.fm users:", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:57:20 -03:00
|
|
|
module.exports = (bot) => {
|
2024-09-25 00:02:41 -03:00
|
|
|
loadUsers();
|
|
|
|
|
|
|
|
bot.command('setuser', (ctx) => {
|
|
|
|
const userId = ctx.from.id;
|
|
|
|
const Strings = getStrings(ctx.from.language_code);
|
|
|
|
const lastUser = ctx.message.text.split(' ')[1];
|
|
|
|
|
|
|
|
if (!lastUser) {
|
|
|
|
return ctx.reply(Strings.lastFmNoUser, {
|
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
users[userId] = lastUser;
|
|
|
|
saveUsers();
|
|
|
|
|
|
|
|
const message = Strings.lastFmUserSet.replace('{lastUser}', lastUser);
|
|
|
|
|
|
|
|
ctx.reply(message, {
|
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-09-08 10:18:42 -03:00
|
|
|
bot.command(['lt', 'lmu', 'last', 'lfm'], spamwatchMiddleware, async (ctx) => {
|
2024-09-25 00:02:41 -03:00
|
|
|
const userId = ctx.from.id;
|
2024-09-07 12:57:20 -03:00
|
|
|
const Strings = getStrings(ctx.from.language_code);
|
2024-09-25 00:02:41 -03:00
|
|
|
const lastfmUser = users[userId];
|
2024-09-07 12:57:20 -03:00
|
|
|
|
|
|
|
if (!lastfmUser) {
|
2024-09-25 00:02:41 -03:00
|
|
|
return ctx.reply(Strings.lastFmNoSet, {
|
2024-09-07 12:57:20 -03:00
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2024-09-07 20:48:16 -03:00
|
|
|
const response = await axios.get(scrobbler_url, {
|
2024-09-07 12:57:20 -03:00
|
|
|
params: {
|
|
|
|
method: 'user.getRecentTracks',
|
|
|
|
user: lastfmUser,
|
2024-09-07 20:48:16 -03:00
|
|
|
api_key,
|
2024-09-07 12:57:20 -03:00
|
|
|
format: 'json',
|
|
|
|
limit: 1
|
2024-09-07 13:08:57 -03:00
|
|
|
},
|
|
|
|
headers: {
|
2024-09-07 21:36:26 -03:00
|
|
|
'User-Agent': "lynx-@LynxBR_bot-node-telegram-bot"
|
2024-09-07 12:57:20 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const track = response.data.recenttracks.track[0];
|
|
|
|
|
|
|
|
if (!track) {
|
2024-09-07 13:12:58 -03:00
|
|
|
const noRecent = Strings.lastFmNoRecent.replace('{lastfmUser}', lastfmUser);
|
|
|
|
return ctx.reply(noRecent, {
|
2024-09-07 12:57:20 -03:00
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const trackName = track.name;
|
|
|
|
const artistName = track.artist['#text'];
|
|
|
|
const nowPlaying = track['@attr'] && track['@attr'].nowplaying ? Strings.lastFmListeningNow : Strings.lastFmLastPlayed;
|
|
|
|
|
2024-09-16 22:27:52 -03:00
|
|
|
const imageExtralarge = track.image.find(img => img.size === 'extralarge');
|
|
|
|
const imageMega = track.image.find(img => img.size === 'mega');
|
|
|
|
const imageUrl = (imageExtralarge && imageExtralarge['#text']) || (imageMega && imageMega['#text']) || '';
|
2024-09-25 00:02:41 -03:00
|
|
|
|
2024-09-07 12:57:20 -03:00
|
|
|
const trackUrl = `https://www.last.fm/music/${encodeURIComponent(artistName)}/_/${encodeURIComponent(trackName)}`;
|
|
|
|
const artistUrl = `https://www.last.fm/music/${encodeURIComponent(artistName)}`;
|
|
|
|
const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`;
|
|
|
|
|
2024-09-07 20:48:16 -03:00
|
|
|
let num_plays = '';
|
2024-09-10 17:52:47 -03:00
|
|
|
try {
|
|
|
|
const response_plays = await axios.get(scrobbler_url, {
|
2024-09-07 20:48:16 -03:00
|
|
|
params: {
|
|
|
|
method: 'track.getInfo',
|
|
|
|
api_key,
|
|
|
|
track: trackName,
|
|
|
|
artist: artistName,
|
|
|
|
username: lastfmUser,
|
|
|
|
format: 'json',
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'User-Agent': "lynx-@LynxBR_bot-node-telegram-bot"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
num_plays = response_plays.data.track.userplaycount;
|
2024-09-10 17:52:47 -03:00
|
|
|
} catch (err) {
|
2024-09-07 20:48:16 -03:00
|
|
|
console.log(err)
|
2024-09-10 17:52:47 -03:00
|
|
|
const message = Strings.lastFmErr
|
|
|
|
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
|
|
|
|
.replace("{err}", err);
|
|
|
|
ctx.reply(message, {
|
2024-09-07 20:48:16 -03:00
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
2024-09-25 00:02:41 -03:00
|
|
|
|
2024-09-07 12:57:20 -03:00
|
|
|
const message = Strings.lastFmStatusFor
|
|
|
|
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
|
|
|
|
.replace("{nowPlaying}", nowPlaying)
|
|
|
|
.replace("{trackName}", `[${trackName}](${trackUrl})`)
|
2024-09-07 20:48:16 -03:00
|
|
|
.replace("{artistName}", `[${artistName}](${artistUrl})`)
|
2024-09-10 17:52:47 -03:00
|
|
|
.replace("{plays}", `${num_plays}`);
|
2024-09-07 12:57:20 -03:00
|
|
|
|
|
|
|
if (imageUrl) {
|
|
|
|
ctx.replyWithPhoto(imageUrl, {
|
|
|
|
caption: message,
|
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ctx.reply(message, {
|
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`;
|
|
|
|
const message = Strings.lastFmErr
|
|
|
|
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
|
|
|
|
.replace("{err}", err);
|
|
|
|
ctx.reply(message, {
|
|
|
|
parse_mode: "Markdown",
|
|
|
|
reply_to_message_id: ctx.message.message_id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|