src/ldtplatform/management/commands/replacedelete.py
author bellierp
Fri, 24 Mar 2017 15:03:50 +0100
changeset 332 324717f075f9
parent 331 509b57af708d
child 333 77b56a7aaa7e
permissions -rw-r--r--
better informations about objects changed

'''
List flv and f4v medias, replace them with mp4 urls and update the content and projects.
'''
import csv
import logging
import re
from itertools import chain

import requests
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from django.core import management
from lxml import etree

from ldt.ldt_utils import models


#this function replace bad suffixs and prefixs of some media URL
#by a new one, beginning with "http" and ending with ".mp4"
def tohttps(source, vidpath, tomp4=1):
    '''
    to https
    '''
    if source[len(source)-3:len(source)] == 'MP4' or source[len(source)-3:len(source)] == 'mp4'\
        or not re.match(r".*\..{3}$", source):
        tomp4 = 0
    if tomp4 == 1:
        source = source[0:len(source)-3]+"mp4"
    if source[0:5] == "https":
        return source
    elif source[0:4] == "http" or source[0:4] == "sftp":
        return "https"+source[4:len(source)]
    elif source[0:7] == "/video/":
        return "https://media.iri.centrepompidou.fr"+source
    elif source[0:6] == "video/" or source[0:6] == "audio/":
        return "https://media.iri.centrepompidou.fr/"+source
    elif vidpath == 'rtmp://media.iri.centrepompidou.fr/ddc_player/video/regardssignes/' or \
        vidpath == 'rtmp://media.iri.centrepompidou.fr/ddc_player/mp4:video/regardssignes/':
        return "https://media.iri.centrepompidou.fr/video/regardssignes/"+source
    elif source[0:4] == "mp4:":
        if vidpath == 'rtmp://media.iri.centrepompidou.fr/ddc_player/':
            if re.match(r".*\..{3}$", source):
                return "https://media.iri.centrepompidou.fr/" + source[4:]
            return "https://media.iri.centrepompidou.fr/" + source[4:] + ".mp4"
    return "https://media.iri.centrepompidou.fr/video/ldtplatform/"+source


def numberofcontents(source):    #this counts the number of contents linked to a media
    '''
    numberofcontents
    '''
    return len(models.Content.objects.filter(media_obj_id=source.id))

def numberofproject(source):
    '''
    numberofproject
    '''
    if numberofcontents(source) > 0:
        return len(models.Project.objects.filter\
                (content=models.Content.objects.filter(media_obj_id=source.id)[0]))
    return 0

def constructytembed(source):
    '''
    construct youtube video oembed link
    '''
    if re.match(r".*feature=player_embedded.+", source) != None:
        return "http://www.youtube.com/oembed?url=http://youtube.com/watch?v="\
        + source[len(source)-11:] +"&format=json"
    return "http://www.youtube.com/oembed?url=" + source + "&format=json"

