| author | ymh <ymh.work@gmail.com> |
| Thu, 14 Oct 2010 11:36:12 +0200 | |
| changeset 88 | 7f2c2d9adf58 |
| parent 62 | 39b2dab4f939 |
| child 91 | 9c83809fda01 |
| permissions | -rw-r--r-- |
| 0 | 1 |
import tempfile |
2 |
import os |
|
3 |
import os.path |
|
4 |
import shutil |
|
5 |
from ldt.utils import zipfileext |
|
6 |
import urllib |
|
7 |
import ldt.utils.xml |
|
8 |
from django.conf import settings |
|
9 |
from models import Content |
|
10 |
import xml |
|
11 |
import xml.dom |
|
12 |
import xml.dom.minidom |
|
13 |
import xml.dom.ext |
|
14 |
import xml.xpath |
|
15 |
import fnmatch |
|
16 |
import Ft |
|
17 |
import uuid |
|
18 |
import shutil |
|
19 |
import lucene |
|
20 |
from ldt.ldt_utils import STORE |
|
21 |
from ldt.ldt_utils import ANALYZER |
|
22 |
||
23 |
def Property(func): |
|
24 |
return property(**func()) |
|
25 |
||
26 |
||
27 |
class ContentIndexer(object): |
|
28 |
||
29 |
def __init__(self, contentList, writer, decoupage_blackList = settings.DECOUPAGE_BLACKLIST): |
|
30 |
self.__contentList = contentList |
|
31 |
self.__decoupage_blacklist = decoupage_blackList |
|
32 |
self.__writer = writer |
|
33 |
||
34 |
@Property |
|
35 |
def decoupage_blacklist(): #@NoSelf |
|
36 |
doc = """get blacklist""" #@UnusedVariable |
|
37 |
||
38 |
def fget(self): |
|
39 |
if self.__decoupage_blacklist is None: |
|
40 |
self.__decoupage_blacklist = () |
|
41 |
return self.__decoupage_blacklist |
|
42 |
||
43 |
def fset(self, value): |
|
44 |
self.__decoupage_blacklist = value |
|
45 |
||
46 |
def fdel(self): |
|
47 |
del self.__decoupage_blacklist |
|
48 |
||
49 |
return locals() |
|
50 |
||
51 |
def index_all(self): |
|
52 |
for content in self.__contentList: |
|
53 |
self.index_content(content) |
|
54 |
||
55 |
def index_content(self, content): |
|
56 |
url =content.iri_url() |
|
57 |
filepath = urllib.urlopen(url) |
|
58 |
doc = xml.dom.minidom.parse(filepath) |
|
59 |
doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
60 |
||
61 |
self.__writer.deleteDocuments(lucene.Term("iri_id", content.iri_id)) |
|
62 |
||
63 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
64 |
res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble", context=con) |
|
65 |
||
66 |
for ensemble in res: |
|
67 |
ensembleId = ensemble.getAttributeNS(None,u"id") |
|
68 |
||
69 |
for decoupageNode in ensemble.childNodes: |
|
70 |
if decoupageNode.nodeType != xml.dom.Node.ELEMENT_NODE or decoupageNode.tagName != "decoupage" or decoupageNode.getAttributeNS(None,u"id") in self.decoupage_blacklist: |
|
71 |
continue |
|
72 |
||
73 |
decoupId = decoupageNode.getAttributeNS(None,u"id") |
|
74 |
res = xml.xpath.Evaluate("elements/element", decoupageNode) |
|
75 |
for elementNode in res: |
|
76 |
doc = lucene.Document() |
|
77 |
elementId = elementNode.getAttributeNS(None,u"id") |
|
78 |
tags = elementNode.getAttributeNS(None,u"tags") |
|
79 |
||
80 |
if tags is not None: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
81 |
tags.replace(";", ",") |
| 0 | 82 |
|
83 |
if tags is None or len(tags) == 0: |
|
84 |
tags = "" |
|
85 |
restagnode = xml.xpath.Evaluate("tag/text()", elementNode) |
|
86 |
for tagnode in restagnode: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
87 |
tags = tags + " , " + tagnode.data |
| 0 | 88 |
|
89 |
if tags is None or len(tags) == 0: |
|
90 |
tags = "" |
|
91 |
restagnode = xml.xpath.Evaluate("tags/tag/text()", elementNode) |
|
92 |
for tagnode in restagnode: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
93 |
tags = tags + " , " + tagnode.data |
| 0 | 94 |
|
95 |
title = "" |
|
96 |
for txtRes in xml.xpath.Evaluate("title/text()", elementNode): |
|
97 |
title = title + txtRes.data |
|
98 |
||
99 |
abstract = "" |
|
100 |
for txtRes in xml.xpath.Evaluate("abstract/text()", elementNode): |
|
101 |
abstract = abstract + txtRes.data |
|
102 |
||
103 |
doc.add(lucene.Field("iri_id", content.iri_id, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED)) |
|
104 |
doc.add(lucene.Field("ensemble_id", ensembleId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
105 |
doc.add(lucene.Field("decoupage_id", decoupId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
106 |
doc.add(lucene.Field("element_id", elementId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
107 |
doc.add(lucene.Field("tags", tags, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
108 |
doc.add(lucene.Field("title", title, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
109 |
doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
110 |
doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
111 |
||
112 |
seg = Segment(content=content, |
|
113 |
iri_id=content.iri_id, |
|
114 |
ensemble_id=ensembleId, |
|
115 |
cutting_id=decoupId, |
|
116 |
element_id=elementId, |
|
117 |
tags=tags, |
|
118 |
title=title, |
|
119 |
abstract=abstract, |
|
120 |
duration=duration, |
|
121 |
author=author, |
|
122 |
start_ts=start_ts, |
|
123 |
date=date_str) |
|
124 |
seg.save() |
|
125 |
||
126 |
||
127 |
self.__writer.addDocument(doc) |
|
128 |
||
129 |
self.__writer.commit() |
|
130 |
||
131 |
||
132 |
class ProjectIndexer(object): |
|
133 |
||
134 |
def __init__(self, projectList, writer, decoupage_blackList = settings.DECOUPAGE_BLACKLIST): |
|
135 |
self.__projectList = projectList |
|
136 |
self.__decoupage_blacklist = decoupage_blackList |
|
137 |
self.__writer = writer |
|
138 |
||
139 |
@Property |
|
140 |
def decoupage_blacklist(): #@NoSelf |
|
141 |
doc = """get blacklist""" #@UnusedVariable |
|
142 |
||
143 |
def fget(self): |
|
144 |
if self.__decoupage_blacklist is None: |
|
145 |
self.__decoupage_blacklist = () |
|
146 |
return self.__decoupage_blacklist |
|
147 |
||
148 |
def fset(self, value): |
|
149 |
self.__decoupage_blacklist = value |
|
150 |
||
151 |
def fdel(self): |
|
152 |
del self.__decoupage_blacklist |
|
153 |
||
154 |
return locals() |
|
155 |
||
156 |
def index_all(self): |
|
157 |
for project in self.__projectList: |
|
158 |
self.index_project(project) |
|
159 |
||
160 |
def index_project(self, project): |
|
161 |
||
162 |
doc = xml.dom.minidom.parseString(project.ldt) |
|
163 |
doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
164 |
||
165 |
self.__writer.deleteDocuments(lucene.Term("iri_id", project.iri_id)) |
|
166 |
||
167 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
168 |
res = xml.xpath.Evaluate("/iri/annotations/content", context=con) |
|
169 |
||
170 |
for content in res: |
|
171 |
contentId = content.getAttributeNS(None,u"id") |
|
172 |
||
173 |
ensembleId = "ens_perso" |
|
174 |
||
175 |
for decoupageNode in content.childNodes: |
|
176 |
if decoupageNode.nodeType != xml.dom.Node.ELEMENT_NODE or decoupageNode.tagName != "decoupage" or decoupageNode.getAttributeNS(None,"id") in self.decoupage_blacklist: |
|
177 |
continue |
|
178 |
||
179 |
decoupId = decoupageNode.getAttributeNS(None,u"id") |
|
180 |
res = xml.xpath.Evaluate("elements/element", decoupageNode) |
|
181 |
for elementNode in res: |
|
182 |
doc = lucene.Document() |
|
183 |
elementId = elementNode.getAttributeNS(None,u"id") |
|
184 |
tags = elementNode.getAttributeNS(None,u"tags") |
|
185 |
||
186 |
if tags is not None: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
187 |
tags.replace(";", ",") |
| 0 | 188 |
|
189 |
if tags is None or len(tags) == 0: |
|
190 |
tags = "" |
|
191 |
restagnode = xml.xpath.Evaluate("tag/text()", elementNode) |
|
192 |
for tagnode in restagnode: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
193 |
tags = tags + " , " + tagnode.data |
| 0 | 194 |
|
195 |
if tags is None or len(tags) == 0: |
|
196 |
tags = "" |
|
197 |
restagnode = xml.xpath.Evaluate("tags/tag/text()", elementNode) |
|
198 |
for tagnode in restagnode: |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
199 |
tags = tags + " , " + tagnode.data |
| 0 | 200 |
|
201 |
title = "" |
|
202 |
for txtRes in xml.xpath.Evaluate("title/text()", elementNode): |
|
203 |
title = title + txtRes.data |
|
204 |
||
205 |
abstract = "" |
|
206 |
for txtRes in xml.xpath.Evaluate("abstract/text()", elementNode): |
|
207 |
abstract = abstract + txtRes.data |
|
208 |
||
209 |
doc.add(lucene.Field("project_id", project.iri_id, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED)) |
|
210 |
doc.add(lucene.Field("iri_id", contentId, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED)) |
|
211 |
doc.add(lucene.Field("ensemble_id", ensembleId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
212 |
doc.add(lucene.Field("decoupage_id", decoupId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
213 |
doc.add(lucene.Field("element_id", elementId, lucene.Field.Store.YES, lucene.Field.Index.NO)) |
|
214 |
doc.add(lucene.Field("tags", tags, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
215 |
doc.add(lucene.Field("title", title, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
216 |
doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
217 |
doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.NO, lucene.Field.Index.ANALYZED)) |
|
218 |
||
219 |
seg = Segment(content=content, |
|
220 |
iri_id=content.iri_id, |
|
221 |
ensemble_id=ensembleId, |
|
222 |
cutting_id=decoupId, |
|
223 |
element_id=elementId, |
|
224 |
tags=tags, |
|
225 |
title=title, |
|
226 |
abstract=abstract, |
|
227 |
duration=duration, |
|
228 |
author=author, |
|
229 |
start_ts=start_ts, |
|
230 |
date=date_str) |
|
231 |
seg.save() |
|
232 |
||
233 |
self.__writer.addDocument(doc) |
|
234 |
||
235 |
self.__writer.commit() |