mirror of
https://github.com/He4eT/fuzzsoma.git
synced 2026-05-04 17:27:23 +00:00
index.js: extract steps
This commit is contained in:
parent
b6f3c4c048
commit
36b8e962e2
1 changed files with 20 additions and 12 deletions
32
index.js
32
index.js
|
|
@ -6,7 +6,7 @@ inquirer.registerPrompt('search-list', require('inquirer-search-list'))
|
|||
|
||||
const channelsUrl = 'https://somafm.com/channels.xml'
|
||||
|
||||
const download = (url) =>
|
||||
const downloadChannelsXML = (url) =>
|
||||
fetch(url)
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
|
|
@ -16,29 +16,37 @@ const download = (url) =>
|
|||
}
|
||||
})
|
||||
|
||||
const extractChannels = (json) =>
|
||||
json.channels.channel
|
||||
|
||||
const fuzzySelect = (choices) =>
|
||||
inquirer.prompt([{
|
||||
type: "search-list",
|
||||
message: "Select SomaFM channel",
|
||||
name: "channel",
|
||||
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)
|
||||
}
|
||||
|
||||
download(channelsUrl)
|
||||
downloadChannelsXML(channelsUrl)
|
||||
.then(xml2js.parseStringPromise)
|
||||
.then((json) => json.channels.channel)
|
||||
.then((channels) => channels.sort((a, b) =>
|
||||
b.listeners[0] - a.listeners[0]))
|
||||
.then((channels) => channels.map((channel) => ({
|
||||
name: `${channel.title[0]} — ${channel.description[0]}`,
|
||||
value: channel.highestpls[0]['_'],
|
||||
})))
|
||||
.then(extractChannels)
|
||||
.then(sortChannels)
|
||||
.then(shapeChannels)
|
||||
.then(fuzzySelect)
|
||||
.then(({channel}) => channel)
|
||||
.then(playChannel)
|
||||
.catch((error) => console.error('Error:', error.message))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue