#!/usr/bin/python
# coding: UTF-8
# iRec10EPG Rec10 iEPG supporting tool
#Copyright 2009 Yukikaze
import os
import ConfigParser
import codecs
import webbrowser
import sys
import re
import urllib
mypath=os.path.dirname(sys.argv[0])
def rec10web(webpath,title,station,btime,etime):
    browserpath="title="+title+"&btime="+btime+"&etime="+etime+"&station="+station
    browserpath=urllib.quote_plus(browserpath,"=&")
    browserpath=webpath+"?"+browserpath
    print browserpath

    webbrowser.open(browserpath,new=2)
def iepg2rec10webopts(path):
    station=""
    year=""
    month=""
    date=""
    start=""
    end=""
    title=""

    for line in codecs.open(path,'r','shift_jis'):
        if  re.search('station:',line):
            station=line.replace("station:","")
            station=station.replace(" ","")
            station=station.replace("\r\n","")
        elif re.search('year:',line):
            year=line.replace("year:","")
            year=year.replace(" ","")
            year=year.replace("\r\n","")
        elif re.search('month:',line):
            month=line.replace("month:","")
            month=month.replace(" ","")
            month=month.replace("\r\n","")
        elif re.search('date:',line):
            date=line.replace("date:","")
            date=date.replace(" ","")
            date=date.replace("\r\n","")
        elif re.search('start:',line):
            start=line.replace("start:","")
            start=start.replace(" ","")
            start=start.replace("\r\n","")
        elif re.search('end:',line):
            end=line.replace("end:","")
            end=end.replace(" ","")
            end=end.replace("\r\n","")
        elif re.search('program-title:',line):
            title=line.replace("program-title:","")
            title=title.replace(" ","")
            title=title.replace("\r\n","")
    ret={}
    ret['title']=title.encode('UTF-8')
    ret['btime']=year+"-"+month+"-"+date+" "+start+":00"
    ret['btime']=ret['btime'].encode('UTF-8')
    ret['etime']=year+"-"+month+"-"+date+" "+end+":00"
    ret['etime']=ret['etime'].encode('UTF-8')
    ret['station']=station.encode('UTF-8')
    return ret


confp=ConfigParser.SafeConfigParser()
Conf=os.path.join(mypath, 'setting.ini')
confp.read(Conf)
rec10path=confp.get('path','rec10web')
print rec10path
argvs=sys.argv
webopt=iepg2rec10webopts(argvs[1])
print webopt

rec10web(rec10path,webopt['title'],webopt['station'],webopt['btime'],webopt['etime'])
