Skip to content

Instantly share code, notes, and snippets.

@eni23
Last active August 29, 2015 13:59
Show Gist options
  • Save eni23/10839170 to your computer and use it in GitHub Desktop.
Save eni23/10839170 to your computer and use it in GitHub Desktop.
housewife for com_artlu_music
#!/bin/bash
#
#
# project: artlu website
# author: cvw-2014-04-16
# version: {$id}
#
# desc:
# convert uploaded audio-files to misc formats
# and delete orphaned entrys from com_artlu_music
#
# deps (debian wheezy):
# apt-get install ffmpeg libavcodec-extra-53
#
# this script needs to run as a cronjob as fcgi-user
#
# set to false if no output is needed
OUTPUT="true"
function logger {
if [ "$OUTPUT" == "true" ]; then
echo $1
fi
}
function get_joomla_setting {
grep "public \$$1 =" $JOOMLA_CONF | awk '{print $4}' | cut -d"'" -f2
}
function mysql_query {
mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS --batch $MYSQL_DB -N -e "$1"
}
# set vars
SDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT=$(dirname $SDIR)
JOOMLA_PATH=$(cat $ROOT/php/conf.php | grep "JOOMLA_PATH" | cut -d'"' -f4)
JOOMLA_CONF="$JOOMLA_PATH/configuration.php"
MYSQL_HOST=$(get_joomla_setting 'host')
MYSQL_USER=$(get_joomla_setting 'user')
MYSQL_PASS=$(get_joomla_setting 'password')
MYSQL_DB=$(get_joomla_setting 'db')
DBPREFIX=$(get_joomla_setting 'dbprefix')
TABLE=$DBPREFIX"artlu_music_tracks"
DATA_PATH="$JOOMLA_PATH/administrator/components/com_artlu_music/audio/"
TMPFILE=$(mktemp --suffix artlumusic)
# get table content
mysql_query "SELECT id,track_data FROM $TABLE WHERE state NOT IN ( '-2' );" > $TMPFILE
# delete orphaned files first
ls -1 $DATA_PATH | while read FILE; do
BASENAME=$(echo ${FILE:0:${#z}-4})
CHK=$(grep "$BASENAME" $TMPFILE)
if [ -z "$CHK" ]; then
REALFILE="$DATA_PATH$FILE"
logger "delete orphaned file $FILE"
rm $REALFILE
fi;
done
# convert files then if needed
cat $TMPFILE | while read ID FILE; do
BASENAME=$(echo ${FILE:0:${#z}-4})
FULL_PATH="$DATA_PATH$BASENAME"
if [ -f "$FULL_PATH.mp3" ]; then
# convert to m4a
if [ ! -f "$FULL_PATH.m4a" ]; then
logger "convert $FILE to m4a"
avconv -i "$FULL_PATH.mp3" -acodec aac -strict experimental -aq 60 "$FULL_PATH.m4a" > /dev/null 2>&1
fi
# convert to ogg
if [ ! -f "$FULL_PATH.ogg" ]; then
logger "convert $FILE to ogg"
avconv -i "$FULL_PATH.mp3" -acodec libvorbis "$FULL_PATH.ogg" > /dev/null 2>&1
fi
# delete orphaned mysql-entry's
else
logger "delete orphaned/trashed mysql-entry: ID#$ID"
mysql_query "DELETE FROM $TABLE where id=$ID LIMIT 1;"
fi
done
# delete trased items from mysql
mysql_query "SELECT id FROM $TABLE WHERE state IN ( '-2' );" | while read ID; do
logger "delete trashed mysql-item #$ID";
mysql_query "DELETE FROM $TABLE where id=$ID LIMIT 1;"
done
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment