#! /usr/bin/python
"""
ncfiles.py -- A Python program to download NCast archive files

This program allows downloading of archive files from one or more Telepresenters.
It requires installation and use of the Python programming language. The most
recent versions of the language may be installed by visiting "www.python.org" and
downloading the correct binaries for Windows, Linux, Macs or other platforms.

All archive files are downloaded into the directory where the program was started.

Sample usage:

 ncfiles.py telepresenter.ncast.com          Download all files from a single host
 ncfiles.py -r telepresenter.ncast.com       Download all files and then remove them
 ncfiles.py 192.168.0.5 192.168.0.6          Download from multiple hosts
 ncfiles.py -p adam telepresenter.ncast.com  Download using password "adam"

Complete specification:

 ncfiles.py -d -f hostfile -h -p password -r -v --debug --file=hostfile --help --pswd=password --remove --version host host1 host2 ...

 where

 -d, --debug          Turn debug statements on
 -f, --file=hostfile  Use a list of hosts from a file (see below)
 -h, --help           Print usage information
 -p, --pswd=password  Use "password"
 -r, --remove         Remove archive file from the host after download
 -v, --version        Report program version

 host ...             One or more hosts to download from

The list of hosts is a text file (e.g. created with Notepad) that contains a
list of hosts to download from and optionally password information:

    # This is a sample host file

    # Optional password line for supplying a password

    pswd=adam

    # Hosts to download from

    192.168.0.5
    telepresenter.ncast.com
    my.other.boxes.com

To run this program under Windows you may use a command line, such as:

C:> "C:\Program Files\Python24\python.exe" ncfiles.py 192.168.0.5

A simpler alternative is to create a shortcut to "ncfiles.py" and then using the
Properties tab for the shortcut enter the required arguments:

"C:\Documents and Settings\owner\My Documents\Python\ncfiles.py" 192.168.0.5

Please send comments or questions to "info@ncast.com".

Copyright (2005) NCast Corporation
All rights reserved

"""

import sys
import os, os.path
import time
import getopt
import string
import urllib
import urllib2
import socket

def Download(host, user, pswd, realm, remove):

# Create an OpenerDirector with support for Basic HTTP Authentication...

	auth_handler = urllib2.HTTPBasicAuthHandler()

	auth_handler.add_password(realm, host, user, pswd)

	opener = urllib2.build_opener(auth_handler)

# ...and install it globally so it can be used with urlopen.

	urllib2.install_opener(opener)

# Get list of files to download


	url = "http://" + host + "/backup/list.cgi"

	try:
	       	filelist = urllib2.urlopen(url)
	except urllib2.URLError, msg:
		print "ncfiles: Urllib2 error (%s)" % msg
		return False
        except socket.error, (errno, strerror):
	        print "ncfiles: Socket error (%s) for host %s (%s)" % (errno, host, strerror)
		return False

# Process the list of files to download

	files = []
	
	for line in filelist.read().split("\n"):
		file = line.strip(' \n\r')
		if file == "":
			break
                files.append(file)

        filelist.close()
											     
	if debug:
		print "ncfiles: Download list", files

	if len(files) == 0:
		print "ncfiles: No files to download"
		return True
		
# Download each file

	for mp4 in files:

		url = "http://" + host + "/backup/download/" + mp4 

		print "ncfiles: Downloading", url
		
		try:
		       	filein = urllib2.urlopen(url)
		except urllib2.URLError, msg:
			print "ncfiles: Urllib2 error (%s)" % msg
			return False
		except socket.error, (errno, strerror):
			print "ncfiles: Socket error (%s) for host %s (%s)" % (errno, host, strerror)
			return False

		fileout = open(mp4, "wb")

		while True:
			try:
				bytes = filein.read(1024000)
				fileout.write(bytes)
			except IOError, (errno, strerror):
				print "ncfiles: I/O error(%s): %s" % (errno, strerror)
				sys.exit(2)
						 
			if bytes == "":
				break

		filein.close()
		fileout.close()

# Download the xml file associated with the mp4 file

		url = "http://" + host + "/backup/download/" + mp4[:-4] + ".xml" 

		if debug:
			print "ncfiles: Downloading", url
		
		try:
		       	filein = urllib2.urlopen(url)
		except urllib2.URLError, msg:
			print "ncfiles: Urllib2 error (%s)" % msg
			return False
		except socket.error, (errno, strerror):
			print "ncfiles: Socket error (%s) for host %s (%s)" % (errno, host, strerror)
			return False

		fileout = open(mp4[:-4] + ".xml", "w")

		bytes = filein.read()
		fileout.write(bytes)

		filein.close()
		fileout.close()

# Remove file if requested

		if remove:
		
			url = "http://" + host + "/backup/remove.cgi?file=" + mp4

			if debug:
				print "ncfiles: Removing", url
		
			try:
			       	filerm = urllib2.urlopen(url)
			except urllib2.URLError, msg:
				print "ncfiles: Urllib2 error (%s):" % msg
				return False
			except socket.error, (errno, strerror):
				print "ncfiles: Socket error (%s) for host %s (%s)" % (errno, host, strerror)
				return False

			filerm.close()

	return True

# Main Program -------------------------------------------------------

hostlist = []
debug = False
inputfile = None
user = "backup"
pswd = "ncast"
realm = "NCast M3"
remove = False

def Usage():
        print "Usage: ncfiles.py -d -f hostfile -h -p password -r -v --debug --file=hostfile --help --pswd=password --remove --version host host1 host2 ..."

try:
    options, args = getopt.getopt(sys.argv[1:], 'df:hp:rv:', ['debug', 'file=', 'help', 'pswd=', 'remove', 'version'])
except getopt.GetoptError:
    Usage()
    sys.exit(2)

for o, a in options:
    if o in ("-d", "--debug"):
        debug = True
    if o in ("-h", "--help"):
        Usage()
        sys.exit()
    if o in ("-p", "--pswd"):
        pswd = a
    if o in ("-r", "--remove"):
        remove = True
    if o in ("-v", "--version"):
        print "ncfiles.py Version 1.0"
        sys.exit()
    if o in ("-f", "--file"):
        inputfile = a
        
hostlist.extend(args)

if inputfile :
    try:
        f = open(inputfile, 'r')
        for line in f:
            host = line.strip(' \n\r')
            if host == "":
                continue
            if host[0] == '#':
                continue
            if host[0:4] == 'pswd':
                pswd = host[5:]
                continue
            print "ncfiles: Adding host", host, "to list of hosts"
            hostlist.append(host)
        f.close()
    except IOError, (errno, strerror):
        print "ncfiles I/O error(%s): %s" % (errno, strerror)
        sys.exit(1)
        
if len(hostlist) == 0 :
    print "ncfiles: No hosts listed for download"
    sys.exit(1)
else:
    print "ncfiles: Downloading from these hosts:", hostlist

for host in hostlist:
    if remove:
	    removetext = "and removing files"
    else:
	    removetext = "and keeping files"
    print "ncfiles: Downloading from host", host, removetext

    result = Download(host, user, pswd, realm, remove)

    if not result:
	    print "ncfiles: Download error on host", host

print "ncfiles: Download complete"
