OSDN Git Service

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