A few days ago I wrote a simple music player and received some feedback from you. The biggest problem is always the failure of resources, for this point yesterday I carried out a rewrite, also put some knowledge points and experience in the process of refactoring down to share with you. How did not write an article, sentence impassability or wrong place also asks everybody to point out, I correct a lot. The project address
Let’s get down to business
There are already many web music players based on binaryify’s netease Cloud API. The interface is beautiful and the experience is good, but it’s still official when you’re using it.
Write the original intent of the project because suddenly want to listen to jay Chou’s songs, and then go to search on the net, listen to all want to charge, then the oil in the monkey market turned over to a can listen to songs for free online tools, this time also prefer to use the node, so when fishing, all with the puppeteer jay Chou songs address down, Then it was written as a simple web player. I also got a star on Github for the first time in my life. At the same time, I also received a lot of feedback, among which the most serious problem was the song failure.
Resolve the failure problem for the first time
When searching for the cause of failure, I found a time-sensitive key in the address of the sorted songs, which should be to prevent people like me from doing the processing. The solution was that I wrote a scheduled task to pull the list again every three hours. I also redirected the GitHub experience address to my blog, using my own server resources. My server configuration is very low, 3 hours is also to save some resources. However, the problem of interval failures still occurs.
Solve the failure problem for the second time
This time I changed my mind, since access to the source address is time-sensitive, so I simply downloaded all the songs, once and for all. How do you download it? It’s not manual. Manual is soulless. Then again, node uses the Request module of Node. So here’s the trivia: download 394 files at the same time? Certainly not ah, so manual implementation of a download queue function, you can customize the number of tasks downloaded at the same time. The function has been written, source code or articles. . After downloading, another question came, where should I save it? Put it on your own server? That’s not gonna happen. So I chose seven niuyun to do storage. Get a small knowledge point, file upload seven niuyun. At this point, no longer need to worry about the problem of song connection failure, unless the record company accused me of infringement, I gave all deleted…
Questions about UI and functionality additions
This project is very simple, mainly node knowledge application. The front end is a simple HTML page. The UI is also ugly. You can modify the extension yourself if you are interested.
A little slot point
There are a lot of things I didn’t add to my Node code; Do not copy me, when writing node had better; Combined with ha. Especially after require()
The first time to write so many words, my expression ability is very poor, if you do not understand what I said, you can go to GitHub directly view the source code. That’s it. That’s it. That’s it. In addition, I recruit front-end development here, starting with 1 year of experience. Don’t practice. The coordinates are Langfang, Hebei. My wechat is at the end of the article.
A download queue function
Two days ago, I wrote a small project to play all songs of Jay Chou online, because the song address is always invalid, so I plan to download all the songs. Put it in seven cows or other clouds for storage. A total of 394 pieces of data, downloading at the same time will not work, so I wrote a manual download method, only allowed to download a maximum of 5 songs at a time. Finally encapsulated into a general method and shared with everyone.
@param {Array} list - List of downloads ** state: not downloaded; Loading: Downloading; Done: Download completed * */
function downloadQueue(list, max) {
for (let i of list) {
i.state = 'not'
}
// Whether the download is complete
let allDone = false
const down = (a)= > {
let noIndex = null
let notDownload = null
for (let i = 0; i < list.length; i++) {
const item = list[i]
if (item.state === 'not') {
notDownload = item
noIndex = i
break}}if(! notDownload) {console.log('All downloaded')
allDone = true
return
}
const downLoadings = list.filter(item= > item.state === 'loading')
if (downLoadings.length >= max) {
return
}
notDownload.state = 'loading'
console.log(Download the first `${noIndex}A `)
down()
setTimeout((a)= > {
console.log(Download the first `${noIndex}A complete `)
console.log(` remaining${list.length}A `)
list[noIndex].state = 'done'
if (allDone) {
return
}
down()
}, 300)
}
down()
}
Copy the code