2010-11-11

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 some stuff
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(urlsglobals(),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
    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.listdirparse_dir )
      #I like things in alphabetical order
      dirlist.sort()
      for asset in dirlist:
        asset_full_path parse_dir+"/"+asset
        asset_short_pathshort_path_prefix+asset
        if os.path.isdirasset_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 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

  1. save the code http://www.jezra.net/downloads/blogcode/audio_browser.tgz
  2. edit line 19 to point to a directory of audio files
  3. run the code with python
  4. point your browser to http://localhost:8080
  5. email me to let me know what went wrong
  6. ?
  7. 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

Comments
2010-11-12 kabniel:
Cool, i've yet to play with python web apps.

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'
2010-11-12 jezra:
excellent! I can use the python socket module to communicate through the named piped. Blend this with some pyjamas ajaxy-ness and I'll be a happy camper.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
6 minus 5
Answer:
required
  • Tags:
  • Python
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008