#!/bin/bash # By Linc 10/1/2004 # Find the latest script at http://linc.homeunix.org:8080/scripts/bashpodder # Last revision 07/01/2005 - Many Contributers! # If you use this and have made improvements or have comments # drop me an email at linc dot fessenden at gmail dot com # I'd appreciate it! # Modified by Dugan Chen (dugan_c@fastmail.fm) # Submitted on Dec 12th, 2005 # # I've merged in the following: # * Jyotirmoy_Bhattacharya's fix to make sure only one instance of # bashpodder runs at a time # * Lorenzo Taylor's URL and filename handling code # * Scott Maxwell's fix for the grep errors # * Leon Pennington's fix for not creating empty directories # # I also implemented the following changes here but did not invent them: # * --catchup_all parameter to log urls but not download them # * storing filenames in the logfile rather than the whole url # # The result is bashpodder as I personally like it! # # Note that it's mean't to be executed manually, not from cron, and # that it needs curl. LOCKFILE="lock.bashpodder" instance_lock () { TEMPFILE="temp.$$" echo $$ > $TEMPFILE || { echo "Could not create lock file" return 1 } trap "rm -f $TEMPFILE; exit" SIGINT SIGTERM ln $TEMPFILE $LOCKFILE >& /dev/null && { rm -f $TEMPFILE trap "rm -f $LOCKFILE; exit" SIGINT SIGTERM return 0 } kill -0 `cat $LOCKFILE` >& /dev/null && { rm -f $TEMPFILE return 1 } echo "Removing stale lock file" rm -f $LOCKFILE ln $TEMPFILE $LOCKFILE >& /dev/null && { rm -f $TEMPFILE trap "rm -f $LOCKFILE; exit" SIGINT SIGTERM return 0 } rm -f $TEMPFILE trap - SIGINT SIGTERM return 1 } # Make script crontab friendly: cd $(dirname $0) # datadir is the directory you want podcasts saved to: datadir=$(date +%Y-%m-%d) # Check for and create datadir if necessary: if test ! -d $datadir then mkdir $datadir fi #Check if another instance is already running instance_lock || { echo "An instance of bashpodder is already running"; exit 1 } # Delete any temp file: rm -f temp.log # If this is the first time, touch podcast.log to shut up the grep below. if ! [ -e podcast.log ] then touch podcast.log fi if [ "$1" = "--catchup_all" ] then # Read the bp.conf file and simply log each URL. while read podcast do file=$(wget -q $podcast -O - | xsltproc parse_enclosure.xsl - 2> /dev/null) || file=$(wget -q $podcast -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*url="\([^"]*\)".*/\1/p') for url in $file do realurl=`curl -s -I -L -w %{url_effective} --url "$url" | tail -n 1` filename=`echo "$realurl" | awk -F / '{print $NF}' | sed -e "s/%20/ /g" -e "s/%27/'/g" -e "s/%23/#/g" | awk -F ? '{print $1}'` if ! grep "$filename" podcast.log > /dev/null then echo $filename >> temp.log fi done done < bp.conf else # Read the bp.conf file and wget any url not already in the podcast.log file: while read podcast do file=$(wget -q $podcast -O - | xsltproc parse_enclosure.xsl - 2> /dev/null) || file=$(wget -q $podcast -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*url="\([^"]*\)".*/\1/p') for url in $file do realurl=`curl -s -I -L -w %{url_effective} --url "$url" | tail -n 1` filename=`echo "$realurl" | awk -F / '{print $NF}' | sed -e "s/%20/ /g" -e "s/%27/'/g" -e "s/%23/#/g" | awk -F ? '{print $1}'` if ! grep "$filename" podcast.log > /dev/null then curl -C - $realurl -o $datadir/$filename && echo $filename >> temp.log fi done done < bp.conf fi # Move dynamically created log file to permanent log file: cat podcast.log >> temp.log sort temp.log | uniq > podcast.log rm temp.log if [[ -z $(ls $datadir) ]]; then rmdir $datadir else # Create an m3u playlist: ls $datadir | grep -v m3u > $datadir/podcast.m3u fi #Release lock rm -f $LOCKFILE