--- a/src/ldtplatform/management/commands/replacedelete.py Fri Mar 24 15:03:50 2017 +0100
+++ b/src/ldtplatform/management/commands/replacedelete.py Thu Mar 30 16:17:24 2017 +0200
@@ -18,7 +18,7 @@
#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):
+def to_https(source, vidpath, tomp4=1):
'''
to https
'''
@@ -46,22 +46,22 @@
return "https://media.iri.centrepompidou.fr/video/ldtplatform/"+source
-def numberofcontents(source): #this counts the number of contents linked to a media
+def number_of_contents(source): #this counts the number of contents linked to a media
'''
- numberofcontents
+ number_of_contents
'''
return len(models.Content.objects.filter(media_obj_id=source.id))
-def numberofproject(source):
+def number_of_projects(source):
'''
- numberofproject
+ number_of_projects
'''
- if numberofcontents(source) > 0:
+ if number_of_contents(source) > 0:
return len(models.Project.objects.filter\
(content=models.Content.objects.filter(media_obj_id=source.id)[0]))
return 0
-def constructytembed(source):
+def construct_youtube_embed(source):
'''
construct youtube video oembed link
'''
@@ -75,22 +75,33 @@
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):
+ def add_arguments(self, parser):
+ '''
+ add arguments
+ '''
+ parser.add_argument('-f',
+ '--force',
+ dest='force',
+ action='store_true'
+ )
+ parser.add_argument('-p',
+ '--path',
+ dest='path',
+ default=None
+ )
+
+
+ def construct_ldt_embed(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)
+ "ldt_id={ldt_id}".format(base_url=self.base_url, ldt_id=ldtid)
- def cleanmediaproject(self, element, force, newsrc=None):
+ def clean_media_project(self, element, force, newsrc=None):
'''
change media objects' videopath and source if necessary
change project .ldt
@@ -103,15 +114,15 @@
if force:
element.src = newsrc
element.save()
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
basesrc,
"Yes",
"changing source/videopath",
newsrc, "\'\'"
])
- if numberofproject(element) == 0:
- Command.mycsvfile.writerow([
+ if number_of_projects(element) == 0:
+ self.mycsvfile.writerow([
"Project",
element.src,
"Yes",
@@ -122,12 +133,12 @@
try:
management.call_command('initfrontproject', mycontentid)
except Exception:
- Command.mycsvfile.writerow([
- "Project",
- element.src,
- "No",
- "socket error"
- ])
+ self.mycsvfile.writerow([
+ "Project",
+ element.src,
+ "No",
+ "socket error"
+ ])
return
self.stdout.write(" Initializing project", ending='')
else:
@@ -136,16 +147,16 @@
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)
+ root = etree.XML(singleproject.ldt.encode('utf-8'), self.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)
+ embedurl = self.construct_ldt_embed(singleproject.ldt_id)
if force:
root.xpath('medias/media')[0].set("video", '')
- self.stdout.write(" changing videopath arg in .ldt ")
- Command.mycsvfile.writerow([
+ self.stdout.write(" changing videopath arg in .ldt ", ending='')
+ self.mycsvfile.writerow([
"Project",
embedurl,
"Yes",
@@ -154,39 +165,47 @@
])
singleproject.ldt = etree.tostring(root)
singleproject.save()
- Command.logger.info("%s DONE\n", embedurl)
+ self.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([
+ self.base_url = Site.objects.get_current().domain + settings.BASE_URL
+ self.parser = etree.XMLParser(encoding='utf-8')
+ self.logger = logging.getLogger(__name__)
+ path = options['path']
+ force = options['force']
+ if not path:
+ path = 'mediaInformations.csv'
+ try:
+ csvfile = open(path, 'wb')
+ except IOError:
+ self.stdout.write('file can\'t be opened')
+ self.logger.error('cant open file')
+ return
+ self.mycsvfile = csv.writer(csvfile)
+ self.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 number_of_contents(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([
+ self.mycsvfile.writerow([
"Media",
elem1.src,
"Yes",
@@ -194,23 +213,23 @@
])
j += 1
continue
- if elem1.src.lower() == tohttps(elem1.src, elem1.videopath).lower():
- self.cleanmediaproject(elem1, force)
+ if elem1.src.lower() == to_https(elem1.src, elem1.videopath).lower():
+ self.clean_media_project(elem1, force)
if re.match(r".*\.youtube\.com.*", elem1.src) != None\
or re.match(r".*youtu\.be.+", elem1.src) != None:
- myembed = constructytembed(elem1.src)
+ myembed = construct_youtube_embed(elem1.src)
if requests.get(myembed).status_code == 404:
self.stdout.write("%s : Video doesn't exists"% elem1.src)
- if numberofproject(elem1) > 0:
+ if number_of_projects(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)
+ root = etree.XML(ldtproj.encode('utf-8'), self.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([
+ self.mycsvfile.writerow([
"Media/Content/Project",
elem1.src,
"Yes",
@@ -218,7 +237,7 @@
])
j += 1
else:
- self.cleanmediaproject(elem1,force)
+ self.clean_media_project(elem1, force)
if force:
self.stdout.write("%s files deleted"%j)
else:
@@ -235,19 +254,19 @@
for elem in files:
self.stdout.write(" \n%s/%s files done"%(i+1, len(files)), ending='')
i += 1
- if numberofcontents(elem) == 0:
+ if number_of_contents(elem) == 0:
continue
mysrc = elem.src
- newsource = tohttps(elem.src, elem.videopath)
+ newsource = to_https(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)
+ self.logger.error("CONNECTION ERROR FOR %s", elem.title)
try:
res = requests.head(elem, timeout=10).status_code
except requests.ConnectionError:
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -256,7 +275,7 @@
])
continue
except (requests.exceptions.MissingSchema, requests.exceptions.InvalidSchema):
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -265,7 +284,7 @@
])
continue
except requests.exceptions.Timeout:
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -274,7 +293,7 @@
])
continue
else:
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -284,8 +303,8 @@
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([
+ self.logger.warning("MISSING SCHEMA FOR %s", elem.title)
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -295,8 +314,8 @@
continue
except requests.exceptions.Timeout:
self.stdout.write(" Timeout !", ending='')
- Command.logger.warning("Timeout FOR %s", elem.title)
- Command.mycsvfile.writerow([
+ self.logger.warning("Timeout FOR %s", elem.title)
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -306,13 +325,13 @@
continue
if res > 400:
try:
- ressrc = requests.head(tohttps(elem.src, elem.videopath, 0),\
+ ressrc = requests.head(to_https(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)
+ self.logger.warning("can't access %s", elem.title)
res = "connection error"
- Command.mycsvfile.writerow([
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -322,8 +341,8 @@
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([
+ self.logger.warning("can't access %s", elem.title)
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -333,10 +352,10 @@
elif ressrc == 200:
self.stdout.write(
" file not transcoded yet :"
- "keep source extension or wait transcoding to be done",
+ "keep source extension or wait transcoding to be done",\
ending='')
- Command.logger.warning("%s not transcoded yet", elem.title)
- Command.mycsvfile.writerow([
+ self.logger.warning("%s not transcoded yet", elem.title)
+ self.mycsvfile.writerow([
"Media",
mysrc,
"No",
@@ -352,13 +371,13 @@
break
if alreadyin:
self.stdout.write(" element already in table", ending='')
- Command.logger.warning("%s already in table", elem.title)
- Command.mycsvfile.writerow([
+ self.logger.warning("%s already in table", elem.title)
+ self.mycsvfile.writerow([
"Media",
newsource,
"No",
"new source already in table"
])
continue
- self.cleanmediaproject(elem, force, newsource)
- Command.csvfile.close()
+ self.clean_media_project(elem, force, newsource)
+ csvfile.close()