From 0ad8cc94ac6cfb2e4f638f6b6439bd78dcf57851 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Wed, 3 Jan 2018 00:54:12 +0100
Subject: [PATCH] Added duration field to videos table and extended rss feed
 accordingly

---
 db_schema.sql      |  3 ++-
 sorter.py          | 20 ++++++++++----------
 templates/feed.rss |  5 ++++-
 3 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/db_schema.sql b/db_schema.sql
index 30c5c58..503102d 100644
--- a/db_schema.sql
+++ b/db_schema.sql
@@ -229,7 +229,8 @@ CREATE TABLE IF NOT EXISTS `videos_data` (
   `file_size` bigINTEGER NOT NULL DEFAULT '-1',
   `video_format` INTEGER NOT NULL,
   `hash` varchar(32) NOT NULL,
-  `source` INTEGER
+  `source` INTEGER,
+  `duration` INTEGER NOT NULL
 );
 CREATE TABLE IF NOT EXISTS `announcements` (
 `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
diff --git a/sorter.py b/sorter.py
index 31ae38a..44eb20c 100644
--- a/sorter.py
+++ b/sorter.py
@@ -35,16 +35,16 @@ def update_video_metadata(jobid, jobtype, data, state, status):
 		if video['hash'] and video['hash'] != status['hash']:
 			print('Hash mismatch for video', data['video_id'])
 			return
-	modify('UPDATE videos_data SET hash = ?, file_size = ? WHERE id = ?',
-			status['hash'], status['filesize'], data['video_id'])
+	modify('UPDATE videos_data SET hash = ?, file_size = ?, duration = ? WHERE id = ?',
+			status['hash'], status['filesize'], data['duration'], data['video_id'])
 
-def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1):
+def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, duration=-1):
 	visible = query('SELECT courses.autovisible FROM courses JOIN lectures ON lectures.course_id = courses.id WHERE lectures.id = ?', lectureid)[0]['autovisible']
 	video_id = modify('''INSERT INTO videos_data 
-		(lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size)
+		(lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, duration)
 		VALUES 
-		(?, ?, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?)''',
-		lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize)
+		(?, ?, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?, ?)''',
+		lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration)
 	query('INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now())
 	schedule_thumbnail(lectureid)
 	schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})
@@ -79,12 +79,12 @@ def insert_transcoded_video(jobid, jobtype, data, state, status):
 		return
 	visible = query('SELECT courses.autovisible FROM courses JOIN lectures ON lectures.course_id = courses.id WHERE lectures.id = ?', data['lecture_id'])[0]['autovisible']
 	video_id = modify('''INSERT INTO videos_data 
-		(lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, source)
+		(lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, source, duration)
 		VALUES 
-		(?, ?, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?, ?)''',
+		(?, ?, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?, ?, ?)''',
 		data['lecture_id'], visible, data['output']['path'], data['format_id'],
 		datetime.now(), datetime.now(), datetime.now(), -1, status['hash'],
-		status['filesize'], data['source_id'])
+		status['filesize'], status['duration'], data['source_id'])
 	schedule_thumbnail(data['lecture_id'])
 	video = query('SELECT videos.*, "format" AS sep, formats.* FROM videos JOIN formats ON formats.id = videos.video_format WHERE videos.id = ?', video_id)[0]
 	lecture = query('SELECT * FROM lectures WHERE id = ?', data['lecture_id'])[0]
@@ -221,7 +221,7 @@ def sort_autoencode():
 def handle_published_video(jobid, jobtype, data, state, status):
 	if 'lecture_id' not in data or 'format_id' not in data:
 		return
-	insert_video(data['lecture_id'], data['path'], data['format_id'], hash=status['hash'], filesize=status['filesize'])
+	insert_video(data['lecture_id'], data['path'], data['format_id'], hash=status['hash'], filesize=status['filesize'], duration=status['duration'])
 
 @app.route('/internal/sort/now')
 @mod_required
diff --git a/templates/feed.rss b/templates/feed.rss
index f3c9871..4dd5193 100644
--- a/templates/feed.rss
+++ b/templates/feed.rss
@@ -18,7 +18,7 @@
 {% endif %}
 {%- endmacro %}
 <?xml version="1.0" encoding="UTF-8"?>
-<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:psc="http://podlove.org/simple-chapters" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
+<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:psc="http://podlove.org/simple-chapters" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
 <channel>
 <title>{{ title(format) }}</title>
 <link>{{ url_for('course', handle=course.handle, _external=True) }}</link>
@@ -48,6 +48,9 @@
 	{% if item.speaker and item.speaker != course.organizer %}
 	<dc:creator>{{ item.speaker|e }}</dc:creator>
 	{% endif %}
+	{% if item.video.duration > 0 %}
+	<itunes:duration>{{ item.video.duration }}</itunes:duration>
+	{% endif %}
 	<psc:chapters xmlns:psc="http://podlove.org/simple-chapters" version="1.2">
 		{% for chapter in chapters|selectattr('lecture_id','equalto',item.id) %}
 		<psc:chapter start="{{ chapter.time }}" title="{{ chapter.text }}"/>
-- 
GitLab