|
7
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
''' |
|
|
3 |
Created on Mar 22, 2013 |
|
|
4 |
|
|
|
5 |
@author: tc |
|
|
6 |
''' |
|
|
7 |
|
|
8
|
8 |
from datetime import datetime |
|
7
|
9 |
from dircache import listdir |
|
|
10 |
from django.core.management.base import BaseCommand, CommandError |
|
|
11 |
from genericpath import isdir, isfile |
|
8
|
12 |
from ldt.api.ldt.serializers.cinelabserializer import CinelabSerializer |
|
|
13 |
from ldt.ldt_utils.models import Media, Content, Project |
|
|
14 |
from ldt.ldt_utils.stat import update_stat_content |
|
|
15 |
from ldt.ldt_utils.utils import generate_uuid |
|
|
16 |
from ldt.security import set_current_user, get_current_user_or_admin |
|
7
|
17 |
from ldt.security.cache import cached_assign |
|
|
18 |
from optparse import make_option |
|
|
19 |
from os.path import join |
|
|
20 |
import json |
|
8
|
21 |
import lxml.etree |
|
7
|
22 |
import os.path |
|
8
|
23 |
import time |
|
|
24 |
|
|
|
25 |
import logging |
|
|
26 |
logger = logging.getLogger(__name__) |
|
7
|
27 |
|
|
|
28 |
|
|
|
29 |
class Command(BaseCommand): |
|
|
30 |
''' |
|
|
31 |
Load medias, contents, project from json generated by ubicast |
|
|
32 |
''' |
|
|
33 |
|
|
|
34 |
args = 'folder containing folders containing json files' |
|
84
|
35 |
help = 'Load medias, contents and project from json files generated by ubicast for THEATRE content' |
|
7
|
36 |
|
|
|
37 |
|
|
|
38 |
def __safe_get(self, dict_arg, key, conv = lambda x: x, default= None): |
|
|
39 |
val = dict_arg.get(key, default) |
|
|
40 |
return conv(val) if val else default |
|
|
41 |
|
|
|
42 |
def __safe_decode(self, s): |
|
|
43 |
if not isinstance(s, basestring): |
|
|
44 |
return s |
|
|
45 |
try: |
|
|
46 |
return s.decode('utf8') |
|
|
47 |
except: |
|
|
48 |
try: |
|
|
49 |
return s.decode('latin1') |
|
|
50 |
except: |
|
|
51 |
return s.decode('utf8','replace') |
|
|
52 |
|
|
|
53 |
def handle(self, *args, **options): |
|
|
54 |
|
|
|
55 |
# Test path |
|
|
56 |
if len(args) != 1: |
|
|
57 |
raise CommandError("The command has no argument or too much arguments. Only one is needed : the folder file path.") |
|
|
58 |
|
|
|
59 |
# Check if temporary files already exist |
|
|
60 |
path = os.path.abspath(args[0]) |
|
|
61 |
if not os.path.exists(path): |
|
|
62 |
raise CommandError("The folder does not exist.") |
|
|
63 |
|
|
|
64 |
do_import = False |
|
|
65 |
confirm = raw_input(""" |
|
|
66 |
If the database already contains datas, they will be overriden |
|
|
67 |
|
|
|
68 |
Do you want to continue ? |
|
|
69 |
|
|
|
70 |
Type 'y' to continue, or 'n' to quit: """) |
|
|
71 |
do_import = (confirm == "y") |
|
|
72 |
|
|
|
73 |
# Continue |
|
|
74 |
if do_import: |
|
|
75 |
print("Parsing folder %s ..." % path) |
|
59
|
76 |
for dirpath, dirnames, filenames in os.walk(path): |
|
|
77 |
#print("Parsing walk %s, %s, %s" % (dirpath, dirnames, filenames)) |
|
|
78 |
for filename in filenames: |
|
|
79 |
if filename == "cinelab_iri.json": |
|
|
80 |
json_path = os.path.join(dirpath, filename) |
|
|
81 |
print("Parsing json file %s ..." % json_path) |
|
|
82 |
json_data = False |
|
|
83 |
try: |
|
|
84 |
file_data = open(json_path) |
|
|
85 |
json_data = json.load(file_data) |
|
|
86 |
file_data.close() |
|
|
87 |
except: |
|
|
88 |
print("Error while parsing json file.") |
|
|
89 |
if json_data: |
|
|
90 |
ctt_id = os.path.basename(dirpath) |
|
|
91 |
# Save media and content |
|
84
|
92 |
dur = int(json_data["medias"][0]["meta"]["duration"]) |
|
115
|
93 |
media, _ = Media.objects.get_or_create(src="/data/theatre/"+ctt_id+"/720p.mp4", duration=dur) |
|
59
|
94 |
media.is_public = True |
|
|
95 |
content = Content.objects.create(iri_id=ctt_id, |
|
|
96 |
iriurl=ctt_id+u"/"+ctt_id+u".iri", |
|
|
97 |
media_obj=media, |
|
|
98 |
title=json_data["meta"]["dc:title"], |
|
84
|
99 |
duration=dur, |
|
59
|
100 |
content_creation_date = json_data["meta"]["dc:created"]) |
|
|
101 |
content.is_public = True |
|
|
102 |
# Get content front projet |
|
|
103 |
proj = content.front_project |
|
|
104 |
username = proj.owner.username |
|
|
105 |
now = datetime.utcnow().isoformat() |
|
|
106 |
# Start data to send to api |
|
|
107 |
proj_data = {} |
|
|
108 |
proj_data["meta"] = {} |
|
|
109 |
proj_data["meta"]["id"] = proj.ldt_id |
|
|
110 |
proj_data["meta"]["dc:title"] = proj.title |
|
|
111 |
proj_data["meta"]["dc:creator"] = username |
|
|
112 |
proj_data["meta"]["dc:description"] = "description added" |
|
|
113 |
proj_data["meta"]["dc:created"] = json_data["meta"]["dc:created"] |
|
|
114 |
proj_data["meta"]["dc:modified"] = json_data["meta"]["dc:modified"] |
|
|
115 |
proj_data["meta"]["dc:contributor"] = username |
|
|
116 |
proj_data["medias"] = [] |
|
|
117 |
proj_data["medias"].append({"id": content.iri_id}) |
|
|
118 |
# The tags and annotations (main part) |
|
|
119 |
proj_data["tags"] = [] |
|
|
120 |
proj_data["annotations"] = [] |
|
|
121 |
tags_id_label = {} |
|
|
122 |
tags_label_id = {} |
|
|
123 |
for a in json_data["annotations"]: |
|
|
124 |
# "content": { "data": { "modalites_sceniques": "costumes,décors",... } } |
|
|
125 |
if "content" in a and "data" in a["content"] and type(a["content"]["data"]) == type(dict()): |
|
|
126 |
annot_tags = [] |
|
|
127 |
desc = "" |
|
|
128 |
title = "" |
|
|
129 |
# Build tags |
|
|
130 |
for k,v in a["content"]["data"].iteritems(): |
|
|
131 |
if k!="commentaire" and k!="description" and k!="titre": |
|
|
132 |
v = v.split(",") |
|
|
133 |
for val in v: |
|
|
134 |
val = val.strip() |
|
|
135 |
tag_label = k + u": " + val |
|
|
136 |
if val!="": |
|
|
137 |
if not tag_label in tags_label_id: |
|
|
138 |
tags_label_id[tag_label] = generate_uuid() |
|
|
139 |
tags_id_label[tags_label_id[tag_label]] = tag_label |
|
|
140 |
#logger.debug("CREATED") |
|
|
141 |
#logger.debug(tags_label_id[tag_label] + " = " + tags_id_label[tags_label_id[tag_label]]) |
|
|
142 |
proj_data["tags"].append({"meta": { "dc:title": tag_label }, "id": tags_label_id[tag_label] }) |
|
|
143 |
annot_tags.append({"id-ref": tags_label_id[tag_label] }) |
|
|
144 |
elif k=="commentaire" or k=="description": |
|
|
145 |
desc = v |
|
|
146 |
elif k=="titre": |
|
|
147 |
title = v |
|
|
148 |
# Build annotation with needed fields |
|
|
149 |
proj_data["annotations"].append({ |
|
|
150 |
"content": { |
|
|
151 |
"mimetype": "application/x-ldt-structured", |
|
|
152 |
"description": desc, |
|
|
153 |
# "img": { |
|
|
154 |
# "src": "" |
|
|
155 |
# }, |
|
|
156 |
"title": title, |
|
|
157 |
# "polemics": [ ], |
|
|
158 |
}, |
|
|
159 |
"begin": a["begin"], |
|
|
160 |
"meta": { |
|
|
161 |
# "dc:contributor": "admin", |
|
|
162 |
"id-ref": a["type"], |
|
|
163 |
"dc:created": now, |
|
|
164 |
# "dc:modified": "2014-03-04T16:40:23.609971", |
|
|
165 |
"dc:creator": username |
|
|
166 |
}, |
|
|
167 |
"end": a["end"], |
|
|
168 |
"tags": annot_tags, |
|
|
169 |
"color": "16763904", |
|
|
170 |
"media": ctt_id, |
|
|
171 |
"id": a["id"] |
|
|
172 |
}) |
|
|
173 |
|
|
|
174 |
# The annotation-types |
|
|
175 |
proj_data["annotation-types"] = [] |
|
|
176 |
at_ids = [] |
|
|
177 |
for at in json_data["annotation_types"]: |
|
|
178 |
proj_data["annotation-types"].append({ |
|
|
179 |
# dc:contributor: "admin", |
|
|
180 |
"dc:creator": username, |
|
|
181 |
"dc:title": at["id"], |
|
|
182 |
"id": at["id"], |
|
|
183 |
# dc:created: "2014-03-04T14:51:13.907674", |
|
|
184 |
"dc:description": "" |
|
|
185 |
# dc:modified: "2014-03-04T14:51:13.907674" |
|
8
|
186 |
}) |
|
59
|
187 |
at_ids.append({ "id-ref": at["id"] }) |
|
|
188 |
# The list of annotation-types |
|
|
189 |
list_id = generate_uuid() |
|
|
190 |
proj_data["lists"] = [{ |
|
|
191 |
"items": at_ids, |
|
|
192 |
"meta": { |
|
|
193 |
"dc:creator": username, |
|
|
194 |
"id-ref": ctt_id, |
|
|
195 |
"dc:title": "SPEL", |
|
|
196 |
"dc:description": "" |
|
|
197 |
}, |
|
|
198 |
"id": list_id |
|
|
199 |
}] |
|
|
200 |
# The views for default display |
|
|
201 |
proj_data["views"] = [{ |
|
|
202 |
"id": generate_uuid(), |
|
|
203 |
"contents": [ ctt_id ], |
|
|
204 |
"annotation_types": [atid["id-ref"] for atid in at_ids] |
|
|
205 |
}] |
|
|
206 |
|
|
|
207 |
serializr = CinelabSerializer() |
|
|
208 |
serializr.validate_cinelab_json(proj_data) |
|
|
209 |
ldt_xml = serializr.cinelab_to_ldt(proj_data) |
|
|
210 |
proj.ldt = lxml.etree.tostring(ldt_xml, pretty_print=True) |
|
|
211 |
#logger.debug(proj.ldt) |
|
|
212 |
proj.save() |
|
|
213 |
update_stat_content(content) |
|
|
214 |
else: |
|
|
215 |
print("Ignoring or not exist.") |
|
7
|
216 |
|
|
|
217 |
# This is the end |
|
9
|
218 |
print("This is the end. DO NOT FORGET TO RUN THE COMMAND 'REINDEX -P' TO GENERATE SEGMENTS AND TAGS.") |
|
7
|
219 |
|