OSDN Git Service

add dist
[rec10/rec10-git.git] / dist / trunk / rec10 / 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 def writeMultiLogoDB(logolists):\r
42     #[type,sv,pngdata]\r
43     for type,sv,pngdata in logolists:\r
44         chtxt=chdb.searchCHFromCsch(sv)['chtxt']\r
45         if len(chtxt)>0:\r
46             rec10d.rec10db.change_logodata_epg_ch(chtxt,type,pngdata)\r
47 def xml2db_dom(xmlpath, bctype):#bctypeは放送種別で'TE'(地デジ)BS,CSがある。地デジの場合は te数字 が入る\r
48     dtb=datetime.datetime.now()\r
49     dom=xml.dom.minidom.parse(file(xmlpath))\r
50     chlist=[]\r
51     tvlist=[]\r
52     bayes={}\r
53     rHisch=re.compile(u"『([^『]+)』(.+)\Z")##History CHのタイトル#01  exp:「#01説明」をタイトルに含める\r
54     for ch in dom.getElementsByTagName('channel'):\r
55         chtxtt=ch.getAttribute("id")\r
56         chname=ch.getElementsByTagName("display-name").item(0).childNodes[0].data\r
57         chlist.append([chtxtt,chname])\r
58     for tv in dom.getElementsByTagName('programme'):\r
59         channel=zenhan.toHankaku_ABC123(tv.getAttribute("channel"))\r
60         start=tv.getAttribute("start").replace(" +0900", "")\r
61         stop=tv.getAttribute("stop").replace(" +0900", "")\r
62         title=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("title"))).replace(",", "_")\r
63         desc=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("desc")))\r
64         longdesc=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("longdesc")))\r
65         category=zenhan.toHankaku_ABC123(getText_item(tv.getElementsByTagName("category")))\r
66         title=addTitle_Subtitle(rHisch,1,title,desc)\r
67         tch=chdb.searchCHFromChtxt(channel)\r
68         if tch!=None:\r
69             if tch.get('bctype',"")==bctype and not tch['visible']==0:\r
70                 bt=bayes.get(tch['chtxt'],["","",0])\r
71                 bt2=[bt[0]+title+" ",bt[1]+desc+" "+longdesc+" ",bt[2]+1]\r
72                 bayes[tch['chtxt']]=bt2\r
73                 tvlist.append([channel,start,stop,title,desc,longdesc,category])\r
74                 p=auto_rec.calcKey(tch['chtxt'], title,desc+" "+longdesc)\r
75                 if p>2000:\r
76                     bttt=datetime.datetime.strptime(start,"%Y%m%d%H%M%S")\r
77                     bttt=bttt-datetime.timedelta(seconds=1200)\r
78                     bttime=bttt.strftime("%Y-%m-%d %H:%M:%S")\r
79                     ettt=datetime.datetime.strptime(stop,"%Y%m%d%H%M%S")\r
80                     ettt=ettt+datetime.timedelta(seconds=1200)\r
81                     ettime=ettt.strftime("%Y-%m-%d %H:%M:%S")\r
82                     chs=rec10d.rec10db.select_bytime_bychtxt_all_timeline(bttime,ettime,tch['chtxt'])\r
83                     p2=0\r
84                     if len(chs)>0:\r
85                         for ch in chs:\r
86                             p2t=n_gram.trigram(ch['title'],title)\r
87                             if p2t>p2:\r
88                                 p2=p2t\r
89                     if p2<350:\r
90                         if status.getSettings_auto_bayes()==1:\r
91                             if p2<200:\r
92                                 maxnum=0\r
93                                 if len(ch['ch'])>2:\r
94                                     maxnum=epgdb.count_schedule_timeline(bttime, ettime)[1]\r
95                                     maxnum=int(configreader.getConfEnv("bscs_max"))-maxnum\r
96                                 else:\r
97                                     maxnum=epgdb.count_schedule_timeline(bttime, ettime)[0]\r
98                                     maxnum=int(configreader.getConfEnv("te_max"))-maxnum\r
99                                 if maxnum>0:\r
100                                     topt=status.getSettings_auto_opt()\r
101                                     recdb.reserveReckey(recdblist.REC_RESERVE,title,tch['chtxt'], bttime, ettime,topt)\r
102                         else:\r
103                             recdb.addAutoBayesKeyword(tch['chtxt'],title,start,stop,p)\r
104     dom.unlink()\r
105     if len(chlist) > 0:\r
106         writeMultiCHDB(chlist)\r
107         chlist = []\r
108         if len(tvlist) > 0:\r
109             writeMultiTVDB(bctype,tvlist)\r
110         tvlist = []\r
111         for ct,list in bayes.items():\r
112             auto_rec.updateRecall(ct,list[0],list[1],list[2])\r
113             auto_rec.updateRecall("ALL",list[0],list[1],list[2])\r
114     dtb=datetime.datetime.now()-dtb\r
115     recdblist.printutf8(bctype + u" epg取り出し終了")\r
116     recdblist.printutf8(str(dtb.days * 24 * 60 * 60 + dtb.seconds)+u"seconds taken for updating bayes-auto DB .")\r
117 def xml2db_dom_logo(xmlpath, bctype):#bctypeは放送種別で'TE'(地デジ)BS,CSがある。地デジの場合は te数字 が入る\r
118     dtb=datetime.datetime.now()\r
119     dom=xml.dom.minidom.parse(file(xmlpath))\r
120     logolist=[]\r
121     for logo in dom.getElementsByTagName('logo'):\r
122         type=int(logo.getAttribute("type"))\r
123         sv=logo.getAttribute("sv")\r
124         pngdata=logo.childNodes[0].data\r
125         logolist.append([type,sv,pngdata])\r
126     dom.unlink()\r
127     if len(logolist) > 0:\r
128         writeMultiLogoDB(logolist)\r
129     dtb=datetime.datetime.now()-dtb\r
130     recdblist.printutf8(bctype + u" epg取り出し終了")\r
131     recdblist.printutf8(str(dtb.days * 24 * 60 * 60 + dtb.seconds)+u"seconds taken for updating logo DB .")\r
132 def addTitle_Subtitle(recompiled,num,title,exp):\r
133     try:\r
134         tST=recompiled.match(exp)\r
135         newtitle=u""\r
136         if tST:\r
137             newtitle=title+u"『"+tST.group(num)+u"』"\r
138         else:\r
139             newtitle=title\r
140     except Exception, inst:\r
141         recdblist.addCommonlogEX("Error", "addTitle_Subtitle(xml2db_dob.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)\r
142         newtitle=title\r
143     return newtitle\r