#!/usr/bin/python

# By hads <epg@nice.net.nz>
# Released under the MIT license
# Last updated 2010-03-14

# TODO
# - Check cache option complies with mythtv (when it's implemented)
# - Implement apiconfig (when it's implemented)

import os, sys
import httplib
import logging
import time

from datetime import datetime, timedelta
from optparse import OptionParser
from cStringIO import StringIO
from gzip import GzipFile
from urlparse import urlparse

NAME = 'tv_grab_nz-py'
VERSION = '0.2.7'
DESCRIPTION = 'New Zealand (py)'

SOURCES = (
    'http://epg.org.nz/freeview.xml.gz',
)

# setup logging
log = logging.getLogger(NAME)
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
log.addHandler(ch)

try:
    try:
        from xml.etree import cElementTree as ElementTree
    except ImportError:
        from elementtree import ElementTree
except ImportError:
    log.critical('ElementTree is required and is not found')
    sys.exit(2)

def doDownload(source):
    """
    Download a gzipped file and return a string
    """
    host = urlparse(source)[1]
    url = urlparse(source)[2]
    log.info('Downloading data from %s...' % host)
    
    h = httplib.HTTPConnection(host)
    headers = {
        'User-Agent': '%s %s' % (NAME, VERSION),
        'Accept-encoding': 'gzip',
    }

    h.request('GET', url, headers=headers)
    res = h.getresponse()

    if res.status != 200:
        return False

    data = res.read()
    log.info('Done')

    return GzipFile('', 'r', 0, StringIO(data)).read()

def getListings(cache=None):
    """
    Loop through listing sources and download them
    """
    if cache:
        try:
            data = open(cache).read()
        except IOError:
            pass
        else:
            return data
    for source in SOURCES:
        res = doDownload(source)
        if res and len(res) > 0:
            return res
        else:
            log.error('Invalid data')

class XMLTVConfig(object):
    """
    Parses and writes configuration files in the XMLTV format
    """
    
    channels = []
    enabled_channels = []
    
    def __init__(self, conf_file):
        self.conf_file = conf_file
        try:
            f = open(conf_file)
        except IOError:
            self.exists = False
        else:
            self.exists = True
            i = 1
            for line in f:
                line = line.strip()
                if line and line[0] != '#':
                    if line.startswith('channel'):
                        if '=' in line:
                            key, value = line.split('=', 1)
                            self.channels.append(value)
                            self.enabled_channels.append(value)
                        elif '!' in line:
                            key, value = line.split('!', 1)
                            self.channels.append(value)
                    else:
                        log.info('Ignoring bad line in config file [%s:%s]', conf_file, i)
                i += 1
            f.close()

    def write(self):
        try:
            f = open(self.conf_file, 'w')
        except IOError:
            log.error("Couldn't write config file")
        else:
            for channel in self.channels:
                if channel in self.enabled_channels:
                    f.write('channel=%s\n' % channel)
                else:
                    f.write('channel!%s\n' % channel)
            f.close()

# Setup command line options
parser = OptionParser(version='%prog ' + str(VERSION))
parser.set_defaults(quiet=False, capabilities=False, debug=False)
parser.add_option('--quiet', action='store_true',
    help='be quiet, don\'t output status information.')
parser.add_option('--debug', action='store_true', dest='debug',
    help='output debugging information.')
parser.add_option('--capabilities', action='store_true',
    help='show this grabbers capabilities.')
parser.add_option('--configure', action='store_true',
    help='manually configure this grabber.')
parser.add_option('--config-file',
    help='Use configuration file CONFIG_FILE.')
parser.add_option('--description', action='store_true',
    help='show desciption of this grabber.')
parser.add_option('--preferredmethod', action='store_true',
    help='show the preferred download method of this grabber.')
parser.add_option('--days', type='int',
    help='supply data for DAYS days.')
parser.add_option('--offset', type='int',
    help='output data for day today plus OFFSET days.')
parser.add_option('--output',
    help='send output to file OUTPUT, the default is STDOUT.')
parser.add_option('--cache',
    help='cache XML data to file CACHE')
(options, args) = parser.parse_args()

if options.debug and options.quiet:
    parser.error('options --debug and --quiet are mutually exclusive')

if options.capabilities:
    print 'baseline'
    print 'manualconfig'
    print 'preferredmethod'
    print 'cache'
    sys.exit()

if options.description:
    print DESCRIPTION
    sys.exit()

if options.preferredmethod:
    print 'allatonce'
    sys.exit()

if options.config_file:
    config_file = options.config_file
else:
    config_dir = os.path.expanduser('~/.xmltv/')
    # Create config directory if it doesn't exist
    if not os.path.isdir(config_dir):
        try:
            os.mkdir(config_dir)
        except:
            log.critical('Failed to create config directory: %s' % config_dir)
            sys.exit(2)
    config_file = os.path.join(config_dir, '%s.conf' % NAME)

# setup configuration
conf = XMLTVConfig(config_file)

if options.debug:
    ch.setLevel(logging.DEBUG)

if options.quiet:
    ch.setLevel(logging.CRITICAL)

if options.configure:
    available_channels = []
    new_channels = []
    new_enabled_channels = []
    
    text = getListings()
    log.info('Parsing channel data...')
    doc = ElementTree.parse(StringIO(text)).getroot()
    
    for element in doc:
        if element.tag == 'channel':
            new_channels.append(element.get('id'))
            available_channels.append((element.get('id'), element[0].text))

    log.info('Done (%s channels)' % len(available_channels))
    print
    print 'Please select the channels you wish to use for'
    print 'this source by pressing y or n when prompted:'
    print
    if conf.exists:
        log.warning('Config file already exists, abort (CTRL-C) if you wish to keep the current config')
    print
    for channel in available_channels:
        use = raw_input('Use channel %s (%s)? [y/N]' % (channel[1], channel[0]))
        if use.lower() == 'y':
            new_enabled_channels.append(channel[0])
    conf.channels = new_channels
    conf.enabled_channels = new_enabled_channels
    conf.write()
    sys.exit()

if not conf.exists:
    log.critical('Not configured! Perhaps you meant to run with --config-file or --configure?')
    sys.exit(2)

today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)

if options.offset:
    offset = timedelta(days=options.offset)
    start_listings = today + offset
    log.debug('Showing listings from %s' % start_listings)
else:
    start_listings = today

if options.days:
    days = timedelta(days=options.days)
    end_listings = start_listings + days
    log.debug('Showing listings until %s' % end_listings)
else:
    end_listings = None

text = getListings(cache=options.cache)

if not text:
    log.critical('No listings data found!')
    sys.exit(2)

log.info('Parsing listings...')
doc = ElementTree.parse(StringIO(text)).getroot()

newdoc = ElementTree.Element('tv', doc.attrib)

for element in doc:
    if element.tag == 'channel' and element.get('id') in conf.enabled_channels:
        newdoc.append(element)
    if element.tag == 'programme' and element.get('channel') in conf.enabled_channels:
        # build datetime from start attribute of programme
        programme_start = datetime.fromtimestamp(
            time.mktime(
                time.strptime(element.get('start')[:-6], '%Y%m%d%H%M%S')
            )
        )
        if programme_start > start_listings:
            if end_listings:
                if programme_start < end_listings:
                    newdoc.append(element)
            else:
                newdoc.append(element)

output = ElementTree.tostring(newdoc)

log.info('Done')

if options.cache:
    open(options.cache, 'w').write(output)

if options.output:
    open(options.output, 'w').write(output)
else:
    print output


