#!/bin/bash ##################################################### # Podcatcher Script # # you must first make a text file in the main directory # and name the text file podlist.txt # # Made By : Ibrahim Riad # # Dependencies : wget , awk , sed , xsltproc # # Some code was taken from bashpodder # http://linc.homeunix.org:8080/scripts/bashpodder/ ##################################################### ##################################################### # Declaring variables ##################################################### main_directory="$HOME/Podcast" temp_directory="$main_directory/temp" download_directory="$main_directory/shows" archive_directory="$main_directory/archive" #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #NOTE: Dont put the history file in the temp_directory #or the download_directory as it will be deleted #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! history="$main_directory/downloaded_history.txt" podcast_number="3" #alarm="/usr/lib/openoffice/share/gallery/sounds/applause.wav" multimedia_player="xmms" stream_player="kaffeine" bittorrent_client="ktorrent" #<---------------------------------------------------> if [ -f "$main_directory/lock" ];then echo "" echo -e "\033[0;31mThe script is already runing\033[0m" echo "" exit else touch "$main_directory/lock" fi function clean_up { # Perform program exit housekeeping echo "" echo "" echo -e "\033[0;31mThe script was exited unexpectatly\033[0m" echo "" echo -e "\033[0;31mTemp files removed\033[0m" echo "" sleep 1 rm "$main_directory/lock" rm "$temp_directory"/* 2>/dev/null exit } trap clean_up SIGHUP SIGINT SIGTERM #<---------------------------------------------------> ##################################################### # Specifying options ##################################################### while [ "$1" != "" ];do case $1 in -n|--podcast_number ) podcast_number="$2" ;; -m|--manage_feeds ) cat -n "$main_directory/podlist.txt" echo "" echo "1) Add feed" echo "2) Delete feed" echo "3) Check status of feeds" echo "" echo "0) Close" echo "" echo -n "Enter choise: " read manage_choise case $manage_choise in 1 ) echo -n "Enter Feed: " read feed_entered echo "$feed_entered" >> "$main_directory/podlist.txt" ;; 2 ) echo -n "Enter Number you want to delete: " read number_delete echo "" cat "$main_directory/podlist.txt" | sed -e "$number_delete"d | sed -e '/^$/d' > "$main_directory/podlist.txt" ;; 3 ) clear for x in `cat "$main_directory/podlist.txt"`;do echo "" echo -n "$x " wget -t 2 -nv --spider "$x" done rm "$main_directory/lock" 2>/dev/null exit ;; * ) rm "$main_directory/lock" 2>/dev/null exit ;; esac rm "$main_directory/lock" 2>/dev/null exit ;; -c|--clean ) rm "$download_directory"/* 2>/dev/null rm "$temp_directory"/* 2>/dev/null rm "$main_directory/lock" 2>/dev/null echo -e "\033[0;31mDirectory Cleaned\033[0m" exit ;; -a|--archive ) if [ ! -d "$archive_directory" ];then echo -e "\033[0;32mMaking archive directory\033[0m" echo "" mkdir "$archive_directory" fi cp "$download_directory"/*.mp3 "$archive_directory" 2>/dev/null cp "$download_directory"/*.ogg "$archive_directory" 2>/dev/null echo -e "\033[0;31mPodcasts archived\033[0m" exit ;; -p|--play ) $multimedia_player "$download_directory/latest.m3u" & exit ;; -d|--download ) download="y" ;; -f|--full-catalogue ) full="y" ;; -h|--help ) echo " Usage: $0 [OPTIONS] Options are: -n, --podcast_number Specify the number of podcast per each stream to be downloaded -m, --manage_feeds Lets you add , delete and check the status of the feeds -c, --clean Delete the podcasts and playlist -p, --play Play the playlist -d, --download Downloads aoutomatically new podcasts -f, --full-catalogue Adds the previous podcasts to your history without downloading them -h, --help Display this text and exit Examples: $0 -n 1 Will download the latest podcasts $0 -n 1 -d Will download the latest podcasts without confirmation " exit ;; esac shift done #<---------------------------------------------------> clear ##################################################### # Checking directories ##################################################### echo "#################################" echo "# Checking directories" echo "#################################" echo "" if [ -d "$main_directory" ];then echo -e "\033[0;32mMain directory exists\033[0m" echo "" else echo -e "\033[0;32mMaking main directory\033[0m" echo "" mkdir "$main_directory" fi if [ -d "$temp_directory" ];then echo -e "\033[0;32mTemp directory exists\033[0m" echo "" else echo -e "\033[0;32mMaking temp directory\033[0m" echo "" mkdir "$temp_directory" fi if [ -d "$download_directory" ];then echo -e "\033[0;32mDownload directory exists\033[0m" echo "" else echo -e "\033[0;32mMaking download directory\033[0m" echo "" mkdir "$download_directory" fi #<---------------------------------------------------> ##################################################### # Downloading feeds ##################################################### echo "#################################" echo "# Downloading feeds" echo "#################################" echo "" if [ ! -f "$main_directory/podlist.txt" ];then echo -e "\033[0;31mFile podlist.txt doesnt exists\033[0m" echo "" echo -n "Create one ? (y/n) : " read cpod echo "" if [ $cpod == "y" ];then touch "$main_directory/podlist.txt" echo "It's created but its empty now , you could check out http://www.podcast.net/ http://www.podcastalley.com/ for some feeds" echo "" rm "$main_directory/lock" exit else rm "$main_directory/lock" exit fi fi if [ ! -s "$main_directory/podlist.txt" ];then echo "The podlist.txt file is empty , you could check out http://www.podcast.net/ http://www.podcastalley.com/ for some feeds" echo "" rm "$main_directory/lock" exit fi echo "" echo -e "\033[0;32mDownloading feeds ....\033[0m" echo "" wget -t 2 -P "$temp_directory" -i "$main_directory/podlist.txt" #<---------------------------------------------------> ##################################################### # Filter the feeds from the enclosure ##################################################### if [ -f "$main_directory/parse_enclosure.xsl" ];then echo "" else echo "" echo ' ' > "$main_directory/parse_enclosure.xsl" fi cd "$temp_directory" for p in `ls` do xsltproc "$main_directory/parse_enclosure.xsl" $p >> $p.log done #<---------------------------------------------------> ##################################################### # Clean the output of the filter ##################################################### for x in `ls *.log` do cat "$x" | head -n $podcast_number >> final.txt done #<---------------------------------------------------> clear ##################################################### # Download the podcast and keep a history of it ##################################################### echo "#################################" echo "# Download or stream the podcast" echo "#################################" echo "" echo "Options:" echo "--------" echo "" echo "Press Ctrl+c to quit at any time" echo "" echo "d = Download , s = Stream , c = Catalogue , n = Skip" echo "" echo "" echo "New shows:" echo "----------" echo "" for t in `cat final.txt` do test=$(cat "$history" | grep "$t") 2>/dev/null status=$(echo $?) if [ $status -eq 1 ];then clean=$(basename "$t") echo "$clean" echo "" fi done echo "" echo "Actions:" echo "--------" echo "" for d in `cat final.txt` do temp=$(cat "$history" | grep "$d") 2>/dev/null exists=$(echo $?) clean_title=$(basename "$d") if [ $exists -eq 1 ];then if [ "$download" == "y" ];then choise="d" elif [ "$full" == "y" ];then echo "$d" >> "$history" echo "$clean_title has been added" continue else read -s -n1 -p "Download , Stream or catalogue : $clean_title (d/s/c/n) ? " choise fi echo "" ### Downloading ### if [ $choise == "d" ] || [ $choise == "D" ] || [ $choise == "y" ];then echo -e "\033[0;32mDownloading : \033[0m" "$clean_title" echo "" wget -c -P "$download_directory" "$d" && echo "$d" >> "$history" ### Check if its a torrent ### Bitt=$(file -b "$download_directory/$clean_title" | awk '{print $1}') if [ "$Bitt" == "BitTorrent" ];then "$bittorrent_client" "$download_directory/$clean_title" & fi ### Streaming ### elif [ $choise == "s" ] || [ $choise == "S" ];then echo -e "\033[0;32mStreaming : \033[0m" "$clean_title" echo "" echo "$d" >> "$history" $stream_player "$d" & ### Catalogue ### elif [ $choise == "c" ];then echo "" echo "Added $clean_title" echo "" echo "$d" >> "$history" ### Skipping ### else echo -e "\033[0;31mSkipping : \033[0m" "$clean_title" echo "" fi else continue fi done #<---------------------------------------------------> ##################################################### # Create an m3u playlist ##################################################### ls -1rc "$download_directory" | grep -v m3u > "$download_directory/latest.m3u" #<---------------------------------------------------> ##################################################### # Cleanup ##################################################### rm "$main_directory/lock" rm * 2>/dev/null echo "#################################" echo -e "#""\033[0;32m Done \033[0m" echo "#################################" echo ""