iviewautodownload / python-iview / iview-cli

#!/usr/bin/env python

import sys, os, getopt, urllib2
import iview.config

def usage():
	-i, --index		prints the iView index (number is the series ID)
	-s, --series <id>	get the list of episodes for the series
	-p, --programme		prints the entire iView programme at once
				(warning: very slow!)
	-d, --download <url>	download an episode
				(pass the url you got from -s or -p)
	-t, --subtitles <url>	download subtitles in SRT format for an episode
				(pass the same url as for --download)
	-o, --output		specify a file to output to (use - for stdout)

	-a, --print-auth	print debug iView auth information
	-x, --proxy <host:port>	used specified SOCKS proxy
	-h, --help		this page""")

def config():
	except urllib2.HTTPError, error:
		print 'Error: could not retrieve an important configuration file from iView.'
		print 'Please make sure you are connected to the Internet.\n'
		print 'If iView works fine in your web browser, the iView API has possibly changed.' \
		      'Try to find an updated version of this program or contact the author.'
		print '\nDebug: could not load URL', error.url

def programme():
	"""	Downloads the iView index, and then prints each item in all series.
		This is a slow function. Using index() and subsequently cherrypicking
		a series to pass to series() is much faster.
	index = iview.comm.get_index()

	for series in index:
		print series['title'] + ':'

		items = iview.comm.get_series_items(series['id'])

		for item in items:
			print '\t' + item['title'] + '\t(' + item['url'] + ')'

def index():
	"""	Downloads the iView index, and prints the corresponding series and IDs.
	index = iview.comm.get_index()

	for series in index:
		print series['id'] + '\t' + series['title']

def series(series_id):
	items = iview.comm.get_series_items(series_id)

	for item in items:
		print item['title'] + '\t(' + item['url'] + ')'

def print_auth():
	auth = iview.comm.get_auth()
	print 'iView auth data:'
	print '\tToken:', auth['token']
	print '\tRTMP URL:', auth['rtmp_url']
	print '\tUnmetered:', str(auth['free'])

def download(url, output=None):
	iview.fetch.fetch_program(url, execvp=True, dest_file=output)

def subtitles(name, output=None):
	# In this function, we print >> sys.stderr because we have the possibility
	# of outputting subtitles to stdout, so we need to keep stdout clean.

	url = ''.join(name.split('.')[:-1])
	if output is not None:
		srt = output
		srt = ''.join(url.split('/')[-1]) + '.srt'

	if not srt == '-' and os.path.isfile(srt):
		print >> sys.stderr, 'Subtitles have already been downloaded to %s' % srt
		return False

	print >> sys.stderr, 'Downloading subtitles to %s...' % srt,

		subtitles = iview.comm.get_captions(url)
	except urllib2.HTTPError, error:
		print >> sys.stderr, 'failed'
		print >> sys.stderr, 'Got an error when downloading %s' % error.url
		return False
	except KeyboardInterrupt:
		return False

	if not srt == '-':
		f = open(srt, 'wb')
		f = sys.stdout


	if not srt == '-':


	print >> sys.stderr, 'done'

def parse_proxy_argument(proxy):
	"""	Try to parse 'proxy' as host:port pair.  Returns an error message
		if it cannot be understood.  Otherwise, it configures the settings
		in iview.config and returns None

	# this could be done much nicer with partition()
	# but it's not available in older versions of python
	parts = proxy.split(':')
	if len(parts) == 2:
			iview.config.socks_proxy_port = int(parts[1])
		except ValueError:
			return "Port '%s' is not numeric" % parts[1]
	elif len(parts) != 1:
		return "Too many ':' in '%s'" % proxy
	iview.config.socks_proxy_host = parts[0]

	return None

	opts, args = getopt.getopt(sys.argv[1:],
		['cache', 'subtitle=', 'subtitles=', 'download=', 'programme', 'program', 'index', 'series=', 'proxy=', 'output=', 'print-auth', 'help'])
	if not len(opts) > 0:
		raise getopt.GetoptError('Not enough arguments')
except getopt.GetoptError, err:

download_arg = None
subtitle_arg = None
output_arg = None

	for o, a in opts:
		if o in ('-c', '--cache'):
			iview.comm.cache = True
		elif o in ('-x', '--proxy'):
			err = parse_proxy_argument(a)
			if err is not None:
				print "Invalid proxy specification: %s\n" % err
		elif o in ('-d', '--download'):
			download_arg = a
		elif o in ('-t', '--subtitle', '--subtitles'):
			subtitle_arg = a
		elif o in ('-o', '--output'):
			output_arg = a
		elif o in ('-h', '--help'):

	# iview.comm (and therefore iview.fetch) can only be imported after
	# configuring the proxy as we need to override the socket before
	# importing urllib2
	import iview.fetch
	import iview.comm

	for o, a in opts:
		if o in ('-p', '--programme', '--program'):
		elif o in ('-i', '--index'):
		elif o in ('-s', '--series'):
		elif o in ('-a', '--print-auth'):

except KeyboardInterrupt: # don't traceback on a Ctrl+C

if download_arg is not None:
	download(download_arg, output_arg)
elif subtitle_arg is not None:
	subtitles(subtitle_arg, output_arg)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.