#!/usr/bin/python
# coding: UTF-8
# Rec10 TS Recording Tools
# Copyright (C) 2009 Yukikaze
#import sys # モジュール属性 argv を取得するため
import os
import re
import time

import chdb
import configreader
import tv2ts

def write(pout, ch):
    """
    tsを取得してepgの入ったxmlとして書き出す
    """
    print "ts2epg処理"
    bctype = chdb.chsearch(ch)['bctype']
    print ch + ":" + bctype
    timet = "90"
    if re.search(u'cs', bctype):
        mode = "/CS"
        timet = "180"
    elif re.search(u'bs', bctype):
        mode = "/BS"
        timet = "240"
    else:
        mode = chdb.bctypesearch(u'te' + ch)['ontv']
    epgdump = configreader.getpath("epgdump")
    if os.access(pout + ".ts", os.F_OK):
        os.remove(pout + ".ts")
    time.sleep(10)
    print type(ch)
    print ch
    tv2ts.tv2tsmix(pout + ".ts", ch, timet)
    exe = "export LANG=ja_JP.UTF-8 && nice -n 15 " + epgdump + " " + mode + " " + pout + ".ts " + pout
    print exe
    os.system(exe)
    time.sleep(10)
        

