Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # w-play 2 (author: Michał Matyas, http://nerdblog.pl)
- # ----------------------------------------------------
- #
- # Rewritten from scratch, new version of Wrzutaplayer.sh
- # Now support simple database system based on text files.
- #
- # How to use, version for dummies:
- #
- # 1. In terminal: chmod +x w-play2.sh (you have to do this ONCE)
- # 2. In terminal: ./w-play2.sh
- #
- # You are now in interactive mode. Neat, huh?
- #
- # 3. In interactive mode: add many Your Favourite Artist
- # 4. In interactive mode: play
- #
- # If you had enough, you can stop playing the music by pressing ^C (Ctrl+C)
- # Double pressing this combination will quit w-play 2 just like 'quit' command
- #
- # Enjoy your listening!
- #
- WP2_VERSION="beta1.6"
- ################################################################################
- #
- # Hey, it supports last.fm now!
- #
- #
- # Path to your lastfmsubmit program (check in /usr/local/lib if you're on Fedora)
- #
- WP2_LASTFMSUBMIT_PATH="/usr/lib/lastfmsubmitd/lastfmsubmit"
- #
- # Set to 1 to scrobble on default
- #
- WP2_LASTFM_ON=1
- ################################################################################
- trap '[[ "${WP2_EMERGENCY_STOP}" == 1 ]] && exit 255 || WP2_EMERGENCY_STOP=1' SIGINT
- #
- # Reads the w-play2 database and saves it into WP2_DB variable
- #
- # @param string $1 Database name
- #
- load_db()
- {
- [[ -z "$1" ]] && return 1;
- if [ ! -r "${1}" ];
- then
- echo "${1} does not exist."
- return 1
- fi
- IFS=$(printf "\n\b")
- local i=''
- [[ -z "${WP2_DB_COUNT}" ]] && local n=0 || local n=$[WP2_DB_COUNT];
- local len=$(cat "${1}" | wc -l)
- local prg=0
- WP2_DB_NAME="${1}"
- for i in $(cat "${1}");
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- prg=$[prg+1]
- progress_bar "Loading database... [%3d%%]" ${prg} ${len}
- if [ ! -z "${i}" ]; then
- WP2_DB[${n}]="${i}";
- n=$[n+1]
- WP2_DB_COUNT="${n}"
- fi
- done
- progress_bar_hide
- search_clear_results
- echo "Database loaded."
- }
- #
- # Lists database fields
- #
- # @uses get_from_db()
- #
- list_db()
- {
- local i=0
- if [[ ${WP2_DB_COUNT} == 0 || -z ${WP2_DB_COUNT} ]];
- then
- echo "Database is empty"
- fi
- for i in $( seq 0 $((${WP2_DB_COUNT} - 1)) );
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- get_from_db "$[i+1]"
- echo "$[i+1]. ${WP2_LAST_ELEMENT[1]}"
- done;
- }
- #
- # Creates empty DB (just a nice wrapper for clearing WP2_DB)
- #
- # @uses search_clear_results()
- #
- clear_db()
- {
- WP2_DB=();
- WP2_DB_COUNT=0
- WP2_DB_NAME=''
- WP2_LAST_ELEMENT=()
- search_clear_results
- }
- #
- # Adds element to the DB
- #
- # @param string $1 Wrzuta.pl address
- # @param string $2 Song name
- # @param string $3 Song length
- # @uses search_clear_results()
- #
- add_to_db()
- {
- WP2_DB[${WP2_DB_COUNT}]="${1};${2};${3}";
- WP2_DB_COUNT=$[WP2_DB_COUNT + 1]
- search_clear_results
- }
- #
- # Saves the w-play2 database in file
- # Warning! All data in the file are erased!
- #
- # @param string $1 File name
- # @uses progress_bar()
- # @uses progress_bar_hide()
- #
- save_db()
- {
- [[ -z "$1" ]] && return 1;
- echo -ne "" > "$1"
- echo "Saving ${WP2_DB_COUNT} entries in ${1}"
- local i=0
- for i in $( seq 0 $((${WP2_DB_COUNT} - 1)) ); do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- progress_bar "Saving... [%3d%%] " $i $((${WP2_DB_COUNT} - 1))
- echo "${WP2_DB[${i}]}" >> "$1"
- done;
- progress_bar_hide
- }
- #
- # Reads the w-play2 element from database
- #
- # @param int $1 Entry ID
- #
- get_from_db()
- {
- if [[ ${WP2_DB_COUNT} -lt ${1} ]];
- then
- echo "Entry ${1} does not exist in this database."
- return 1
- fi
- local db_elem="${WP2_DB[ $[ $((${1} - 1)) ] ]}"
- local array_names=( name link )
- local i=0
- WP2_LAST_ELEMENT=()
- if [[ -z "${db_elem}" ]];
- then
- echo "Entry ${1} does not exist in this database."
- return 1
- fi
- for i in 1 2 3;
- do
- WP2_LAST_ELEMENT[$[ ${i} - 1 ]]="$( echo "${db_elem}" | cut -d";" -f$i )";
- done;
- }
- #
- # Deletes an entry from database
- #
- # @param id $1 Entry ID
- #
- delete_from_db()
- {
- get_from_db "$1"
- echo "${WP2_LAST_ELEMENT[1]} removed from database"
- unset WP2_DB[${1}]
- search_clear_results
- }
- #
- # Searches for a song in database
- #
- # @param string $1 Searched entry
- # @uses get_from_db
- #
- search_db()
- {
- local i=0
- local n=0
- for i in $( seq 1 ${WP2_DB_COUNT} );
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- progress_bar "Searching... [%3d%%]" $i $[${WP2_DB_COUNT}-1]
- get_from_db "$i"
- # Search for keywords inside, not 1:1 match
- if [[ -n $(echo "${WP2_LAST_ELEMENT[*]}" | grep -i "${1}") ]];
- then
- WP2_SEARCH[${n}]="${i}"
- n=$[n+1]
- fi
- WP2_SEARCH_COUNT=${n}
- done;
- progress_bar_hide
- [[ -n "${WP2_SEARCH[*]}" ]] && return 0 || return 1;
- }
- #
- # Clears search results
- #
- search_clear_results()
- {
- WP2_SEARCH=()
- WP2_SEARCH_COUNT=0
- }
- #
- # Wrapper for search_db() in interactive mode
- #
- # @param string $1 Searched entry
- # @uses search_db()
- #
- find_in_db()
- {
- search_db "${1}"
- echo "Found ${WP2_SEARCH_COUNT} entries"
- if [[ -n "${WP2_SEARCH}" ]];
- then
- for i in ${WP2_SEARCH[*]};
- do
- get_from_db ${i}
- echo "${i}. ${WP2_LAST_ELEMENT[1]}"
- done;
- echo "To play them all, type play found"
- fi
- }
- #
- # Plays a song from database
- #
- # @param int $1 ID of database entry
- # @uses get_from_db
- #
- play_from_db()
- {
- get_from_db $1
- [[ $? != 0 ]] && return 1;
- echo -ne "Loading...\r"
- local mp3="$( wrzuta_mp3 "${WP2_LAST_ELEMENT[0]}" )"
- if [[ -z "$mp3" ]];
- then
- echo "An error occured while loading an MP3. Maybe it's just a glitch."
- else
- echo "Now playing: ${WP2_LAST_ELEMENT[1]} (${WP2_LAST_ELEMENT[2]})"
- local starttime="$(date +%s)"
- MPLAYER_VERBOSE=-6 mplayer -msglevel all=-1:statusline=5 "$mp3" 2>/dev/null
- local errorcode=$?
- local endtime="$(date +%s)"
- local mintime=$(( $( echo "${WP2_LAST_ELEMENT[2]}" | awk -F: '{if($0=="") print '0';else if($3!="") print $1*3600+$2*60+$3;else if($2!="") print $1*60+$2;else print $1}' ) / 3 ))
- if [[ "${errorcode}" == "0" && $((${endtime}-${starttime})) > ${mintime} ]];
- then
- lastfm_scrobble "$1"
- fi
- fi
- }
- #
- # Tries to guess song's data based on name
- # Yep, it's pretty dumb
- #
- # @param int $1 Entry ID
- #
- guess_tags()
- {
- WP2_TAGS=()
- get_from_db $1
- if [[ -n "${WP2_LAST_ELEMENT[*]}" ]];
- then
- local tested=$(echo ${WP2_LAST_ELEMENT[1]} | sed -e "s/^[0-9_-'\" ]*//" -e "s/ *\$//")
- # Title
- WP2_TAGS[0]=$(echo ${tested} | cut -d '-' -f 1 | sed -e "s/(.*)//g" -e "s/\[.*\]//g" -e "s/live.*//i" -e "s/cover\$//i" -e "s/ *\$//")
- # Name
- WP2_TAGS[1]=$(echo ${tested} | cut -d '-' -f 2- | sed -e "s/(.*)//g" -e "s/\[.*\]//g" -e "s/live.*//i" -e "s/cover\$//i" -e "s/^[0-9_-'\" ]*//" -e "s/ *\$//")
- # Duration
- WP2_TAGS[2]=${WP2_LAST_ELEMENT[2]}
- # Link
- WP2_TAGS[3]=${WP2_LAST_ELEMENT[0]}
- fi
- }
- #
- # Progress bar
- #
- # @param string $1 Text to be shown (uses %d to show percentage)
- # @param int $2 Number
- # @param int $3 Maximum value of Number
- #
- progress_bar()
- {
- tput sc
- if [[ $3 == 0 ]];
- then
- return 1;
- fi
- local percent=$(( ($2 * 100) / $3 ));
- bars=$((${percent}/10))
- progressbar="$(for bar in $(seq ${bars} ); do echo -n '='; done)>"
- [[ bars -ge 10 ]] && progressbar="==========";
- printf "\r${1} [%-10s]" "${percent}" "${progressbar}"
- #printf "\r$1" $percent
- }
- #
- # Hides scraps left behind by progress bar
- #
- progress_bar_hide()
- {
- tput el1;
- tput rc;
- printf "\r"
- }
- #
- # Gets one song from Wrzuta.pl's audio search
- #
- # @param string $1 Song title, or whatever should be searched for
- #
- wrzuta_get_one()
- {
- echo "Searching for: ${1}"
- local link="$(wget "http://www.wrzuta.pl/szukaj/audio/${1}" --user-agent="Mozilla 5.0" -q -O - | awk -F \" '/class="title"/{ print $4 }' | head -n 1)"
- echo -ne "Saving ${link##*/} ... "
- songname=$(wrzuta_title "$link")
- songlength=$(wrzuta_duration "$link")
- echo -ne "done.\n"
- add_to_db "$link" "$songname" "$songlength"
- }
- #
- # Gets whole list of songs from Wrzuta.pl's audio search
- #
- # @param string $1 Song title, or whatever should be searched for
- # @param int $2 Number of pages to search
- #
- wrzuta_get_many()
- {
- echo "Searching for: ${1}"
- local links=" ";
- while [[ -n "$links" ]]; do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- local page=$[page+1];
- if [[ -n "${2}" && "${2}" < "${page}" ]];
- then
- return 0;
- fi
- local links=$(wget "http://www.wrzuta.pl/szukaj/audio/${1}/$page" --user-agent="Mozilla 5.0" -q -O - | awk -F \" '/class="title"/{ print $4 }')
- [[ -n "${links}" ]] && echo -e "\nPage: ${page}";
- local link
- for link in $links; do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && return 1;
- echo -ne "Saving ${link##*/} ... "
- songname=$(wrzuta_title $link)
- songlength=$(wrzuta_duration $link)
- echo -ne "done.\n"
- add_to_db "$link" "$songname" "$songlength"
- done;
- echo ""
- done;
- }
- #
- # Adds song to Last.fm
- #
- # @param int $1 Entry ID
- #
- lastfm_scrobble()
- {
- if [[ "${WP2_LASTFM_ON}" == "1" ]];
- then
- guess_tags "${1}"
- if [[ -n "${WP2_TAGS[0]}" && -n "${WP2_TAGS[1]}" && -n "${WP2_TAGS[2]}" && "${WP2_TAGS[0]}" != "${WP2_TAGS[1]}" ]];
- then
- ${WP2_LASTFMSUBMIT_PATH} --artist "${WP2_TAGS[0]}" --title "${WP2_TAGS[1]}" --length "${WP2_TAGS[2]}"
- fi
- fi
- }
- #
- # Gets list of songs from Wrzuta.pl's audio search based on
- # TasteKid simliar search engine
- #
- # @param string $1 Artist name
- #
- wrzuta_get_similar()
- {
- IFS=$(printf "\n\b")
- for i in $(tastekid_similar ${1});
- do
- wrzuta_get_many ${i} 1
- done
- }
- #
- # Helper method, gets song's name
- #
- # @param string $1 Link to the song
- #
- wrzuta_title()
- {
- echo "$(wget -q -O - "$1" | grep '<title>' | sed -e 's/.*WRZUTA - \([^<]*\).*/\1/' -e "s/'/'/g" -e 's/&[^;]*;//g' -e 's/;//g' )"
- }
- #
- # Helper method, gets song's duration
- #
- # @param string $1 Link to the song
- #
- wrzuta_duration()
- {
- echo $(echo $(wget -q -O - "$1" |tr -d '\n'|sed -e 's/.*<span id="file_info_data_size">\([^>]*\)<\/span>.*/\1/g'))
- }
- #
- # Helper method, gets song's MP3 address
- #
- # @param string $1 Link to the song
- #
- wrzuta_mp3()
- {
- echo $(wget -q "$(echo "$1/f/a/i/l" | sed -e 's/audio/xml\/plik/')" -O -|grep "fileId"|sed -e 's/.*\[\(.*\)\]\].*/\1/')
- }
- #
- # Searches in TasteKid.com for similar artists
- #
- # @param string $1 Artist name
- #
- tastekid_similar()
- {
- # Thanks blueyed for perl code
- wget "http://www.tastekid.com/ask/ws?q=band:$( echo ${1} | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' )" -q -O -|grep 'name'|sed -e 's/.*CDATA\[\(.*\)\]\].*/\1/'
- }
- #
- # Meat!
- #
- wplay2()
- {
- # It *WILL* come handy
- local i=0;
- echo "w-play 2 (${WP2_VERSION})"
- echo "For command list type: help"
- if [[ -n "${1}" ]];
- then
- if [[ -r "${1}" ]];
- then
- load_db "${1}"|less
- play
- else
- echo "Usage: ${0} [dbname]"
- echo ""
- echo "Or use interactive mode, you dummy."
- fi
- fi
- while read -p '> ' arg; do
- # QUIT
- [[ "${arg}" == "quit" || "${arg}" == "exit" ]] && exit 0;
- # Load Database
- if [[ "${arg:0:5}" == "load " ]];
- then
- load_db "${arg:5}"
- elif [[ "${arg}" == "list" ]];
- then
- list_db
- elif [[ "${arg}" == "clear" ]];
- then
- clear_db
- elif [[ "${arg:0:8}" == "add one " ]];
- then
- wrzuta_get_one "${arg:8}"
- echo "Finished";
- elif [[ "${arg:0:9}" == "add many " ]];
- then
- wrzuta_get_many "${arg:9}"
- echo "Finished";
- elif [[ "${arg:0:12}" == "add similar " ]];
- then
- wrzuta_get_similar "${arg:12}"
- echo "Finished";
- # Hidden from command list because it's mostly for debugging
- elif [[ "${arg:0:5}" == "tags " ]];
- then
- guess_tags "${arg:5}"
- if [[ -n "${WP2_TAGS[*]}" ]];
- then
- echo "Artist : ${WP2_TAGS[0]}"
- echo "Song : ${WP2_TAGS[1]}"
- echo "Duration : ${WP2_TAGS[2]}"
- echo "Link : ${WP2_TAGS[3]}"
- fi
- elif [[ "${arg:0:5}" == "find " ]];
- then
- find_in_db "${arg:5}"
- elif [[ "${arg:0:4}" == "del " ]];
- then
- if [[ ${WP2_DB_COUNT} > 0 ]];
- then
- if [[ -n "${arg:4}" ]];
- then
- for i in ${arg:4};
- do
- delete_from_db ${i}
- done
- fi
- else
- echo "Database is not loaded"
- fi
- elif [[ "${arg:0:5}" == "save " ]];
- then
- if [[ -z "${arg:5}" ]];
- then
- save_db "${WP2_DB_NAME}"
- else
- save_db "${arg:5}"
- fi
- elif [[ "${arg}" == "play" ]];
- then
- if [[ ${WP2_DB_COUNT} > 0 ]];
- then
- for i in $(seq 1 ${WP2_DB_COUNT});
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- play_from_db ${i}
- done
- echo -e "\nFinished";
- else
- echo "Database is empty"
- fi
- elif [[ "${arg}" == "play random" ]];
- then
- if [[ ${WP2_DB_COUNT} > 0 ]];
- then
- local tmp_playlist=()
- for i in $(seq 1 ${WP2_DB_COUNT});
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- tmp_playlist[${RANDOM}]="${i}"
- done
- for i in ${tmp_playlist[*]};
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- play_from_db ${i}
- done
- echo -e "\nFinished";
- else
- echo "Database is empty"
- fi
- elif [[ "${arg}" == "play found" ]];
- then
- if [[ ${WP2_DB_COUNT} > 0 ]];
- then
- if [[ -n ${WP2_SEARCH} && ${WP2_SEARCH_COUNT} > 0 ]];
- then
- for i in ${WP2_SEARCH[*]};
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- play_from_db ${i}
- done
- echo -e "\nFinished";
- else
- echo "You have to find something first."
- fi
- else
- echo "Database is empty"
- fi
- elif [[ "${arg:0:5}" == "play " ]];
- then
- if [[ ${WP2_DB_COUNT} > 0 ]];
- then
- if [[ -n "${arg:5}" ]];
- then
- for i in ${arg:5};
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- play_from_db ${i}
- done
- else
- for i in $(seq 1 ${WP2_DB_COUNT});
- do
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && break;
- play_from_db ${i}
- done
- fi
- echo -e "\nFinished";
- else
- echo "Database is empty"
- fi
- elif [[ "${arg:0:8}" == "similar " ]];
- then
- echo -ne "Checking on TasteKid... \r"
- echo $(tastekid_similar "${arg:8}")
- elif [[ "${arg:0:8}" == "scrobble" ]];
- then
- if [[ ! -x "${WP2_LASTFMSUBMIT_PATH}" ]];
- then
- echo "You have to install lastfmsubmitd and set a proper path in configuration"
- else
- if [[ "${arg:9}" == "on" ]];
- then
- WP2_LASTFM_ON=1
- echo "Scrobbling enabled."
- else
- WP2_LASTFM_ON=0
- echo "Scrobbling disabled."
- fi
- fi
- elif [[ "${arg}" == "help" ]];
- then
- echo " "
- echo " List of commands:"
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " load [filename] - loads database from file"
- echo " save [filename] - saves database to file (file is overwritten)"
- echo " "
- echo " list - lists all entries in database"
- echo " clear - clears database"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " add one [name] - looks for a song in Wrzuta.pl"
- echo " add many [name] - looks for a list of songs in Wrzuta.pl"
- echo " "
- echo " add similar [artist] - looks for songs of similar artists"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " del [id] - remove ID entry from database"
- echo " del [list] - remove listed IDs from database"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " play - plays all entries from database"
- echo " play [id] - plays ID entry from database"
- echo " play [list] - plays listed IDs from database"
- echo " "
- echo " play random - plays random entries from database"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " find [name] - searches database for NAME"
- echo " play found - plays entries found using 'find' command"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " similar [name] - looks for similar bands on TasteKid"
- echo " scrobble [on/off] - toggles scrobbling on Last.FM"
- echo " "
- echo "--------------------------------------------------------------------------------"
- echo " "
- echo " help - this list of commands"
- echo " quit - closes w-play2"
- echo " "
- elif [[ "${WP2_EMERGENCY_STOP}" != "1" ]];
- then
- echo "Unknown command: '${arg}'. Type help for command list."
- fi
- [[ "${WP2_EMERGENCY_STOP}" == "1" ]] && echo "Stopped on user request."
- WP2_EMERGENCY_STOP=0; # We're safe now, bad people are gone
- done;
- }
- wplay2 # kaboom!
Add Comment
Please, Sign In to add comment