OSDN Git Service

implement ch scan system.(alpha)
[rec10/rec10-git.git] / rec10 / trunk / src / xml2db_dom.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5
6 import xml.dom.minidom
7 import datetime
8 import re
9 import traceback
10
11 import zenhan
12 import chdb
13 import auto_rec
14 import recdb
15 import rec10d
16 import n_gram
17 import recdblist
18 import epgdb
19 import status
20 def getText(elm):
21     nodelist = elm.childNodes
22     rc = ""
23     # 全てのノードに対して
24     for node in nodelist:
25         # テキストノードなら値を取得
26         if node.nodeType == node.TEXT_NODE:
27             rc = rc + node.data
28     return rc
29 def getText_item(obj):
30     rc=""
31     for o in obj:
32         rc=rc+getText(o)
33     return rc
34 def writemultitvDB(bctype,tvlists):
35     rec10d.rec10db.new_epg_timeline(bctype)
36     for channel,start,stop,title,desc,longdesc,category in tvlists:
37         rec10d.rec10db.add_epg_timeline(bctype, channel, start, stop, title, desc, longdesc, category)
38 def writemultichDB(chlists):
39     for chtxt,dn in chlists:
40         rec10d.rec10db.update_chname_by_chtxt_epg_ch(chtxt,dn)
41 def xml2db_dom(xmlpath, bctype):#bctypeは放送種別で'TE'(地デジ)BS,CSがある。地デジの場合は te数字 が入る
42     dtb=datetime.datetime.now()
43     dom=xml.dom.minidom.parse(file(xmlpath))
44     chlist=[]
45     tvlist=[]
46     bayes={}
47     rHisch=re.compile(u"『([^『]+)』(.+)\Z")##History CHのタイトル#01  exp:「#01説明」をタイトルに含める
48     for ch in dom.getElementsByTagName('channel'):
49         chtxtt=ch.getAttribute("id")
50         chname=ch.getElementsByTagName("display-name").item(0).childNodes[0].data
51         chlist.append([chtxtt,chname])
52     for tv in dom.getElementsByTagName('programme'):
53         channel=zenhan.toHankaku_ABC123(tv.getAttribute("channel"))
54         start=tv.getAttribute("start").replace(" +0900", "")
55         stop=tv.getAttribute("stop").replace(" +0900", "")
56         title=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("title"))).replace(",", "_")
57         desc=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("desc")))
58         longdesc=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("longdesc")))
59         category=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("category")))
60         title=addTitle_Subtitle(rHisch,1,title,desc)
61         tch=chdb.chtxtsearch(channel)
62         if tch!=None:
63             if tch.get('bctype',"")==bctype:
64                 bt=bayes.get(tch['chtxt'],["","",0])
65                 bt2=[bt[0]+title+" ",bt[1]+desc+" "+longdesc+" ",bt[2]+1]
66                 bayes[tch['chtxt']]=bt2
67                 tvlist.append([channel,start,stop,title,desc,longdesc,category])
68                 p=auto_rec.calc_key(tch['chtxt'], title,desc+" "+longdesc)
69                 if p>2000:
70                     bttt=datetime.datetime.strptime(start,"%Y%m%d%H%M%S")
71                     bttt=bttt-datetime.timedelta(seconds=1200)
72                     bttime=bttt.strftime("%Y-%m-%d %H:%M:%S")
73                     ettt=datetime.datetime.strptime(stop,"%Y%m%d%H%M%S")
74                     ettt=ettt+datetime.timedelta(seconds=1200)
75                     ettime=ettt.strftime("%Y-%m-%d %H:%M:%S")
76                     chs=rec10d.rec10db.select_bytime_bychtxt_all_timeline(bttime,ettime,tch['chtxt'])
77                     p2=0
78                     if len(chs)>0:
79                         for ch in chs:
80                             p2t=n_gram.trigram(ch['title'],title)
81                             if p2t>p2:
82                                 p2=p2t
83                     if p2<350:
84                         if status.getSettings_auto_bayes()==1:
85                             if p2<200:
86                                 maxnum=0
87                                 if len(ch['ch'])>2:
88                                     maxnum=epgdb.count_schedule_timeline(bttime, ettime)[1]
89                                     maxnum=int(configreader.getenv("bscs_max"))-maxnum
90                                 else:
91                                     maxnum=epgdb.count_schedule_timeline(bttime, ettime)[0]
92                                     maxnum=int(configreader.getenv("te_max"))-maxnum
93                                 if maxnum>0:
94                                     topt=status.getSettings_auto_opt()
95                                     recdb.recreserv(title,tch['chtxt'], bttime, ettime,topt)
96                         else:
97                             recdb.add_auto_bayes(tch['chtxt'],title,start,stop,p)
98
99     dom.unlink()
100     if len(chlist) > 0:
101         writemultichDB(chlist)
102         chlist = []
103         if len(tvlist) > 0:
104             writemultitvDB(bctype,tvlist)
105         tvlist = []
106         for ct,list in bayes.items():
107             auto_rec.update_recall(ct,list[0],list[1],list[2])
108             auto_rec.update_recall("ALL",list[0],list[1],list[2])
109     dtb=datetime.datetime.now()-dtb
110     recdblist.printutf8(bctype + u" epg取り出し終了")
111     recdblist.printutf8(str(dtb.days * 24 * 60 * 60 + dtb.seconds)+u"seconds taken for updating bayes-auto DB .")
112 def addTitle_Subtitle(recompiled,num,title,exp):
113     try:
114         tST=recompiled.match(exp)
115         newtitle=u""
116         if tST:
117             newtitle=title+u"『"+tST.group(num)+u"』"
118         else:
119             newtitle=title
120     except Exception, inst:
121         recdblist.Commonlogex("Error", "addTitle_Subtitle(xml2db_dob.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
122         newtitle=title
123     return newtitle