class Command(BaseCommand):
    '''
    Command class
    '''
    help = 'delete medias without contents, replace media\'s source by a new URL'
    base_url = Site.objects.get_current().domain + settings.BASE_URL
    parser = etree.XMLParser(encoding='utf-8')
    logger = logging.getLogger(__name__)
    csvfile = open('mediaInformations.csv', 'wb')
    mycsvfile = csv.writer(csvfile)

    def constructldtembed(self, ldtid):
        '''
        construct ldt embed
        '''
        return "http://{base_url}ldtplatform/ldt/embed/v3/config?json_url=" \
                   "http://{base_url}ldtplatform/ldt/cljson/id/{ldt_id}&" \
                   "player_id=player_project_{ldt_id}&" \
                   "ldt_id={ldt_id}".format(base_url=Command.base_url, ldt_id=ldtid)

    def cleanmediaproject(self, element, force, newsrc=None):
        '''
        change media objects' videopath and source if necessary
        change project .ldt
        '''
        basesrc = element.src
        if force:
            element.videopath = ''
            element.save()
        if newsrc != None:
            if force:
                element.src = newsrc
                element.save()
            Command.mycsvfile.writerow([
                "Media",
                basesrc,
                "Yes",
                "changing source/videopath",
                newsrc, "\'\'"
                ])
        if numberofproject(element) == 0:
            Command.mycsvfile.writerow([
                "Project",
                element.src,
                "Yes",
                "initializing object(no project)"
                ])
            if force:
                mycontentid = models.Content.objects.filter(media_obj_id=element.id)[0].iri_id
                try:
                    management.call_command('initfrontproject', mycontentid)
                except Exception:
                    Command.mycsvfile.writerow([
                    "Project",
                    element.src,
                    "No",
                    "socket error"
                    ])
                    return
                self.stdout.write(" Initializing project", ending='')
            else:
                self.stdout.write(" Project has to be initialized ", ending='')
                return
        ldtproj = models.Project.objects.filter\
        (content=models.Content.objects.filter(media_obj_id=element.id)[0])
        for singleproject in ldtproj:
            root = etree.XML(singleproject.ldt.encode('utf-8'), Command.parser)
            if root.xpath('medias/media') == []:
                self.stdout.write(" le .ldt ne contient pas de media", ending='')
                continue
            if root.xpath('medias/media')[0].get("video") != '':
                embedurl = self.constructldtembed(singleproject.ldt_id)
                if force:
                    root.xpath('medias/media')[0].set("video", '')
                self.stdout.write(" changing videopath arg in .ldt ")
                Command.mycsvfile.writerow([
                    "Project",
                    embedurl,
                    "Yes",
                    "changing .ldt /medias/media/video",
                    "\'\'"
                    ])
                singleproject.ldt = etree.tostring(root)
                singleproject.save()
                Command.logger.info("%s DONE\n", embedurl)
        element.save()

    def add_arguments(self, parser):
        '''
        add arguments
        '''
        parser.add_argument('-f', action='store_true')

    def handle(self, *args, **options):
        '''
        handle
        '''
        Command.mycsvfile.writerow([
            "Object type",
            "which object",
            "Change ?",
            "What(if Y)/Why (if N)",
            "How"
            ])

        force = bool(options['f'])
        j = 0
        files1 = models.Media.objects.all() #this list contains every media
        for elem1 in files1:
            if numberofcontents(elem1) == 0:
                if force:
                    elem1.delete()  #if there is no content
                    #linked to the media, the media is removed for the database
                    self.stdout.write(" No content found, media has been removed")
                else:
                    self.stdout.write(" No content found, media will be removed")
                Command.mycsvfile.writerow([
                    "Media",
                    elem1.src,
                    "Yes",
                    "deleting object (no content)"
                    ])
                j += 1
                continue
            if elem1.src.lower() == tohttps(elem1.src, elem1.videopath).lower():
                self.cleanmediaproject(elem1, force)
            if re.match(r".*\.youtube\.com.*", elem1.src) != None\
            or re.match(r".*youtu\.be.+", elem1.src) != None:
                myembed = constructytembed(elem1.src)
                if requests.get(myembed).status_code == 404:
                    self.stdout.write("%s : Video doesn't exists"% elem1.src)
                    if numberofproject(elem1) > 0:
                        ldtproj = models.Project.objects.get(id=models.Content.objects.filter\
                        (media_obj_id=elem1.id)[0].front_project_id).ldt
                        root = etree.XML(ldtproj.encode('utf-8'), Command.parser)
                        if root.xpath('annotations/content/ensemble/decoupage/elements/element')\
                        == []:
                            if force:
                                elem1.delete()
                                self.stdout.write("video doesn't exist anymore : media deleted")
                            Command.mycsvfile.writerow([
                                "Media/Content/Project",
                                elem1.src,
                                "Yes",
                                "deleting(Video doesn't exist anymore + empty projects)"
                                ])
                            j += 1
                else:
                    self.cleanmediaproject(elem1,force)
        if force:
            self.stdout.write("%s files deleted"%j)
        else:
            self.stdout.write("%s files to delete"%j)
        i = 0
        files = list(chain(
            models.Media.objects.filter(src__iregex=r".*.flv$"),
            models.Media.objects.filter(src__iregex=r".*.f4v$"),
            models.Media.objects.filter(src__iregex=r".*.m4v$"),
            models.Media.objects.filter(src__iregex=r".*.mp4$").exclude(src__iregex=r"^https://.*"),
            models.Media.objects.filter(src__iregex=r"^mp4:.*").exclude(src__iregex=r".*\..{3}$")
        ))

        for elem in files:
            self.stdout.write(" \n%s/%s files done"%(i+1, len(files)), ending='')
            i += 1
            if numberofcontents(elem) == 0:
                continue
            mysrc = elem.src
            newsource = tohttps(elem.src, elem.videopath)
            try:
                res = requests.head(newsource, timeout=10).status_code
            except requests.ConnectionError:
                self.stdout.write(" connection error", ending='')
                Command.logger.error("CONNECTION ERROR FOR %s", elem.title)
                try:
                    res = requests.head(elem, timeout=10).status_code
                except requests.ConnectionError:
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "connection error",
                        newsource
                        ])
                    continue
                except (requests.exceptions.MissingSchema, requests.exceptions.InvalidSchema):
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "missing schema on base source!",
                        newsource
                        ])
                    continue
                except requests.exceptions.Timeout:
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "TIMEOUT!",
                        newsource
                        ])
                    continue
                else:
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "use source link : website doesn't work with https",
                        newsource
                        ])
                    continue
            except (requests.exceptions.MissingSchema, requests.exceptions.InvalidSchema):
                self.stdout.write(" Missing schema !", ending='')
                Command.logger.warning("MISSING SCHEMA FOR %s", elem.title)
                Command.mycsvfile.writerow([
                    "Media",
                    mysrc,
                    "No",
                    "missing schema!",
                    newsource
                    ])
                continue
            except requests.exceptions.Timeout:
                self.stdout.write(" Timeout !", ending='')
                Command.logger.warning("Timeout FOR %s", elem.title)
                Command.mycsvfile.writerow([
                    "Media",
                    mysrc,
                    "No",
                    "TIMEOUT!",
                    newsource
                    ])
                continue
            if res > 400:
                try:
                    ressrc = requests.head(tohttps(elem.src, elem.videopath, 0),\
                    timeout=10).status_code
                except (requests.exceptions.Timeout, requests.ConnectionError):
                    self.stdout.write(" can't access source/new files", ending='')
                    Command.logger.warning("can't access %s", elem.title)
                    res = "connection error"
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "website doesn't exist anymore",
                        newsource
                        ])
                    continue
                if ressrc == 404:
                    self.stdout.write(" can't access source/new files", ending='')
                    Command.logger.warning("can't access %s", elem.title)
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "can't access source/new files",
                        newsource
                        ])
                elif ressrc == 200:
                    self.stdout.write(
                        " file not transcoded yet :"
                        "keep source extension or wait transcoding to be done",
                        ending='')
                    Command.logger.warning("%s not transcoded yet", elem.title)
                    Command.mycsvfile.writerow([
                        "Media",
                        mysrc,
                        "No",
                        "file not transcoded yet : keep source extension",
                        newsource
                        ])
                continue
            self.stdout.write(" It works", ending='')
            alreadyin = False
            for everyelem in models.Media.objects.all():
                if newsource == everyelem.src:
                    alreadyin = True
                    break
            if alreadyin:
                self.stdout.write(" element already in table", ending='')
                Command.logger.warning("%s already in table", elem.title)
                Command.mycsvfile.writerow([
                    "Media",
                    newsource,
                    "No",
                    "new source already in table"
                    ])
                continue
            self.cleanmediaproject(elem, force, newsource)
        Command.csvfile.close()