mirror of
https://github.com/He4eT/fuzzsoma.git
synced 2026-05-05 01:37:22 +00:00
index.js: cleanup
This commit is contained in:
parent
0c770d7e61
commit
8d95e2ad13
1 changed files with 23 additions and 18 deletions
41
index.js
41
index.js
|
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const { exec } = require('child_process')
|
||||||
const inquirer = require('inquirer')
|
const inquirer = require('inquirer')
|
||||||
|
const inquirerSearchList = require('inquirer-search-list')
|
||||||
const xml2js = require('xml2js')
|
const xml2js = require('xml2js')
|
||||||
const { exec } = require('child_process');
|
|
||||||
|
|
||||||
const CHANNELS_XML_URL = 'https://somafm.com/channels.xml'
|
const CHANNELS_XML_URL = 'https://somafm.com/channels.xml'
|
||||||
|
|
||||||
|
|
@ -12,27 +13,19 @@ const downloadChannelsXML = (url) =>
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return response.text()
|
return response.text()
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to download XML. Status: ${response.status} ${response.statusText}`)
|
throw new Error([
|
||||||
|
'Failed to download XML:',
|
||||||
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
].join(' '))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const extractChannels = (json) =>
|
const extractChannels = (json) =>
|
||||||
json.channels.channel
|
json.channels.channel
|
||||||
|
|
||||||
const fuzzySelect = (choices) => {
|
|
||||||
inquirer.registerPrompt('search-list', require('inquirer-search-list'))
|
|
||||||
return inquirer.prompt([{
|
|
||||||
type: "search-list",
|
|
||||||
message: "SomaFM channel",
|
|
||||||
name: "channel",
|
|
||||||
choices,
|
|
||||||
}])
|
|
||||||
}
|
|
||||||
|
|
||||||
const sortChannels = (channels) =>
|
const sortChannels = (channels) =>
|
||||||
channels.sort((a, b) =>
|
channels.sort((a, b) => b.listeners[0] - a.listeners[0])
|
||||||
b.listeners[0] - a.listeners[0]
|
|
||||||
)
|
|
||||||
|
|
||||||
const shapeChannels = (channels) =>
|
const shapeChannels = (channels) =>
|
||||||
channels.map((channel) => ({
|
channels.map((channel) => ({
|
||||||
|
|
@ -40,8 +33,20 @@ const shapeChannels = (channels) =>
|
||||||
value: channel.highestpls[0]['_'],
|
value: channel.highestpls[0]['_'],
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const playChannel = ({channel}) =>
|
const fuzzySelect = (choices) =>
|
||||||
exec(`cvlc ${channel}`)
|
inquirer.prompt([{
|
||||||
|
choices,
|
||||||
|
message: 'SomaFM channel',
|
||||||
|
name: 'channelURL',
|
||||||
|
type: 'search-list',
|
||||||
|
}])
|
||||||
|
|
||||||
|
const playChannel = ({channelURL}) =>
|
||||||
|
exec(`cvlc ${channelURL}`)
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
inquirer.registerPrompt('search-list', inquirerSearchList)
|
||||||
|
|
||||||
downloadChannelsXML(CHANNELS_XML_URL)
|
downloadChannelsXML(CHANNELS_XML_URL)
|
||||||
.then(xml2js.parseStringPromise)
|
.then(xml2js.parseStringPromise)
|
||||||
|
|
@ -50,4 +55,4 @@ downloadChannelsXML(CHANNELS_XML_URL)
|
||||||
.then(shapeChannels)
|
.then(shapeChannels)
|
||||||
.then(fuzzySelect)
|
.then(fuzzySelect)
|
||||||
.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