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