mediabox rev 6
Add mediabox cmdline tool and main CGI script
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Thu Feb 23 21:48:35 2017 +0100 (2017-02-23) |
parents | f758d6f609be |
children | 1c1fdd9f8385 |
files | index.cgi mediabox |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/index.cgi Thu Feb 23 21:48:35 2017 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +#!/bin/sh 1.5 +# 1.6 +# CGI/SHell MediaBox using HTML5 features. Coded with lightness and 1.7 +# elegence in mind. 1.8 +# 1.9 +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License 1.10 +# 1.11 +. /usr/lib/slitaz/httphelper.sh 1.12 +header 1.13 + 1.14 +# 1.15 +# Internal variables 1.16 +# 1.17 +version="beta" 1.18 +cache="$PWD/cache" 1.19 +script="$SCRIPT_NAME" 1.20 + 1.21 +# Set $home - Cache user ID and source configs 1.22 +if [ "$(GET home)" ]; then 1.23 + echo $(GET home) > ${cache}/home 1.24 + rm -f ${cache}/audio ${cache}/videos 1.25 +fi 1.26 +home=$(cat $cache/home) 1.27 +config="$home/.config/mediabox" 1.28 +if [ -f "${config}/mediabox.conf" ]; then 1.29 + . ${config}/mediabox.conf 1.30 +else 1.31 + gettext "Missing config file:"; echo " $config"; exit 1 1.32 +fi 1.33 + 1.34 +# i18n 1.35 +export LANG=${LOCALE} LC_ALL=${LOCALE} 1.36 +. /usr/bin/gettext.sh 1.37 +export TEXTDOMAIN='mediabox' 1.38 + 1.39 +# 1.40 +# Functions 1.41 +# 1.42 + 1.43 +# Usage: html_header "Page Title" 1.44 +html_header() { 1.45 + cat lib/header.html | sed s"/%TITLE%/$1/" 1.46 + cat << EOT 1.47 +<header> 1.48 + <h1>$1</h1> 1.49 +EOT 1.50 +} 1.51 + 1.52 +html_footer() { 1.53 + cat << EOT 1.54 +<footer> 1.55 + ♥ 1.56 +</footer> 1.57 +</body> 1.58 +</html> 1.59 +EOT 1.60 +} 1.61 + 1.62 +# Header navigation 1.63 +nav_menu() { 1.64 + cat << EOT 1.65 + <nav> 1.66 + <a href="$script">$(gettext "Home")</a> 1.67 + <a href="$script?music">$(gettext "Music")</a> 1.68 + <a href="$script?videos">$(gettext "Videos")</a> 1.69 + <a href="$script?playlists">$(gettext "Playlists")</a> 1.70 + <a href="$script?radio">$(gettext "Radio")</a> 1.71 + </nav> 1.72 +</header> 1.73 +EOT 1.74 +} 1.75 + 1.76 +# Page navigation 1.77 +nav_page() { 1.78 + cat << EOT 1.79 +</header> 1.80 +<div id="home"> 1.81 + <nav > 1.82 + <a href="$script?music">$(gettext "Music")</a> 1.83 + <a href="$script?videos">$(gettext "Videos")</a> 1.84 + <a href="$script?playlists">$(gettext "Playlists")</a> 1.85 + <a href="$script?radio">$(gettext "Radio")</a> 1.86 + <a href="$script?settings">$(gettext "Settings")</a> 1.87 + </nav> 1.88 +</div> 1.89 +EOT 1.90 +} 1.91 + 1.92 +# Find and list audio/videos files. 1.93 +find_audio() { 1.94 + [ ! -f "${cache}/audio" ] && find "${MUSIC}" \ 1.95 + -regex '.*\.\(mp3\|ogg\|wav\)' > ${cache}/audio 1.96 + cat ${cache}/audio 1.97 +} 1.98 +find_videos() { 1.99 + find "${VIDEOS}" -regex '.*\.\(mp4\|ogv\|avi\)' > ${cache}/videos 1.100 + cat ${cache}/videos 1.101 +} 1.102 + 1.103 +list_audio() { 1.104 + count="$(wc -l ${cache}/audio | cut -d " " -f 1)" 1.105 + echo "<ul id='audio-list'>" 1.106 + cat << EOT 1.107 + <li><span>♫ ♫ ♫ $count $(gettext "Tracks found")</span></li> 1.108 +EOT 1.109 + find_audio | while read a 1.110 + do 1.111 + filename="$(basename "${a}")" 1.112 + echo " <li><a href='$script?music&play=${a}'>${filename%.*}</a></li>" 1.113 + done 1.114 + echo "</ul>" 1.115 +} 1.116 + 1.117 +list_videos() { 1.118 + count="$(wc -l ${cache}/videos | cut -d " " -f 1)" 1.119 + cat << EOT 1.120 +<ul id='videos-list'> 1.121 + <li><span>$count $(gettext "Videos found")</span></li> 1.122 +EOT 1.123 + find_videos | while read v 1.124 + do 1.125 + filename="$(basename "${v}")" 1.126 + echo " <li><a href='$script?videos&play=${v}'>${filename%.*}</a></li>" 1.127 + done 1.128 + echo "</ul>" 1.129 +} 1.130 + 1.131 +list_radio() { 1.132 + if [ ! -f "$config/radio.list" ]; then 1.133 + cp lib/radio.list ${config} && chmod 0666 ${config}/radio.list 1.134 + fi 1.135 + echo "<ul id='radio-list'>" 1.136 + IFS="|" 1.137 + cat ${config}/radio.list | while read url info 1.138 + do 1.139 + [ "$info" ] || info="N/A" 1.140 + cat << EOT 1.141 + <li><a href='$script?radio&play=${url}&info=$info'>${url#http://}<span>$info</span></a></li> 1.142 +EOT 1.143 + done 1.144 + unset IFS info 1.145 + echo "</ul>" 1.146 +} 1.147 + 1.148 +list_playlists() { 1.149 + echo "<ul id='radio-list'>" 1.150 + IFS="|" 1.151 + cat ${config}/playlists.list | while read file info 1.152 + do 1.153 + [ "$info" ] || info="N/A" 1.154 + cat << EOT 1.155 + <li><a href='$script?playlists&play=${file}&info=$info'>$info</a></li> 1.156 +EOT 1.157 + done 1.158 + unset IFS info 1.159 + echo "</ul>" 1.160 +} 1.161 + 1.162 +# HTML5 audio/video attributes: autoplay loop controls preload="auto" 1.163 +# 1.164 +# Usage: audio_player [/path/audio.ogg|http://url] 1.165 +audio_player() { 1.166 + case "$1" in 1.167 + http://*) 1.168 + source="$1" title="$2" ;; 1.169 + *) 1.170 + filepath="$1" 1.171 + filename="$(basename "$1")" 1.172 + title="$(gettext "No track playing")" 1.173 + [ "$1" ] && title="${filename%.*}" 1.174 + # We need to get file via http url 1.175 + source="cache/play/$filename" 1.176 + rm -rf ${cache}/play && mkdir ${cache}/play 1.177 + [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename" ;; 1.178 + esac 1.179 + cat << EOT 1.180 +<div id="audio-player"> 1.181 + <div id="audio-title">$title</div> 1.182 + <audio controls autoplay="true" preload="auto"> 1.183 + <source src="$source"> 1.184 + <div>Your browser does not support audio</div> 1.185 + </audio> 1.186 +</div> 1.187 +EOT 1.188 +} 1.189 + 1.190 +playlist_player() { 1.191 + playlist="$1" 1.192 +} 1.193 + 1.194 +# Usage: video_player "/path/video.mp4" 1.195 +video_player() { 1.196 + filepath="$1" 1.197 + filename="$(basename "$1")" 1.198 + title="$(gettext "No video playing")" 1.199 + [ "$1" ] && title="${filename%.*}" 1.200 + # We need to get file via http url 1.201 + rm -rf ${cache}/play && mkdir ${cache}/play 1.202 + [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename" 1.203 + cat << EOT 1.204 +<div id="video-player"> 1.205 + <video width="560px" height="315px" 1.206 + controls autoplay="true" poster="images/poster.png"> 1.207 + <source src="cache/play/$filename"> 1.208 + <div>Unsupported video file format</div> 1.209 + </video> 1.210 + <div id="video-title">$title</div> 1.211 +</div> 1.212 +EOT 1.213 +} 1.214 + 1.215 +# 1.216 +# Media Box Tools 1.217 +# 1.218 + 1.219 +case " $(GET) " in 1.220 + *\ music\ *) 1.221 + html_header "$(gettext "Music")" 1.222 + nav_menu 1.223 + audio_player "$(GET play)" 1.224 + list_audio 1.225 + html_footer ;; 1.226 + 1.227 + *\ videos\ *) 1.228 + html_header "$(gettext "Videos")" 1.229 + nav_menu 1.230 + video_player "$(GET play)" 1.231 + list_videos 1.232 + html_footer ;; 1.233 + 1.234 + *\ playlists\ *) 1.235 + html_header "$(gettext "Playlists")" 1.236 + nav_menu 1.237 + playlist_player "$(GET play)" "$(GET info)" 1.238 + list_playlists 1.239 + html_footer ;; 1.240 + 1.241 + *\ radio\ *) 1.242 + html_header "$(gettext "Radio")" 1.243 + nav_menu 1.244 + audio_player "$(GET play)" "$(GET info)" 1.245 + list_radio 1.246 + html_footer ;; 1.247 + 1.248 + *\ settings\ *) 1.249 + html_header "$(gettext "Settings")" 1.250 + nav_menu 1.251 + cat << EOT 1.252 +<div id="settings"> 1.253 +<pre> 1.254 +Version : $version 1.255 +Cache : $(du -sh $cache | cut -d " " -f 1) 1.256 +Language : $LOCALE 1.257 +Config : $config 1.258 +Music : $MUSIC 1.259 +Videos : $VIDEOS 1.260 +EOT 1.261 + echo -n "Tools : " 1.262 + # Only small nd light tools! 1.263 + for tool in mediainfo normalize sox 1.264 + do 1.265 + if [ -x "/usr/bin/$tool" ]; then 1.266 + echo -n "$tool " 1.267 + fi 1.268 + done 1.269 + cat << EOT 1.270 + 1.271 + 1.272 +$(cat README) 1.273 +</pre> 1.274 +EOT 1.275 + # End of <div id="settings"> 1.276 + echo "</div>" 1.277 + html_footer ;; 1.278 + 1.279 + *) 1.280 + # Home page 1.281 + html_header "MediaBox" 1.282 + nav_page 1.283 + html_footer ;; 1.284 +esac && exit 0
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/mediabox Thu Feb 23 21:48:35 2017 +0100 2.3 @@ -0,0 +1,84 @@ 2.4 +#!/bin/sh 2.5 +# 2.6 +# Cmdline tool to run, debug and config CGI/SHell MediaBox 2.7 +# 2.8 +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License 2.9 +# 2.10 + 2.11 +config="$HOME/.config/mediabox" 2.12 +playlists="$config/playlists" 2.13 +#url="http://localhost/cgi-bin/mediabox/index.cgi" 2.14 +url="http://localhost/~pankso/cgi-bin/mediabox/index.cgi" 2.15 + 2.16 +# Internationalization 2.17 +. /usr/bin/gettext.sh 2.18 +export TEXTDOMAIN='mediabox' 2.19 + 2.20 +help() { 2.21 + cat << EOT 2.22 +$(gettext "MediaBox cmdline tool") 2.23 + 2.24 +$(gettext "Usage:") $(basename $0) [file|command] [file] 2.25 +$(gettext "Commands:") 2.26 + add-playlist Add a playlist into MediaBox database 2.27 + 2.28 +$(gettext "Examples:") 2.29 + $(basename $0) path/to/Music/audio.ogg 2.30 + $(basename $0) add-playlist path/to/playlist.m3u 2.31 + 2.32 +EOT 2.33 +} 2.34 + 2.35 +init() { 2.36 + install -m 0777 -d ${config} 2.37 + install -m 0777 -d ${playlists} 2.38 + cat > ${config}/mediabox.conf << EOT 2.39 +# CGI/SHell MediaBox configuration file 2.40 + 2.41 +LOCALE="$(locale | grep LANG= | cut -d "=" -f 2)" 2.42 + 2.43 +MUSIC="$HOME/Music" 2.44 +VIDEOS="$HOME/Videos" 2.45 + 2.46 +EOT 2.47 + echo "favorites.list|Favorites" > ${config}/playlists.list 2.48 + touch ${playlists}/favorites.m3u 2.49 + chmod 0666 \ 2.50 + ${config}/mediabox.conf \ 2.51 + ${config}/playlists.list \ 2.52 + ${playlists}/favorites.m3u 2.53 +} 2.54 + 2.55 +# 2.56 +# Handle commands|files|urls 2.57 +# 2.58 +case "$1" in 2.59 + 2.60 + add-playlist) 2.61 + [ ! -f "$2" ] && help && exit 0 2.62 + file=$(basename "$2") 2.63 + count=$(wc -l "$2" | cut -d ' ' -f 1) 2.64 + gettext "Adding playlist:"; echo -n " "; basename "$2" 2.65 + gettext "Playlist tracks:"; echo " $count" 2.66 + cp -f "$2" ${playlists} || exit 1 2.67 + cat >> ${config}/playlists.list << EOT 2.68 +$file|$(echo ${file%.m3u}) 2.69 +EOT 2.70 + ;; 2.71 + 2.72 + *.m3u) 2.73 + tazweb "${url}?playlists&play=$1" & ;; 2.74 + 2.75 + *.avi) 2.76 + tazweb "${url}?videos&play=$1" & ;; 2.77 + 2.78 + *.mp3|*.ogg|*.wav) 2.79 + tazweb "${url}?music&play=$1" & ;; 2.80 + 2.81 + "") 2.82 + [ -f "$config/mediabox.conf" ] || init 2.83 + tazweb --notoolbar "${url}?home=$HOME" & ;; 2.84 + 2.85 + *) help ;; 2.86 + 2.87 +esac && exit 0