view iviewget.py @ 2:6edc508e0775

Try and run flvstreamer on init and exit if it fails. It's very annoying if we go and select a bunch of stuff and the user hasn't remember to edit the path for it..
author Daniel O'Connor <darius@dons.net.au>
date Thu, 20 Aug 2009 17:34:50 +0930
parents fa7ca67af8c9
children 34471d354603
line wrap: on
line source

#!/usr/bin/env python

import iview
import os.path
import re
import subprocess
import sys
import time

outpath = '.'
outext = 'flv16x9'
flvstreamerpath = '/home/darius/projects/flvstreamer/flvstreamer_x86'
flvpattern = re.compile("FLVStreamer v")

def listChannels(iview):
    i = 1
    print "Id %-30s %-35s" % ('Name', 'Description')
    for c in iview.channels:
        print "%2d %-30s %-35s" % (i, c.name, c.descr)
        i += 1

def listSeries(chan):
    i = 1
    for s in chan.series:
        if len(s.title) > 1:
            title = s.title[1]
        else:
            title = s.title[0]
        print "%3d %s" % (i, title)
        i += 1

def getInt(f):
    ans = f.readline()
    try:
        i = int(ans)
    except ValueError:
        return None

    return i

def getSeries(iview, series, outfile = None):
    cmd = [flvstreamerpath]

    if outfile == None:
        outfile = outpath + '/' + series.asset.replace('/', '-') + '.' + outext
    cmd += iview.genFetchCmd(series, outfile)
    p = subprocess.Popen(cmd)
    while True:
        res = p.poll()
        if res == None:
            time.sleep(0.1)
        else:
            return res

def checkFLVStreamer():
    try:
        s = subprocess.Popen(flvstreamerpath, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    except OSError:
        print "Couldn't execute FLVstreamer"
        return False
    while s.poll() != 1:
        time.sleep(0.1)
        
    out, err = s.communicate()
    if flvpattern.match(err) == None:
        print "Unrecognised output from FLVstreamer"
        return False

    return True

if __name__ == "__main__":
    if not checkFLVStreamer():
        sys.exit(1)

    iview = iview.IView()
    if iview.metered:
        print "WARNING: iview is metered"
        print

    slist = []
    while True:
        chan = None
        listChannels(iview)
        while True:
            print "Please select a channel number (0 to exit): ",
            i = getInt(sys.stdin)
            if i != None and i >= 0 and i <= len(iview.channels):
                break
            print "Invalid entry, try again"

        if i == 0:
            break

        cidx = i - 1
        chan = iview.channels[cidx]
        chan.getSeries()

        listSeries(chan)
        while True:
            print "Please select which series to download (space separated numbers):"
            res = sys.stdin.readline()
            for r in res.split():
                try:
                    i = int(r)
                except ValueError:
                    i = None

                if i != None and i >= 0 and i <= len(chan.series):
                    slist += [chan.series[i - 1]]
                
            if len(slist) > 0 or i == 0:
                break
            print "Invalid entry, try again"

    if len(slist) == 0:
        print "Nothing to download"
        sys.exit(0)

    print "Downloading %d items" % (len(slist))
    for series in slist:
        print "Fetching " + series.asset
        res = getSeries(iview, series)
        print "Result is " + str(res)
        # Re-auth or the server won't love us
        iview.getAuth()