335
|
1 |
''' |
|
2 |
Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri |
|
3 |
''' |
|
4 |
import csv |
|
5 |
import sys |
|
6 |
|
|
7 |
import requests |
|
8 |
from django.core.management.base import BaseCommand |
|
9 |
from django.core import management |
|
10 |
from lxml import etree |
|
11 |
from django.db import connections |
|
12 |
from ldt.ldt_utils import models |
|
13 |
|
|
14 |
|
|
15 |
def create_source(iri_id): |
|
16 |
''' |
|
17 |
create .iri source |
|
18 |
''' |
|
19 |
return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4" |
|
20 |
def create_iri_url(iri_url): |
|
21 |
''' |
|
22 |
create iri url |
|
23 |
''' |
|
24 |
return "http://amateur.iri.centrepompidou.fr" + iri_url |
|
25 |
|
|
26 |
|
|
27 |
class Command(BaseCommand): |
|
28 |
''' |
|
29 |
command |
|
30 |
''' |
|
31 |
def add_arguments(self, parser): |
|
32 |
''' |
|
33 |
add arguments |
|
34 |
''' |
|
35 |
|
|
36 |
parser.add_argument('-i', |
|
37 |
'--pathin', |
|
38 |
dest='pathin', |
|
39 |
default=None |
|
40 |
) |
|
41 |
parser.add_argument('-o', |
|
42 |
'--pathout', |
|
43 |
dest='pathout', |
|
44 |
default=None |
|
45 |
) |
|
46 |
|
|
47 |
def get_duration(self, elem): |
|
48 |
''' |
|
49 |
get duration |
|
50 |
''' |
|
51 |
for element in self.mylist: |
|
52 |
element[0] = element[0][:len(element[0])-1] |
|
53 |
if elem == element[0]: |
|
54 |
element[1] = element[1][1:] |
|
55 |
root = etree.XML(element[1].encode('utf-8'), self.parser) |
|
56 |
duration = root.xpath('format')[0].get('duration') |
|
57 |
duration = duration[:7] |
|
58 |
return duration |
|
59 |
|
|
60 |
def change_annotations(self, iriin, ldtout): |
|
61 |
''' |
|
62 |
change annotations |
|
63 |
''' |
|
64 |
rootout = etree.XML(ldtout.encode('utf-8'), self.parser) |
|
65 |
rootin = etree.XML(iriin.encode('utf-8'), self.parser) |
|
66 |
ensembles = rootin.xpath('body/ensembles/ensemble') |
|
67 |
for ensemble in ensembles: |
|
68 |
idens = ensemble.get("id") |
|
69 |
decoups = ensemble.xpath('decoupage') |
|
70 |
rootout.xpath('annotations/content')[0].append(ensemble) |
|
71 |
for decoup in decoups: |
|
72 |
iddec = decoup.get("id") |
|
73 |
nouveaudecoup = '<decoupage idens=\"' + idens +'\" id=\"' + iddec +'\" tagsSelect=\"\"/>' |
|
74 |
nouveaudecxml = etree.fromstring(nouveaudecoup) |
|
75 |
rootout.xpath('displays/display/content')[0].append(nouveaudecxml) |
|
76 |
return etree.tostring(rootout) |
|
77 |
def handle(self, *args, **options): |
|
78 |
''' |
|
79 |
handle command |
|
80 |
''' |
|
81 |
pathin = options['pathin'] |
|
82 |
pathout = options['pathout'] |
|
83 |
if not pathin or not pathout: |
|
84 |
return "Error : specify path in and path out" |
|
85 |
try: |
|
86 |
csvfile = open(pathin, 'r') |
|
87 |
csvfile2 = open(pathout, 'wb') |
|
88 |
except IOError: |
|
89 |
self.stdout.write('file can\'t be opened') |
|
90 |
return |
|
91 |
|
|
92 |
cursor = connections['default2'].cursor() |
|
93 |
cursor.execute('SELECT ct.iri_id, ct.iriurl, ct.creation_date, ct.title, ct.description,\ |
|
94 |
pj.ldt_id, pj.ldt\ |
|
95 |
FROM ldt_content AS ct INNER JOIN ldt_ldtproject_contents\ |
|
96 |
AS ctpj ON ct.id = ctpj.content_id\ |
|
97 |
INNER JOIN ldt_ldtproject AS pj ON ctpj.ldtproject_id = pj.id;' |
|
98 |
) |
|
99 |
amateurdata = cursor.fetchall() |
|
100 |
reload(sys) |
|
101 |
sys.setdefaultencoding('utf8') |
|
102 |
self.parser = etree.XMLParser(encoding='utf-8') |
|
103 |
self.myfile = csv.reader(csvfile) |
|
104 |
self.writefile = csv.writer(csvfile2) |
|
105 |
self.mylist = list(self.myfile) |
|
106 |
for mediaproj in amateurdata: |
|
107 |
iri_id = mediaproj[0] |
|
108 |
try: |
|
109 |
models.Project.objects.get(title='front project : %s' % iri_id) |
|
110 |
continue |
|
111 |
except models.Project.MultipleObjectsReturned: |
|
112 |
continue |
|
113 |
except models.Project.DoesNotExist: |
|
114 |
self.stdout.write('Media %s will be created'%iri_id) |
|
115 |
iriurl = mediaproj[1] |
|
116 |
#TODO set creationdate, title... |
|
117 |
# creationdate = mediaproj[2] |
|
118 |
# title = mediaproj[3] |
|
119 |
description = mediaproj[4] |
|
120 |
# ldt_id = mediaproj[5] |
|
121 |
# ldt = mediaproj[6] |
|
122 |
mysource = create_source(iri_id) |
|
123 |
myiriurl = create_iri_url(iriurl) |
|
124 |
myiri = requests.get(myiriurl)._content |
|
125 |
duration = self.get_duration(mysource) |
|
126 |
if requests.head(mysource).status_code == 200: |
|
127 |
management.call_command( |
|
128 |
'createmediacontent', |
|
129 |
source=mysource, |
|
130 |
title=iri_id, |
|
131 |
videopath='', |
|
132 |
description=description, |
|
133 |
duration=duration, |
|
134 |
public=True, |
|
135 |
creator='admin', |
|
136 |
update=True |
|
137 |
) |
|
138 |
myfrontproj = models.Project.objects.get(title='front project : %s' % iri_id) |
|
139 |
myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt) |
|
140 |
self.writefile.writerow([mysource, |
|
141 |
iri_id, |
|
142 |
models.Content.objects.get(title=iri_id).iri_id, |
|
143 |
myfrontproj.ldt_id |
|
144 |
]) |
|
145 |
self.stdout.write("Project changed") |
|
146 |
myfrontproj.save() |
|
147 |
csvfile.close() |
|
148 |
csvfile2.close() |