#!/usr/bin/python
# coding: euc-jp

import cgi
import cgitb
import pyExcelerator
import sys
from datetime import date, datetime, timedelta
from hice import final_output

cgitb.enable()

####################################################################
def int_or_missing(v,i):
    try:
        ret = v[index,i]
    except:
        ret = 0
    return ret

####################################################################
def is_missing(v,i):
    ret=1
    try:
        tmp = v[index,i]
    except:
        ret = 0
    return ret

####################################################################
def errorpage(str):
    print """Contetn-type: text/plain

%s
""" % (str)
    sys.exit(0)

####################################################################
cgiinput = cgi.FieldStorage()
request_id = cgiinput.getfirst("cid","")
request_size = cgiinput.getfirst("size","l")
use_nav = cgiinput.getfirst("nv","0")
try:
    fac = cgiinput.getfirst("f","-1")
except:
    fac=-1


if request_id == "":
    errorpage("no cid specified")

sheets = pyExcelerator.parse_xls('data/hoc_prog.xls')

d={}
for sheet_name, values in sheets:
    if sheet_name != u'2010' and sheet_name != u'2011':
        continue

    for index in xrange(1,1000):
        # 学部が書いてないところで終了
        try:
            d['faculty']=values[index,0].encode("euc-jp")
        except:
            break

        # course id (講義コード)を調べる
        try:
            d['code']=values[index,3].encode("euc-jp")
        except:
            continue

        if d['code'] != request_id:
            continue


        # 公開されているか？されてなければエラー。
        try:
            tmp=values[index,33]
        except:
            errorpage("This course is not published yet.")
            break;
    
        # 著作権問題な資料があるか？
        try:
            copyrighted = values[index,43];
            d['copyright_note']="""
<div class="copyright_note">
<p>授業中にスクリーンなどに提示された資料のうち、このサイトの動画内での使用許諾がとれていないものがあります。そのような場合、カメラをそらしたり、画像処理をしたりして資料を見えなくしています。あらかじめご了承ください。</p>
<p>また、このような資料も、著作権法第35条の「教育機関における複製等」の例外規定などにより、教室内では合法的に使用している事を申し添えておきます。</p>
</div>
"""
        except:
            d['copyright_note']="";


        d['program']=values[index,1].encode("euc-jp")
        d['course']=values[index,2].encode("euc-jp")
        d['prof']=values[index,4].encode("euc-jp")
        d['affil']=values[index,6].encode("euc-jp")
        try:
            tmpdate = date(1904,1,1)+timedelta(values[index,8])
            d['recdate']="%d年%d月%d日" % (tmpdate.year,tmpdate.month,tmpdate.day)
        except:
            d['recdate']="--年--月--日"

        d['pid']=values[index,7]

        d['is_kougi']=int_or_missing(values,35)
        d['is_jisshu']=int_or_missing(values,36)
        d['is_sonota']=int_or_missing(values,37)

        try:
            d['grade']=values[index,38]
        except:
            d['grade']=0

        d['is_zenki']=int_or_missing(values,39)
        d['is_kouki']=int_or_missing(values,40)
        d['is_shuchu']=int_or_missing(values,40)

        d['request_size'] = request_size
        if request_size == "l":
            d['flv_size'] = "640,360"
        else:
            d['flv_size'] = "320,180"

        try:
            tmpdate = date(1904,1,1)+timedelta(values[index,42])
            d['pubdate']="%d年%d月%d日" % (tmpdate.year,tmpdate.month,tmpdate.day) 
        except:
            d['pubdate']="--年--月--日"

        d['contents']="""
<div class="video">
<h1>%(prof)s「%(course)s」</h1>
<h2>%(faculty)s%(program)s</h2>

<p id="player" style="text-align:center">
</p>
<script type="text/javascript">setmovie_rtmp(%(flv_size)s,"%(code)s-%(request_size)s");</script>

%(copyright_note)s
<hr>
<p>このビデオを見るには、<a href="http://www.adobe.com/jp/products/flashplayer/">Adobe Flash Player</a> が必要です。Adobe Flash Player をお持ちでない場合は、以下のバナーをクリックしてインストールをして下さい。</p>

        <p><a href="http://www.adobe.com/go/JP-H-GET-FLASH"><img src="http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png" alt="Get Flash Player"></a></p>
</div>
""" % d

        html_l = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="ja">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-jp">
<meta http-equiv="content-style-type" content="text/css">
<link REL="stylesheet" href="./files/hoc.css" TYPE="TEXT/CSS">
<title>%(course)s</title>
<script type="text/javascript" src="./files/swfobject.js"></script>
<script type="text/javascript" src="./files/hoc.js"></script>
</head>
<body>
<p class="heading">広島大学 Web 公開授業</p>
%(contents)s
</body>
</html>
""" % d


        if use_nav=="1":
            final_output(int(fac),d['contents'],True,d['course'])
        else:
            print "Content-type: text/html; charset=EUC-JP\n"
            print html_l


        sys.exit(0)
        
errorpage("no such cid")

#
