📜 ⬆️ ⬇️

Adding Last.fm radio to MPD playlist

Hello, Habramensh,

IchBin's. It all started with the fact that support for last.fm radio was so clumsily implemented in MPD that I even stopped compiling this function. Its sloppyness was that having tuned in to the channel, you still had to manually add tracks to the playlist, because the server issues five tracks per request. Therefore, after all the tracks are lost, you need to repeat the request and get the next 5 tracks! Then we will discuss the scripts that would automate this process.

Bash.fm - Aship work


An original idea was found here . Scripts on the link have long been outdated and did not work. In addition, the author of the scripts has a very peculiar style and has managed to add unnecessary dependencies, for example, on lynx. Therefore, I had to change most of it, although the logic of the work remained the same:


Let's start with fmstart:
root@Buffalo:~# cat /mnt/sd/bin/fmstart #!/bin/sh username=$(sed -nr 's/^username=(.*)/\1/p' ~/.bashfm) password=$(sed -nr 's/^password=([^[:space:]]*).*/\1/p' ~/.bashfm) mediaplayer=$(sed -nr 's/^mediaplayer=(.*)/\1/p' ~/.bashfm) if [ ! -z "$password" ]; then passwordmd5=$(echo -n $password | md5sum | sed -nr 's/([^[:space:]]*).*/\1/p') else passwordmd5=$(sed -nr 's/^passwordmd5=([^[:space:]]*).*/\1/p' ~/.bashfm) fi echo 'username='$username > ~/.bashfm echo 'password=' >> ~/.bashfm echo 'mediaplayer='"$mediaplayer" >> ~/.bashfm echo 'passwordmd5='$passwordmd5 >> ~/.bashfm login_url='http://ws.audioscrobbler.com/radio/handshake.php?' login_url="$login_url"'version=1.1.1&platform=linux&username=' login_url="$login_url"$username'&passwordmd5='$passwordmd5 login_url="$login_url"'&debug=0&partner=' echo "login_url=$login_url" >> ~/.bashfm wget -q -O - "$login_url" >> ~/.bashfm echo >> ~/.bashfm 

For the script to work properly, you must first create a ~ / .bashfm file with the following content:
 root@Buffalo:~# cat ~/.bashfm username=ichbins password=123456 mediaplayer=/mnt/sd/bin/mpc add 

here username and password are your username and password with last.fm , and mediaplayer is the command with which the tracks are added to the playlist.
When you first start fmstart, the password will be deleted and replaced with the md5 amount.
')
A small digression:
MPC is the standard command line MPD client. But I do not compile it, but I use the script in one line:
 root@Buffalo:~# cat /mnt/sd/bin/mpc #!/bin/sh echo "$*" | nc localhost 6600 

This script is not a complete analog of MPC, but for my needs it was always enough.

Now about the channel selection scripts. In fact, they are all the same, only the channel URL inside is different. I listen to either a personal radio or the radio of a particular artist - so I’ll only give these two scripts below.

Script to activate the personal radio channel:
 root@Buffalo:~# cat /mnt/sd/bin/fmpersonal #!/bin/sh # # "fmpersonal" plays user's personal radio station # session=$(sed -nr 's/^session=(.*)/\1/p' ~/.bashfm) username=$(sed -nr 's/^username=(.*)/\1/p' ~/.bashfm) tuning_url='http://ws.audioscrobbler.com/radio/adjust.php?' tuning_url="${tuning_url}session=${session}&url=lastfm://user/" tuning_url="${tuning_url}${username}/personal" #echo "$tuning_url" wget -q -O - "$tuning_url" echo 

If successful, the script will issue:
 root@Buffalo:~# /mnt/sd/bin/fmpersonal response=OK url=http://www.last.fm/listen/user/IchBins/personal stationname=IchBins's Library Radio 

The artist channel activation script looks like:
 root@Buffalo:~# cat /mnt/sd/bin/fmart #!/bin/sh # "fmart" plays the radio station corresponding to a # certain artist. It takes the artist's name as command # line argument. Use quotes when the artist's name # contains spaces. # # Example: # fmart 'jahcoozi' artist=$(echo $1 | sed 's/ /%20/g') session=$(sed -nr 's/^session=(.*)/\1/p' ~/.bashfm) tuning_url='http://ws.audioscrobbler.com/radio/adjust.php?' tuning_url="${tuning_url}session=${session}&url=lastfm://artist/" tuning_url="${tuning_url}${artist}/similarartists&debug=0" #echo "$tuning_url" wget -q -O - "$tuning_url" echo 

Result of performance:
 root@Buffalo:~# /mnt/sd/bin/fmart response=FAILED error=4 root@Buffalo:~# /mnt/sd/bin/fmart "Knorkator" response=OK url=http://www.last.fm/listen/artist/Knorkator/similarartists stationname=Knorkator Radio 

Having tuned to the channel, you can add tracks from it to your playlist using fmget:
 root@Buffalo:~# cat /mnt/sd/bin/fmget #!/bin/sh # "fmget" gets list of mp3's from a radiostation and # adds it into the current playlist # # Example: # fmart 'Depeche Mode'; fmget session=$(sed -nr 's/^session=(.*)/\1/p' ~/.bashfm) mplayer=$(sed -nr 's/^mediaplayer=(.*)/\1/p' ~/.bashfm) tuning_url='http://ws.audioscrobbler.com/radio/xspf.php?' tuning_url="${tuning_url}sk=${session}" tuning_url="${tuning_url}&discovery=0&desktop=1.5.1" #echo "$tuning_url" mp3list=$(wget -q -O - "$tuning_url" | sed -nr "s/.*<location>(.*)<\/location>/\1/p") for i in $mp3list; do #echo $i $mplayer "$i" done 

Result of work:
 root@Buffalo:~# /mnt/sd/bin/fmget OK MPD 0.16.0 OK OK MPD 0.16.0 OK OK MPD 0.16.0 OK OK MPD 0.16.0 OK OK MPD 0.16.0 OK 

The resulting links to the tracks are links to mp3 files, so they can be fed to any player, not just MPD. It is enough to replace the mediaplayer parameter in the file ~ / .bashfm

That's all that I wanted to tell. As for automating the process of adding tracks to a playlist, I’ll leave this script to you as a homework assignment. Here you can call fmget +100500 times or call “mpc status” in a loop, parse the response and if nextsong == playlistlength, then call fmget.

As a bonus, if you have already mastered it here, I will give you, my curious friend, all the scripts from the article in one bottle

Source: https://habr.com/ru/post/144116/


All Articles