335
|
1 |
''' |
|
2 |
Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri |
|
3 |
''' |
|
4 |
import csv |
336
|
5 |
import re |
335
|
6 |
import sys |
336
|
7 |
import requests |
335
|
8 |
|
|
9 |
from django.core.management.base import BaseCommand |
|
10 |
from django.core import management |
|
11 |
from lxml import etree |
|
12 |
from django.db import connections |
|
13 |
from ldt.ldt_utils import models |
|
14 |
|
|
15 |
|
|
16 |
def create_source(iri_id): |
|
17 |
''' |
|
18 |
create .iri source |
|
19 |
''' |
|
20 |
return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4" |
|
21 |
def create_iri_url(iri_url): |
|
22 |
''' |
|
23 |
create iri url |
|
24 |
''' |
|
25 |
return "http://amateur.iri.centrepompidou.fr" + iri_url |
|
26 |
|
336
|
27 |
def create_title(edition, day, name, order, session): |
|
28 |
''' |
|
29 |
create title |
|
30 |
''' |
|
31 |
if re.match(r'....\_1', edition) or edition == '2009': |
|
32 |
event = "Prepa ENMI " |
|
33 |
else: |
|
34 |
event = "ENMI " |
|
35 |
session += 1 |
|
36 |
return event + edition[0:4] + " - " + str(day) + "." + str(session) + "." + str(order) + " - " + name |
|
37 |
def create_name(fname, lname): |
|
38 |
''' |
|
39 |
create name |
|
40 |
''' |
|
41 |
if fname is None: |
|
42 |
if lname is None: |
|
43 |
return "" |
|
44 |
return lname |
|
45 |
if lname is None: |
|
46 |
return fname |
|
47 |
return fname + " " + lname |
335
|
48 |
|
336
|
49 |
def create_tmp(edition, title): |
|
50 |
''' |
|
51 |
create temp to check medias with two authors |
|
52 |
''' |
|
53 |
if re.match(r'....\_1', edition) or edition == '2009': |
|
54 |
length = 24 |
|
55 |
else: |
|
56 |
length = 18 |
|
57 |
return title[0:length] |
|
58 |
|
|
59 |
def create_tag(edition): |
|
60 |
''' |
|
61 |
create name |
|
62 |
''' |
|
63 |
if re.match(r'....\_1', edition) or edition == '2009': |
|
64 |
event = "prepaenmi" |
|
65 |
else: |
|
66 |
event = "enmi" |
|
67 |
return event + edition[0:4] |
335
|
68 |
class Command(BaseCommand): |
|
69 |
''' |
|
70 |
command |
|
71 |
''' |
|
72 |
def add_arguments(self, parser): |
|
73 |
''' |
|
74 |
add arguments |
|
75 |
''' |
|
76 |
|
|
77 |
parser.add_argument('-i', |
|
78 |
'--pathin', |
|
79 |
dest='pathin', |
|
80 |
default=None |
|
81 |
) |
|
82 |
parser.add_argument('-o', |
|
83 |
'--pathout', |
|
84 |
dest='pathout', |
|
85 |
default=None |
|
86 |
) |
|
87 |
|
|
88 |
def get_duration(self, elem): |
|
89 |
''' |
|
90 |
get duration |
|
91 |
''' |
|
92 |
for element in self.mylist: |
|
93 |
element[0] = element[0][:len(element[0])-1] |
|
94 |
if elem == element[0]: |
|
95 |
element[1] = element[1][1:] |
|
96 |
root = etree.XML(element[1].encode('utf-8'), self.parser) |
|
97 |
duration = root.xpath('format')[0].get('duration') |
|
98 |
duration = duration[:7] |
|
99 |
return duration |
|
100 |
|
|
101 |
def change_annotations(self, iriin, ldtout): |
|
102 |
''' |
|
103 |
change annotations |
|
104 |
''' |
|
105 |
rootout = etree.XML(ldtout.encode('utf-8'), self.parser) |
|
106 |
rootin = etree.XML(iriin.encode('utf-8'), self.parser) |
|
107 |
ensembles = rootin.xpath('body/ensembles/ensemble') |
|
108 |
for ensemble in ensembles: |
|
109 |
idens = ensemble.get("id") |
|
110 |
decoups = ensemble.xpath('decoupage') |
|
111 |
rootout.xpath('annotations/content')[0].append(ensemble) |
|
112 |
for decoup in decoups: |
|
113 |
iddec = decoup.get("id") |
|
114 |
nouveaudecoup = '<decoupage idens=\"' + idens +'\" id=\"' + iddec +'\" tagsSelect=\"\"/>' |
|
115 |
nouveaudecxml = etree.fromstring(nouveaudecoup) |
|
116 |
rootout.xpath('displays/display/content')[0].append(nouveaudecxml) |
|
117 |
return etree.tostring(rootout) |
|
118 |
def handle(self, *args, **options): |
|
119 |
''' |
|
120 |
handle command |
|
121 |
''' |
|
122 |
pathin = options['pathin'] |
|
123 |
pathout = options['pathout'] |
|
124 |
if not pathin or not pathout: |
|
125 |
return "Error : specify path in and path out" |
|
126 |
try: |
|
127 |
csvfile = open(pathin, 'r') |
|
128 |
csvfile2 = open(pathout, 'wb') |
|
129 |
except IOError: |
|
130 |
self.stdout.write('file can\'t be opened') |
|
131 |
return |
|
132 |
|
|
133 |
cursor = connections['default2'].cursor() |
336
|
134 |
cursor.execute('SELECT ct.iri_id, ct.iriurl, ct.creation_date, ct.description,\ |
|
135 |
pj.ldt,\ |
|
136 |
day._order,\ |
|
137 |
edit.code,\ |
|
138 |
pers.firstname, pers.lastname,\ |
|
139 |
speak.order,\ |
|
140 |
sess._order\ |
|
141 |
FROM ldt_content AS ct\ |
|
142 |
LEFT JOIN ldt_ldtproject_contents AS ctpj ON ct.id = ctpj.content_id\ |
|
143 |
LEFT JOIN ldt_ldtproject AS pj ON ctpj.ldtproject_id = pj.id\ |
|
144 |
LEFT JOIN conf_speak AS speak ON ct.id=speak.content_id\ |
|
145 |
LEFT JOIN conf_session AS sess ON sess.event_ptr_id=speak.session_id\ |
|
146 |
LEFT JOIN conf_day AS day ON sess.day_id=day.event_ptr_id\ |
|
147 |
LEFT JOIN conf_edition AS edit ON day.edition_id=edit.event_ptr_id\ |
|
148 |
LEFT JOIN conf_speak_speakers AS spkr ON speak.event_ptr_id=spkr.speak_id\ |
|
149 |
LEFT JOIN conf_person AS pers ON spkr.person_id=pers.id;' |
335
|
150 |
) |
|
151 |
amateurdata = cursor.fetchall() |
|
152 |
reload(sys) |
|
153 |
sys.setdefaultencoding('utf8') |
|
154 |
self.parser = etree.XMLParser(encoding='utf-8') |
|
155 |
self.myfile = csv.reader(csvfile) |
|
156 |
self.writefile = csv.writer(csvfile2) |
|
157 |
self.mylist = list(self.myfile) |
336
|
158 |
titletmp = '' |
335
|
159 |
for mediaproj in amateurdata: |
|
160 |
iri_id = mediaproj[0] |
336
|
161 |
mysource = create_source(iri_id) |
|
162 |
dayorder = mediaproj[5] |
|
163 |
edition = mediaproj[6] |
|
164 |
sessionorder = mediaproj[10] |
|
165 |
speakorder = mediaproj[9] |
|
166 |
firstname = mediaproj[7] |
|
167 |
lastname = mediaproj[8] |
|
168 |
if dayorder is None and edition is None: |
|
169 |
title = iri_id |
|
170 |
fullname = '' |
|
171 |
tag = iri_id |
|
172 |
else: |
|
173 |
tag = create_tag(edition) |
|
174 |
fullname = create_name(firstname, lastname) |
|
175 |
title = create_title(edition, dayorder + 1, fullname, speakorder, sessionorder) |
335
|
176 |
try: |
336
|
177 |
mymedia = models.Media.objects.get(src=mysource) |
|
178 |
mycontent = models.Content.objects.get(media_obj_id=mymedia.id) |
|
179 |
thisregex = r"front project : " + titletmp + r".*" |
|
180 |
myproject = models.Project.objects.get(title__iregex=thisregex) |
|
181 |
myregex = r".*" + re.escape(fullname) + r".*" |
|
182 |
if not re.match(myregex, mycontent.title): |
|
183 |
mycontent.title += " & " |
|
184 |
mycontent.title += fullname |
|
185 |
mycontent.save() |
|
186 |
myproject.title += " & " |
|
187 |
myproject.title += fullname |
|
188 |
myproject.save() |
335
|
189 |
continue |
336
|
190 |
except (models.Media.MultipleObjectsReturned, |
|
191 |
models.Content.MultipleObjectsReturned, |
|
192 |
models.Project.MultipleObjectsReturned |
|
193 |
): |
335
|
194 |
continue |
336
|
195 |
except (models.Media.DoesNotExist, |
|
196 |
models.Content.DoesNotExist, |
|
197 |
models.Project.DoesNotExist |
|
198 |
): |
|
199 |
self.stdout.write('Media %s and Content %s will be created'%(iri_id, title)) |
335
|
200 |
iriurl = mediaproj[1] |
336
|
201 |
if dayorder is None and edition is None: |
|
202 |
titletmp = '' |
|
203 |
else: |
|
204 |
titletmp = create_tmp(edition, title) |
|
205 |
#TODO set creationdate |
|
206 |
#creationdate = mediaproj[2] |
|
207 |
description = mediaproj[3] |
|
208 |
# ldt = mediaproj[4] |
335
|
209 |
myiriurl = create_iri_url(iriurl) |
|
210 |
myiri = requests.get(myiriurl)._content |
|
211 |
duration = self.get_duration(mysource) |
|
212 |
if requests.head(mysource).status_code == 200: |
|
213 |
management.call_command( |
|
214 |
'createmediacontent', |
|
215 |
source=mysource, |
336
|
216 |
title=title, |
335
|
217 |
videopath='', |
|
218 |
description=description, |
|
219 |
duration=duration, |
|
220 |
public=True, |
|
221 |
creator='admin', |
336
|
222 |
update=True, |
|
223 |
tags=tag |
335
|
224 |
) |
336
|
225 |
myfrontproj = models.Project.objects.get(title='front project : %s' % title) |
335
|
226 |
myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt) |
|
227 |
self.writefile.writerow([mysource, |
|
228 |
iri_id, |
336
|
229 |
models.Content.objects.get(title=title).iri_id, |
|
230 |
title, |
|
231 |
myfrontproj.ldt_id, |
335
|
232 |
]) |
|
233 |
self.stdout.write("Project changed") |
|
234 |
myfrontproj.save() |
|
235 |
csvfile.close() |
|
236 |
csvfile2.close() |