Bash-script to extract LIKED titles from PyRadio titles-logfile

Newest feature in PyRadio is the logging of currently playing radio stations and music titles/broadcasts (toggle on/off with ‘W’. Additionally you can tag a playing title as “liked” by pressing ‘w’ in PyRadio.

To extract these likes automatically and store them in a chronological list so you can review them later, I wrote this script. It creates an individual list-file in ~.config/pyradio/.

As gimmick there’s a second step where the titles are converted into a searchlink for DuckDuckGo and added to a second alphabetical list. That should make it more uncomplicated to start a websearch for a band/title you liked (just open with a pager in a terminal or a text-editor with hyperlink-support, hold CTRL pressed and click a link).

I hope you may draw some benefit from this script. I’m also always happy about feedback or suggestions for improvement regarding my bash-scripting, since I’m still a bloody noob :innocent:.

Have fun and wish you all a nice evening!

#!/bin/bash

         # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
         #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
         #::: Bash script to extract your LIKES from 'pyradio-titles.log'::::#
         #:::¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯:::#
         # # Faehnchen,  Apr. 20, 2022 # # # # # # # # # # # # # # # # # # # #

# https://forum.maboxlinux.org/c/userland/scripts-tutorials-tips/17

LOGFILE="$HOME/.config/pyradio/pyradio-titles.log"

#  default filepath for favourites list
LISTFILE="$HOME/.config/pyradio/$USER_pyradio_favs.list"

#  temporary files
EXTRFILE="$HOME/.config/pyradio/usr_favs.tmp"
SORTFILE="$HOME/.config/pyradio/sorted_favs.tmp"

echo -e "\n      \033[1;4;32m Extracting your LIKES from 'pyradio-titles.log' \033[0m"
sleep 0.5
echo -e "\n\033[1;34m Create or update file in\033[0m $LISTFILE\033[1;34m ? \033[0m\n"
echo -e " Yes, resume  \033[1m [r]   |  \033[0m No, please cancel!  \033[1m [ANY KEY] \n"
echo -n "   > "; read resume

  if [[ $resume != [r] ]]; then
            exit 0

fi

#  creating/updating chronological list
touch "$EXTRFILE"
touch "$SORTFILE"

grep -E '(LIKED)|Station' "$LOGFILE" | sed 's/ (LIKED)//' | sort > "$EXTRFILE"
uniq "$EXTRFILE" >> "$LISTFILE"
sort "$LISTFILE" > "$EXTRFILE"
uniq "$EXTRFILE" > "$LISTFILE"
cat /dev/null > "$EXTRFILE"
sleep 1
echo -e "\n\033[1;32m                            Done! \033[0m"
sleep 1

#  Sort, erase duplicates and update alphabetical list
echo -e "\n\033[0m Sorting $LISTFILE.alphabetically... \033[1m\n "
awk -F'[|]' '{print $2}' "$LISTFILE" | sed 's/     //;s/ >>> Station: //' > "$EXTRFILE"
sort "$EXTRFILE" | uniq > "$SORTFILE"
sleep 1

#  Adding DuckDuckGo websearch
echo -e "\n\033[1;32m         Ready. Adding websearch-links. Please wait! \033[0m\n"
sed 's/ /+/g' "$SORTFILE" > "$EXTRFILE"
sleep 1


#  write list with searchlinks line by line
lncount=`wc -l < "$SORTFILE"`
count=1
URLFILE="$HOME/.config/pyradio/$USER_favs-searchlinks_$lncount.list"

  until (( $count > $lncount )); do

    title=$(awk 'NR=='$count'{print $0}' "$SORTFILE")
    url=$(awk 'NR=='$count'{print $0}' "$EXTRFILE")

    printf "%-s \n  »»» https://duckduckgo.com/?q=%s&t=ffab&ia=web \n\n"  "$title" "$url" >> "$URLFILE"

    printf "\r\033[1;34m                         »%d / %-d« \033[0m"  $count $lncount
    let count++
    sleep 0.1   # "speed"

done

sleep 1
echo -e "\n\n\033[1;32m            Done! Updated/created your lists in: \033[0m"
echo -e "  $LISTFILE \n  $URLFILE"
echo -e '\n\033[1;34m     View in terminal with pager less or bat "/filepath"! \033[0m'

rm "$EXTRFILE"
rm "$SORTFILE"

exit 0
2 Likes

I believe this should be

grep -E '(LIKED)' "$LOGFILE"

Do you really want the station name in your list?
If you do, then no sort or uniq should be done
Then, when you create the duckduckgo list, you just do not print any line with a "Station: " in it (otherwise you would be searching for the station name in addition to the song name)

It’s up to you (it depends on what you want to do), but you should not be doing both :slight_smile:

BTW, do not use single quotes in filenames, you may make some poor user very unhappy :wink:

3 Likes

Yeah, that was the plan. First I filtered just the lines with (LIKED), but then I don’t know on which station the songs or features played so I added them in. Therefore the radio stations occur also in the list with the search links, but always just once per station, so no big deal (actually a benefit if you want to look up the station itself).

I’ll change the last echo with the path in single quotation marks. I knew this but using “” inside an echo message with double quotations didn’t work. But I just checked. When I put the message in single quotes I can use double quotes inside the message, right?

Edit:
Ah, sorry, now I know what you meant…
Ill change the filenames!

2 Likes