src/ldtplatform/management/commands/importamateur.py
changeset 337 6152504f5452
parent 336 ff3b847c14a2
equal deleted inserted replaced
336:ff3b847c14a2 337:6152504f5452
     1 '''
     1 '''
     2 Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri
     2 Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri
     3 '''
     3 '''
     4 import csv
     4 import csv
     5 import re
     5 import re
     6 import sys
     6 
     7 import requests
     7 import requests
     8 
     8 from django.core import management
     9 from django.core.management.base import BaseCommand
     9 from django.core.management.base import BaseCommand
    10 from django.core import management
    10 from django.db import connections
    11 from lxml import etree
    11 from lxml import etree
    12 from django.db import connections
    12 
    13 from ldt.ldt_utils import models
    13 from ldt.ldt_utils import models
    14 
    14 
    15 
    15 
    16 def create_source(iri_id):
    16 def create_source(iri_id):
    17     '''
    17     '''
    18     create .iri source
    18     create .iri source
    19     '''
    19     '''
    20     return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4"
    20     return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4"
       
    21 
       
    22 
    21 def create_iri_url(iri_url):
    23 def create_iri_url(iri_url):
    22     '''
    24     '''
    23     create iri url
    25     create iri url
    24     '''
    26     '''
    25     return "http://amateur.iri.centrepompidou.fr" + iri_url
    27     return "http://amateur.iri.centrepompidou.fr" + iri_url
       
    28 
    26 
    29 
    27 def create_title(edition, day, name, order, session):
    30 def create_title(edition, day, name, order, session):
    28     '''
    31     '''
    29     create title
    32     create title
    30     '''
    33     '''
    31     if re.match(r'....\_1', edition) or edition == '2009':
    34     if re.match(r'....\_1', edition) or edition == '2009':
    32         event = "Prepa ENMI "
    35         event = "Prepa ENMI "
    33     else:
    36     else:
    34         event = "ENMI "
    37         event = "ENMI "
    35     session += 1
    38     session += 1
    36     return event + edition[0:4] + " - " + str(day) + "." + str(session) + "." + str(order) + " - " + name
    39     return event + edition[0:4] + " - " + str(day) + "." \
       
    40         + str(session) + "." + str(order) + " - " + name
       
    41 
       
    42 
    37 def create_name(fname, lname):
    43 def create_name(fname, lname):
    38     '''
    44     '''
    39     create name
    45     create name
    40     '''
    46     '''
    41     if fname is None:
    47     if fname is None:
    44         return lname
    50         return lname
    45     if lname is None:
    51     if lname is None:
    46         return fname
    52         return fname
    47     return fname + " " + lname
    53     return fname + " " + lname
    48 
    54 
       
    55 
    49 def create_tmp(edition, title):
    56 def create_tmp(edition, title):
    50     '''
    57     '''
    51     create temp to check medias with two authors
    58     create temp to check medias with two authors
    52     '''
    59     '''
    53     if re.match(r'....\_1', edition) or edition == '2009':
    60     if re.match(r'....\_1', edition) or edition == '2009':
    54         length = 24
    61         length = 24
    55     else:
    62     else:
    56         length = 18
    63         length = 18
    57     return title[0:length]
    64     return title[0:length]
    58 
    65 
       
    66 
    59 def create_tag(edition):
    67 def create_tag(edition):
    60     '''
    68     '''
    61     create name
    69     create name
    62     '''
    70     '''
    63     if re.match(r'....\_1', edition) or edition == '2009':
    71     if re.match(r'....\_1', edition) or edition == '2009':
    64         event = "prepaenmi"
    72         event = "prepaenmi"
    65     else:
    73     else:
    66         event = "enmi"
    74         event = "enmi"
    67     return event + edition[0:4]
    75     return event + edition[0:4]
       
    76 
       
    77 
    68 class Command(BaseCommand):
    78 class Command(BaseCommand):
    69     '''
    79     '''
    70     command
    80     command
    71     '''
    81     '''
       
    82 
       
    83     def __init__(self, *args, **kwargs):
       
    84         super(Command, self).__init__(*args, **kwargs)
       
    85         self.parser = None
       
    86         self.myfile = None
       
    87         self.writefile = None
       
    88         self.durations = None
       
    89 
       
    90 
    72     def add_arguments(self, parser):
    91     def add_arguments(self, parser):
    73         '''
    92         '''
    74         add arguments
    93         add arguments
    75         '''
    94         '''
    76 
    95 
    83                             '--pathout',
   102                             '--pathout',
    84                             dest='pathout',
   103                             dest='pathout',
    85                             default=None
   104                             default=None
    86                            )
   105                            )
    87 
   106 
       
   107     def parse_duration(self, xmlstr):
       
   108         '''
       
   109         parse duration from xml def
       
   110         '''
       
   111         root = etree.XML(xmlstr.encode('utf-8'), self.parser)
       
   112         return root.xpath('format')[0].get('duration')[:7]
       
   113 
    88     def get_duration(self, elem):
   114     def get_duration(self, elem):
    89         '''
   115         '''
    90         get duration
   116         get duration
    91         '''
   117         '''
    92         for element in self.mylist:
   118         return self.durations.get(elem)
    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 
   119 
   101     def change_annotations(self, iriin, ldtout):
   120     def change_annotations(self, iriin, ldtout):
   102         '''
   121         '''
   103         change annotations
   122         change annotations
   104         '''
   123         '''
   109             idens = ensemble.get("id")
   128             idens = ensemble.get("id")
   110             decoups = ensemble.xpath('decoupage')
   129             decoups = ensemble.xpath('decoupage')
   111             rootout.xpath('annotations/content')[0].append(ensemble)
   130             rootout.xpath('annotations/content')[0].append(ensemble)
   112             for decoup in decoups:
   131             for decoup in decoups:
   113                 iddec = decoup.get("id")
   132                 iddec = decoup.get("id")
   114                 nouveaudecoup = '<decoupage idens=\"' + idens +'\" id=\"' + iddec +'\" tagsSelect=\"\"/>'
   133                 nouveaudecoup = '<decoupage idens=\"' \
       
   134                     + idens +'\" id=\"' \
       
   135                     + iddec +'\" tagsSelect=\"\"/>'
   115                 nouveaudecxml = etree.fromstring(nouveaudecoup)
   136                 nouveaudecxml = etree.fromstring(nouveaudecoup)
   116                 rootout.xpath('displays/display/content')[0].append(nouveaudecxml)
   137                 rootout.xpath('displays/display/content')[0].append(nouveaudecxml)
   117         return etree.tostring(rootout)
   138         return etree.tostring(rootout)
       
   139 
   118     def handle(self, *args, **options):
   140     def handle(self, *args, **options):
   119         '''
   141         '''
   120         handle command
   142         handle command
   121         '''
   143         '''
   122         pathin = options['pathin']
   144         pathin = options['pathin']
   147                         LEFT JOIN conf_edition AS edit ON day.edition_id=edit.event_ptr_id\
   169                         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\
   170                         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;'
   171                         LEFT JOIN conf_person AS pers ON spkr.person_id=pers.id;'
   150                       )
   172                       )
   151         amateurdata = cursor.fetchall()
   173         amateurdata = cursor.fetchall()
   152         reload(sys)
       
   153         sys.setdefaultencoding('utf8')
       
   154         self.parser = etree.XMLParser(encoding='utf-8')
   174         self.parser = etree.XMLParser(encoding='utf-8')
   155         self.myfile = csv.reader(csvfile)
   175         self.myfile = csv.reader(csvfile)
   156         self.writefile = csv.writer(csvfile2)
   176         self.writefile = csv.writer(csvfile2)
   157         self.mylist = list(self.myfile)
   177         self.durations = dict([
       
   178             (duration_def[0].strip(), self.parse_duration(duration_def[1].strip()))
       
   179             for duration_def in self.myfile
       
   180         ])
   158         titletmp = ''
   181         titletmp = ''
   159         for mediaproj in amateurdata:
   182         for mediaproj in amateurdata:
   160             iri_id = mediaproj[0]
   183             iri_id = mediaproj[0]
   161             mysource = create_source(iri_id)
   184             mysource = create_source(iri_id)
   162             dayorder = mediaproj[5]
   185             dayorder = mediaproj[5]
   205             #TODO set creationdate
   228             #TODO set creationdate
   206             #creationdate = mediaproj[2]
   229             #creationdate = mediaproj[2]
   207             description = mediaproj[3]
   230             description = mediaproj[3]
   208             # ldt = mediaproj[4]
   231             # ldt = mediaproj[4]
   209             myiriurl = create_iri_url(iriurl)
   232             myiriurl = create_iri_url(iriurl)
   210             myiri = requests.get(myiriurl)._content
   233             myiri = requests.get(myiriurl).text
   211             duration = self.get_duration(mysource)
   234             duration = self.get_duration(mysource)
   212             if requests.head(mysource).status_code == 200:
   235             if requests.head(mysource).status_code == 200:
   213                 management.call_command(
   236                 management.call_command(
   214                     'createmediacontent',
   237                     'createmediacontent',
   215                     source=mysource,
   238                     source=mysource,
   216                     title=title,
   239                     title=title.encode('utf-8') if title else '',
   217                     videopath='',
   240                     videopath='',
   218                     description=description,
   241                     description=description.encode('utf-8') if description else '',
   219                     duration=duration,
   242                     duration=duration,
   220                     public=True,
   243                     public=True,
   221                     creator='admin',
   244                     creator='admin',
   222                     update=True,
   245                     update=True,
   223                     tags=tag
   246                     tags=tag.encode('utf-8') if tag else ''
   224                     )
   247                     )
   225                 myfrontproj = models.Project.objects.get(title='front project : %s' % title)
   248                 myfrontproj = models.Project.objects.get(title='front project : %s' % title)
   226                 myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
   249                 myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
   227                 self.writefile.writerow([mysource,
   250                 self.writefile.writerow([mysource,
   228                                          iri_id,
   251                                          iri_id,
   229                                          models.Content.objects.get(title=title).iri_id,
   252                                          models.Content.objects.get(title=title).iri_id,
   230                                          title,
   253                                          title.encode('utf-8') if title else '',
   231                                          myfrontproj.ldt_id,
   254                                          myfrontproj.ldt_id,
   232                                         ])
   255                                         ])
   233                 self.stdout.write("Project changed")
   256                 self.stdout.write("Project changed")
   234                 myfrontproj.save()
   257                 myfrontproj.save()
   235         csvfile.close()
   258         csvfile.close()