index.js: extract steps

This commit is contained in:
He4eT 2023-12-25 16:20:05 +01:00
commit 36b8e962e2

View file

@ -6,7 +6,7 @@ inquirer.registerPrompt('search-list', require('inquirer-search-list'))
const channelsUrl = 'https://somafm.com/channels.xml' const channelsUrl = 'https://somafm.com/channels.xml'
const download = (url) => const downloadChannelsXML = (url) =>
fetch(url) fetch(url)
.then((response) => { .then((response) => {
if (response.ok) { if (response.ok) {
@ -16,29 +16,37 @@ const download = (url) =>
} }
}) })
const extractChannels = (json) =>
json.channels.channel
const fuzzySelect = (choices) => const fuzzySelect = (choices) =>
inquirer.prompt([{ inquirer.prompt([{
type: "search-list", type: "search-list",
message: "Select SomaFM channel", message: "Select SomaFM channel",
name: "channel", name: "channel",
choices, choices,
pageSize: 10,
}]) }])
const playChannel = (channel) => { const sortChannels = (channels) =>
channels.sort((a, b) =>
b.listeners[0] - a.listeners[0]
)
const shapeChannels = (channels) =>
channels.map((channel) => ({
name: `${channel.title[0]}${channel.description[0]}`,
value: channel.highestpls[0]['_'],
}))
const playChannel = ({channel}) => {
console.log(channel) console.log(channel)
} }
download(channelsUrl) downloadChannelsXML(channelsUrl)
.then(xml2js.parseStringPromise) .then(xml2js.parseStringPromise)
.then((json) => json.channels.channel) .then(extractChannels)
.then((channels) => channels.sort((a, b) => .then(sortChannels)
b.listeners[0] - a.listeners[0])) .then(shapeChannels)
.then((channels) => channels.map((channel) => ({
name: `${channel.title[0]}${channel.description[0]}`,
value: channel.highestpls[0]['_'],
})))
.then(fuzzySelect) .then(fuzzySelect)
.then(({channel}) => channel)
.then(playChannel) .then(playChannel)
.catch((error) => console.error('Error:', error.message)) .catch((error) => console.error('Error:', error.message))