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 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))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue