#!/bin/bash
# This script downloads radio-broadcasts in mp3-format from
# the sites of Radio-France.
# The only argument to the script is the URL to a player-page,
# i.e. the page for 1 broadcast, showing a play-button on top.
#
# ©2019-2019 Michael Uplawski <michael.uplawski@uplawski.eu>
# Use ths script at your own risk, modify it as you please.
# But maybe leave the copyright-notice intact. Thank You.

SC=`basename "$0"`

if [ $# -ne 1 ]
then
  clear
  echo -e "ERREUR ! Il faut l'URL d'une page avec un audio-player"
  echo -e "Exemple :\n\t"$SC" https://www.franceculture.fr/emissions/la-fabrique-mediatique/defiance-envers-les-medias-quelles-solutions-22"
  exit 1
fi

# --------- SOME DEFINITIONS ----------
# The command to extract an mp3-file from a page
EXTR_CULT='puts $_.at_css("div.heading-zone-wrapper>div.heading-zone-player-button>button.replay-button/@data-asset-source")'

EXTR_INTER='puts $_.at_css("div.cover-emission-actions-buttons-wrapper>button.replay-button/@data-url")'

EXTR=""

if [[ $1 == *"franceinter"* ]]
then
  EXTR=$EXTR_INTER
elif [[ $1 == *"franceculture"* ]]
then
  EXTR=$EXTR_CULT
else
  echo -e "ERREUR ! Téléchargements sont possibles seulement des sites de"
  echo -e "France-Culture ou France-Inter !"
  exit 2
fi
# extract the URL of the mp3
mp3=`curl -s "$1" | nokogiri -e "$EXTR"`
# extract the title of the broadcast
title=`curl -s "$DICT""$1" | nokogiri -e 'puts $_.at_css("title/text()")'`
title=`echo "$title"|tr -s "[:space:][:punct:]" _` 

# Output-file
OFL="$title".mp3
echo $OFL

# --------> ACTION <---------
# download the mp3
torify wget -c "$mp3" --output-document="$OFL"
# <-------- END ACTION --------->
#EOF
