Recently, I've been playing around with web.py, a simple yet very powerful web framework for writing web applications using python, and I thought I would share my first test of web.py that goes beyond "hello world". I discussed this briefly on Episode 13 of Frostcast
what it do
In a nutshell, the code will produce a browseable list of directories and audio files.
When run, the code will start a web server on port 8080 that dynamically displays the list of directories and files. Clicking on a link to an audio file will cause the audio file to be played on the server machine using sap. One could easily use any command line audio player of their choice in place of sap; such as mplayer.
Enter the Python
import web
import subprocess
import os
import urllib
#show the errors as a lot of debug info
web.internalerror = web.debugerror
#what URL do we need to handle?
urls = ("/","list",
"/list/(.*)","list",
"/play/(.*)","play",
"/stop","stop",
)
#what extensions do my audio files have?
good_extensions = (".mp3",".ogg")
#create the app
app = web.application(urls, globals(),True)
#where is the music?
music_dir = "/storage/music/"
class stop:
def GET(self):
#kill sap
subprocess.call("killall sap",shell=True)
#redirect to list
raise web.seeother("/")
class play:
def GET(self,asset):
#kill sap
subprocess.call("killall sap",shell=True)
#launch the sap
f = music_dir+asset
#return f
command = "sap \"%s\"" % (f)
print command
subprocess.Popen(command,shell=True )
#recdirect to list
raise web.seeother("/")
class list:
def GET(self,dir=None):
body=""
dir_list=""
file_list=""
short_path_prefix=""
#determine which directory we should look in
if dir!=None:
short_path_prefix=dir+"/"
parse_dir = music_dir+short_path_prefix
#if this directory exists, we need to show some shit
if os.path.exists(parse_dir):
body+="<b>%s</b> <a href='/stop'>stop</a><br>" % (parse_dir)
#get the dir contents
dirlist = os.listdir( parse_dir )
#I like things in alphabetical order
dirlist.sort()
for asset in dirlist:
asset_full_path = parse_dir+"/"+asset
asset_short_path= short_path_prefix+asset
if os.path.isdir( asset_full_path ):
encoded_path = urllib.pathname2url(asset_short_path);
dir_list+="<a href='/list/%s'>%s</a><br>" % (encoded_path,asset)
else:
#check the extension
(f,e) = os.path.splitext(asset)
#TODO: make this case insensitive
if e in good_extensions:
encoded_path = urllib.pathname2url(asset_short_path);
file_list+="<a href='/play/%s'>%s</a><br>" % (encoded_path,asset)
else:
body+="%s doesn't exist" % (parse_dir)
#add the links to directories and files
body+=dir_list
body+=file_list
return "<html><title>Music Browser</title><body>%s</body></html>" % body
if __name__ == "__main__":
app.run()
Give it a go
- save the code http://www.jezra.net/downloads/blogcode/audio_browser.tgz
- edit line 19 to point to a directory of audio files
- run the code with python
- point your browser to http://localhost:8080
- email me to let me know what went wrong
- ?
- Did you remember to install web.py?
Moving Forward
Welcome to "Shot of Code", this is only the start of the code. So where can this go? AKA, what would I like this to do. Well I can think of a few things.
- a web interface for playing videos on my media machine. This will require some some way to send commands to Mplayer for play, pause, fast-forward, etc.
- a web front end to a command line audcatcher (sorry, I couldn't resist).
- recreate shnerkel with a web interface.
The reason that all of my code ideas are audio or video related is because I want to be able to control my media machine with a web interface and not have to SSH into the machine to control media playback.
Now quit reading, and go sweeten that sauce!
jezra
I played a bit with mplayer control from bash scripts a few weeks ago and a combination of mplayer's 'slave=yes' and 'input=file=/path/to/fifo' options makes it pretty easy to control mplayer by sending commands like 'echo "play" > /path/to/fifo'