Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Human readable name for links #1

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
require('dotenv').config();
const Telegraf = require('telegraf');
const getData = require('./getData');
const parseURL = require('./parseURL');
const validator = require('validator');

const readableNames = {
yandex: 'Yandex',
spotify: 'Spotify',
appleMusic: 'Apple Music',
youtubeMusic: 'YouTube Music',
youtube: 'YouTube',
pandora: 'Pandora',
google: 'Google',
deezer: 'Deezer',
tidal: 'Tidal',
napster: 'Napster',
fanburst: 'Fanburst',
};
const bot = new Telegraf(process.env.TOKEN);
bot.start((ctx) => {
ctx.reply('👋 Привет!\n\nПоделись со мной ссылкой на трек или альбом из любого приложения, а я в ответ пришлю ссылки, на все музыкальные сервисы где можно найти этот альбом или композицию.')
Expand All @@ -14,19 +27,30 @@ bot.hears('тотален', (ctx) => ctx.reply('100% пидор'));

bot.on('message', async (ctx) => {
const message = ctx.message.text;

const urls = parseURL(message);
if (message) {
try {
if (validator.isURL(message)) {
if (validator.isURL(urls[0])) {
const sendLinks = async () => {
ctx.reply('🚬 Подождите немного, пока я ищу ссылки...');
const data = await getData({ link: message });
const data = await getData({link: urls[0]});
if (data) {
let links = '';
data.songlink.links.listen.sort((a, b) => {
const nameA = readableNames[a.name] || a.name;
const nameB = readableNames[b.name] || b.name;
if (nameA > nameB) {
return 1;
} else if (nameA < nameB) {
return -1;
}
return 0;
});
data.songlink.links.listen.forEach(item => {
links = `${links}\n${item.name}\n${item.data.listenUrl}\n`;
const name = readableNames[item.name] || item.name;
links = `${links}\n[${name}](${item.data.listenUrl})\n`;
});
ctx.reply(links);
ctx.reply(links, {parse_mode: 'markdown'});
ctx.reply('👋 Готово!');
} else {
ctx.reply('😣 Кажется у меня нет данных по этой ссылке. Убедись, что адрес верный.');
Expand Down
4 changes: 4 additions & 0 deletions parseURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function(text) {
const regexp = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/g
return text.match(regexp) || ['']
}