341
|
1 |
'''l |
|
2 |
Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri |
|
3 |
''' |
|
4 |
import csv |
|
5 |
import re |
|
6 |
import sys |
|
7 |
import requests |
|
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 |
|
|
17 |
|
|
18 |
def get_video(nbr): |
|
19 |
return "https://media.iri.centrepompidou.fr/video/seminaire_" + str(nbr) + ".mp4" |
|
20 |
|
|
21 |
def get_iri(nbr): |
|
22 |
return "http://web.iri.centrepompidou.fr/fonds/seminaires/seminaire/ldtSeance/" + str(nbr) |
|
23 |
|
|
24 |
def create_tag(id): |
|
25 |
tags = { |
|
26 |
1:'figamat', |
|
27 |
6:'museo2008', |
|
28 |
8:'museo2009', |
|
29 |
9:'poltechamat', |
|
30 |
10:'capdigital', |
|
31 |
11:'nanotech', |
|
32 |
12:'modernisation', |
|
33 |
13:'museo2010', |
|
34 |
14:'createrr', |
|
35 |
15:'geste', |
|
36 |
16:'universcience', |
|
37 |
17:'wikiradio', |
|
38 |
18:'hceac', |
|
39 |
19:'museo2011', |
|
40 |
20:'educpop', |
|
41 |
21:'fablab', |
|
42 |
22:'regardssignes' |
|
43 |
} |
|
44 |
if tags[id]: |
|
45 |
return tags[id] |
|
46 |
else: |
|
47 |
return '' |
|
48 |
|
|
49 |
class Command(BaseCommand): |
|
50 |
''' |
|
51 |
command |
|
52 |
''' |
342
|
53 |
def __init__(self, *args, **kwargs): |
|
54 |
super(Command, self).__init__(*args, **kwargs) |
|
55 |
self.parser = None |
|
56 |
self.myfile = None |
|
57 |
self.writefile = None |
|
58 |
self.durations = None |
|
59 |
|
341
|
60 |
def add_arguments(self, parser): |
|
61 |
''' |
|
62 |
add arguments |
|
63 |
''' |
|
64 |
|
|
65 |
parser.add_argument('-i', |
|
66 |
'--pathin', |
|
67 |
dest='pathin', |
|
68 |
default=None |
|
69 |
) |
|
70 |
parser.add_argument('-o', |
|
71 |
'--pathout', |
|
72 |
dest='pathout', |
|
73 |
default=None |
|
74 |
) |
|
75 |
|
|
76 |
def parse_duration(self, xmlstr): |
|
77 |
''' |
|
78 |
parse duration from xml def |
|
79 |
''' |
|
80 |
root = etree.XML(xmlstr.encode('utf-8'), self.parser) |
|
81 |
return root.xpath('format')[0].get('duration')[:7] |
|
82 |
|
|
83 |
def get_duration(self, elem): |
|
84 |
''' |
|
85 |
get duration |
|
86 |
''' |
|
87 |
return self.durations.get(elem) |
|
88 |
|
|
89 |
def change_annotations(self, iriin, ldtout): |
|
90 |
''' |
|
91 |
change annotations |
|
92 |
''' |
|
93 |
rootout = etree.XML(ldtout.encode('utf-8'), self.parser) |
|
94 |
rootin = etree.XML(iriin.encode('utf-8'), self.parser) |
|
95 |
if len(rootin.xpath('annotations/content')) > 0: |
|
96 |
decoups = rootin.xpath('annotations/content')[0] |
|
97 |
else: |
|
98 |
return etree.tostring(rootout) |
|
99 |
idens = rootout.xpath('annotations/content/ensemble')[0].get("id") |
|
100 |
for decoup in decoups: |
|
101 |
iddec = decoup.get("id") |
|
102 |
nouveaudecoup = '<decoupage idens=\"' \ |
|
103 |
+ idens +'\" id=\"' \ |
|
104 |
+ iddec +'\" tagsSelect=\"\"/>' |
|
105 |
nouveaudecxml = etree.fromstring(nouveaudecoup) |
|
106 |
rootout.xpath('annotations/content/ensemble')[0].append(decoup) |
|
107 |
rootout.xpath('displays/display/content')[0].append(nouveaudecxml) |
|
108 |
return etree.tostring(rootout) |
|
109 |
|
|
110 |
def handle(self, *args, **options): |
|
111 |
''' |
|
112 |
handle |
|
113 |
''' |
|
114 |
pathin = options['pathin'] |
|
115 |
pathout = options['pathout'] |
|
116 |
if not pathin or not pathout: |
|
117 |
return "Error : specify path in and path out" |
|
118 |
try: |
|
119 |
csvfile = open(pathin, 'r') |
|
120 |
csvfile2 = open(pathout, 'wb') |
|
121 |
except IOError: |
|
122 |
self.stdout.write('file can\'t be opened') |
|
123 |
return |
|
124 |
cursor = connections['default3'].cursor() |
|
125 |
cursor.execute("SELECT mc.id, mc.titre, se.titre, se.id\ |
|
126 |
FROM metaconf as mc\ |
|
127 |
INNER JOIN seminaire as se ON mc.fk_seminaire=se.id;" |
|
128 |
) |
|
129 |
webdata = cursor.fetchall() |
|
130 |
self.parser = etree.XMLParser(encoding='utf-8') |
|
131 |
self.myfile = csv.reader(csvfile) |
|
132 |
self.writefile = csv.writer(csvfile2) |
|
133 |
self.durations = dict([ |
|
134 |
(duration_def[0].strip(), self.parse_duration(duration_def[1].strip())) |
|
135 |
for duration_def in self.myfile |
|
136 |
]) |
|
137 |
|
|
138 |
for data in webdata: |
|
139 |
number = data[0] |
|
140 |
mysource = get_video(number) |
|
141 |
title = data[2] + " - " + data[1] |
|
142 |
ldtproject = requests.get(get_iri(number)).text |
|
143 |
duration = self.get_duration(mysource) |
|
144 |
description = '' |
|
145 |
creator = 'admin' |
|
146 |
videopath = '' |
|
147 |
tag = create_tag(data[3]) |
|
148 |
try: |
|
149 |
mymedia = models.Media.objects.get(src=mysource) |
|
150 |
mycontent = models.Content.objects.get(media_obj_id=mymedia.id) |
|
151 |
myproject = models.Project.objects.get(title='front project : %s' % title) |
|
152 |
except (models.Media.MultipleObjectsReturned, |
|
153 |
models.Content.MultipleObjectsReturned, |
|
154 |
models.Project.MultipleObjectsReturned |
|
155 |
): |
|
156 |
continue |
|
157 |
except (models.Media.DoesNotExist, |
|
158 |
models.Content.DoesNotExist, |
|
159 |
models.Project.DoesNotExist |
|
160 |
): |
|
161 |
if requests.head(mysource).status_code == 200: |
|
162 |
management.call_command( |
|
163 |
'createmediacontent', |
|
164 |
source=mysource, |
|
165 |
title=title.encode('utf-8') if title else '', |
|
166 |
videopath=videopath, |
|
167 |
description=description, |
|
168 |
duration=duration, |
|
169 |
public=True, |
|
170 |
creator=creator, |
|
171 |
update=True, |
|
172 |
tags=tag.encode('utf-8') if tag else '' |
|
173 |
) |
|
174 |
myfrontproj = models.Project.objects.get(title='front project : %s' % title) |
|
175 |
myfrontproj.ldt = self.change_annotations(ldtproject, myfrontproj.ldt) |
|
176 |
myfrontproj.save() |
|
177 |
self.stdout.write("%s done"%title) |
|
178 |
self.writefile.writerow([mysource, |
342
|
179 |
models.Content.objects.get(title=title).iri_id, |
|
180 |
title.encode('utf-8') if title else '', |
|
181 |
myfrontproj.ldt_id, |
|
182 |
]) |
341
|
183 |
csvfile.close() |
|
184 |
csvfile2.close() |