Correction on get_durations + small formatting pb
authorymh <ymh.work@gmail.com>
Thu, 20 Apr 2017 13:57:51 +0200
changeset 337 6152504f5452
parent 336 ff3b847c14a2
child 338 187513b9d07a
Correction on get_durations + small formatting pb
src/ldtplatform/management/commands/importamateur.py
--- a/src/ldtplatform/management/commands/importamateur.py	Thu Apr 13 16:20:26 2017 +0200
+++ b/src/ldtplatform/management/commands/importamateur.py	Thu Apr 20 13:57:51 2017 +0200
@@ -3,13 +3,13 @@
 '''
 import csv
 import re
-import sys
+
 import requests
-
+from django.core import management
 from django.core.management.base import BaseCommand
-from django.core import management
+from django.db import connections
 from lxml import etree
-from django.db import connections
+
 from ldt.ldt_utils import models
 
 
@@ -18,12 +18,15 @@
     create .iri source
     '''
     return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4"
+
+
 def create_iri_url(iri_url):
     '''
     create iri url
     '''
     return "http://amateur.iri.centrepompidou.fr" + iri_url
 
+
 def create_title(edition, day, name, order, session):
     '''
     create title
@@ -33,7 +36,10 @@
     else:
         event = "ENMI "
     session += 1
-    return event + edition[0:4] + " - " + str(day) + "." + str(session) + "." + str(order) + " - " + name
+    return event + edition[0:4] + " - " + str(day) + "." \
+        + str(session) + "." + str(order) + " - " + name
+
+
 def create_name(fname, lname):
     '''
     create name
@@ -46,6 +52,7 @@
         return fname
     return fname + " " + lname
 
+
 def create_tmp(edition, title):
     '''
     create temp to check medias with two authors
@@ -56,6 +63,7 @@
         length = 18
     return title[0:length]
 
+
 def create_tag(edition):
     '''
     create name
@@ -65,10 +73,21 @@
     else:
         event = "enmi"
     return event + edition[0:4]
+
+
 class Command(BaseCommand):
     '''
     command
     '''
+
+    def __init__(self, *args, **kwargs):
+        super(Command, self).__init__(*args, **kwargs)
+        self.parser = None
+        self.myfile = None
+        self.writefile = None
+        self.durations = None
+
+
     def add_arguments(self, parser):
         '''
         add arguments
@@ -85,18 +104,18 @@
                             default=None
                            )
 
+    def parse_duration(self, xmlstr):
+        '''
+        parse duration from xml def
+        '''
+        root = etree.XML(xmlstr.encode('utf-8'), self.parser)
+        return root.xpath('format')[0].get('duration')[:7]
+
     def get_duration(self, elem):
         '''
         get duration
         '''
-        for element in self.mylist:
-            element[0] = element[0][:len(element[0])-1]
-            if elem == element[0]:
-                element[1] = element[1][1:]
-                root = etree.XML(element[1].encode('utf-8'), self.parser)
-                duration = root.xpath('format')[0].get('duration')
-                duration = duration[:7]
-                return duration
+        return self.durations.get(elem)
 
     def change_annotations(self, iriin, ldtout):
         '''
@@ -111,10 +130,13 @@
             rootout.xpath('annotations/content')[0].append(ensemble)
             for decoup in decoups:
                 iddec = decoup.get("id")
-                nouveaudecoup = '<decoupage idens=\"' + idens +'\" id=\"' + iddec +'\" tagsSelect=\"\"/>'
+                nouveaudecoup = '<decoupage idens=\"' \
+                    + idens +'\" id=\"' \
+                    + iddec +'\" tagsSelect=\"\"/>'
                 nouveaudecxml = etree.fromstring(nouveaudecoup)
                 rootout.xpath('displays/display/content')[0].append(nouveaudecxml)
         return etree.tostring(rootout)
+
     def handle(self, *args, **options):
         '''
         handle command
@@ -149,12 +171,13 @@
                         LEFT JOIN conf_person AS pers ON spkr.person_id=pers.id;'
                       )
         amateurdata = cursor.fetchall()
-        reload(sys)
-        sys.setdefaultencoding('utf8')
         self.parser = etree.XMLParser(encoding='utf-8')
         self.myfile = csv.reader(csvfile)
         self.writefile = csv.writer(csvfile2)
-        self.mylist = list(self.myfile)
+        self.durations = dict([
+            (duration_def[0].strip(), self.parse_duration(duration_def[1].strip()))
+            for duration_def in self.myfile
+        ])
         titletmp = ''
         for mediaproj in amateurdata:
             iri_id = mediaproj[0]
@@ -207,27 +230,27 @@
             description = mediaproj[3]
             # ldt = mediaproj[4]
             myiriurl = create_iri_url(iriurl)
-            myiri = requests.get(myiriurl)._content
+            myiri = requests.get(myiriurl).text
             duration = self.get_duration(mysource)
             if requests.head(mysource).status_code == 200:
                 management.call_command(
                     'createmediacontent',
                     source=mysource,
-                    title=title,
+                    title=title.encode('utf-8') if title else '',
                     videopath='',
-                    description=description,
+                    description=description.encode('utf-8') if description else '',
                     duration=duration,
                     public=True,
                     creator='admin',
                     update=True,
-                    tags=tag
+                    tags=tag.encode('utf-8') if tag else ''
                     )
                 myfrontproj = models.Project.objects.get(title='front project : %s' % title)
                 myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
                 self.writefile.writerow([mysource,
                                          iri_id,
                                          models.Content.objects.get(title=title).iri_id,
-                                         title,
+                                         title.encode('utf-8') if title else '',
                                          myfrontproj.ldt_id,
                                         ])
                 self.stdout.write("Project changed")