astroport/www/LOVELand/_CopyLaRadio/copy.sh

102 lines
6.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
### _shell.sh's are used ONLY with pi USER (change it if you want)
###
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
# Clean /tmp/ytdl.list
#echo > /tmp/ytdl.list
export YOU=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 1) || er+=" ipfs daemon not running"
[[ $er ]] && echo $er
## Accents and different languages needed to create encodings. on 8, 16, 32, 64 bits ...
# it is a big mess/// so we remove accent until someone take care of making it better. TODO???
#
function replace_accents (){
INPUT="$1"
declare -a acc
declare -a noa
acc=('$' 'è' 'ê' 'é' 'À' 'Á' 'Â' 'Ã' 'Ä' 'Å' 'Æ' 'Ç' 'È' 'É' 'Ê' 'Ë' 'Ì' 'Í' 'Î' 'Ï' 'Ð' 'Ñ' 'Ò' 'Ó' 'Ô' 'Õ' 'Ö' 'Ø' 'Ù' 'Ú' 'Û' 'Ü' 'Ý' 'ß' 'à' 'á' 'â' 'ã' 'ä' 'å' 'æ' 'ç' 'è' 'é' 'ê' 'ë' 'ì' 'í' 'î' 'ï' 'ñ' 'ò' 'ó' 'ô' 'õ' 'ö' 'ø' 'ù' 'ú' 'û' 'ü' 'ý' 'ÿ' 'Ā' 'ā' 'Ă' 'ă' 'Ą' 'ą' 'Ć' 'ć' 'Ĉ' 'ĉ' 'Ċ' 'ċ' 'Č' 'č' 'Ď' 'ď' 'Đ' 'đ' 'Ē' 'ē' 'Ĕ' 'ĕ' 'Ė' 'ė' 'Ę' 'ę' 'Ě' 'ě' 'Ĝ' 'ĝ' 'Ğ' 'ğ' 'Ġ' 'ġ' 'Ģ' 'ģ' 'Ĥ' 'ĥ' 'Ħ' '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 'c' 'C' 'c' 'C' 'c' 'D' 'd' 'D' 'd' 'E' 'e' 'E' 'e' 'E' 'e' 'E' 'e' 'E' 'e' 'G' 'g' 'G' 'g' 'G' 'g' 'G' 'g' 'H' 'h' 'H' 'h' 'I' 'i' 'I' 'i' 'I' 'i' 'I' 'i' 'I' 'i' 'IJ' 'ij' 'J' 'j' 'K' 'k' 'L' 'l' 'L' 'l' 'L' 'l' 'L' 'l' 'l' 'l' 'N' 'n' 'N' 'n' 'N' 'n' 'n' 'O' 'o' 'O' 'o' 'O' 'o' 'OE' 'oe' 'R' 'r' 'R' 'r' 'R' 'r' 'S' 's' 'S' 's' 'S' 's' 'S' 's' 'T' 't' 'T' 't' 'T' 't' 'U' 'u' 'U' 'u' 'U' 'u' 'U' 'u' 'U' 'u' 'U' 'u' 'W' 'w' 'Y' 'y' 'Y' 'Z' 'z' 'Z' 'z' 'Z' 'z' 's' 'f' 'O' 'o' 'U' 'u' 'A' 'a' 'I' 'i' 'O' 'o' 'U' 'u' 'U' 'u' 'U' 'u' 'U' 'u' 'U' 'u' 'A' 'a' 'AE' 'ae' 'O' 'o');
OUTPUT=""
i=0
length=${#INPUT}
while [[ $i -lt $length ]]; do
char=${INPUT:$i:1};
#echo $i:$char
j=0
for letter in "${acc[@]}"
do
if [[ "$letter" == "$char" ]]; then
char="${noa[$j]}"
fi
((j++))
done
((i++))
OUTPUT=$OUTPUT$char
done
echo $OUTPUT
}
# BASH CAN MAKE INFINTE LOOP LOOKING FOR A NEW LINE APPEERING IN A FILE.
# this technique can transmit FIFO command between process
# This code is from www.copylaradio.com and RUN youtube-dl search and copy from the use of a button.
#
tail -fn 1 /tmp/ytdl.list | while read -r line
do
#################################################
radio=$(echo $line | cut -d "|" -f 1)
artist=$(echo $line | cut -d "|" -f 2)
song=$(echo $line | cut -d "|" -f 3)
echo "######## $radio ########" >> /tmp/youtube-dl.log 2>&1
echo $(date) >> /tmp/youtube-dl.log 2>&1
echo ${line} >> /tmp/youtube-dl.log 2>&1
#################################################
# YOUTUBE-DL SEARCH & COPY : "radio|artist|song"
if [[ "$artist" != "" && "$song" != "" && "$radio" != "" && "$radio" != "$song" && "$radio" != "undefined" && "$artist" != "$song" ]]; then
mkdir -p "/home/$YOU/.zen/audio/${artist}/${song}"
$MY_PATH/parle.sh "Démarrage Enregistrement $radio."
$MY_PATH/parle.sh "$artist" "en-US"
$MY_PATH/parle.sh "$song" "en-US"
echo "Copy youtube-dl: $artist | $song ($radio)" >> /tmp/youtube-dl.log 2>&1
# Clean search string
mysearch=$(replace_accents "$artist $song")
if [[ "$mysearch" == "" ]]; then ~/parle.sh "Aucune recherche reçue!"; continue; fi
# Search & Download bestaudio from 1st video result (ogg / opus / m4a): MEDIUM CPU
# OPTION: --embed-thumbnail --metadata-from-title "%(artist)s - %(title)s" --add-metadata
youtube-dl --default-search ytsearch1: --download-archive /home/$YOU/.ytdl.list --continue --no-overwrites --metadata-from-title "$artist - %(title)s" --add-metadata --embed-thumbnail --ignore-errors -f m4a --extract-audio -o "/home/$YOU/audio/${artist}/${song}/%(id)s.%(title)s.%(uploader_id)s.%(ext)s" "$mysearch" >> /tmp/youtube-dl.log 2>&1
# TODO: CHOOSE --metadata-from-title "$artist - %(title)s" or --metadata-from-title %(title)s"
# Update mpd playlist
curl -b "currenthost=Default;player_backend=mpd" http://localhost/albums.php?rebuild=yes > /dev/null
$MY_PATH/parle.sh "Enregistrement $radio terminé!"
######################################################
#### COPY LINK : "CopyLaRadio||https://link2media.url"
elif [[ "$radio" != "" && "$artist" == "" && "$song" != "" ]]; then
# LINK COPY
$MY_PATH/parle.sh "Copie du lien!"
echo "Copy LINK: $song" >> /tmp/youtube-dl.log 2>&1
mkdir -p /home/$YOU/download/
youtube-dl --download-archive /home/$YOU/.ytdl.list --continue --no-overwrites --embed-thumbnail --metadata-from-title "%(artist)s - %(title)s" --add-metadata -o "/home/$YOU/download/%(title)s.%(ext)s" "$song" >> /tmp/youtube-dl.log 2>&1
$MY_PATH/parle.sh "Enregistrement du lien terminé!"
######################################################
#### COPY LINK : "radio||"
elif [[ "$radio" != "" && "$artist" == "" && "$song" == "" ]]; then
# EXTERNAL PARSER: ADD SCRAPERS IN ./libradio
$MY_PATH/parle.sh "Recherche RADIO ${radio}"
echo "External search: ${radio}" >> /tmp/youtube-dl.log 2>&1
if [[ -f $MY_PATH/libradio/${radio}.php ]]; then
$MY_PATH/parle.sh "Module existant"
php "$MY_PATH/libradio/${radio}.php"
else
$MY_PATH/parle.sh "Aucun module de décodage pour cette radio."
fi
#### NOTHING GOOD
else
$MY_PATH/parle.sh "Rien à copier"
fi
done