Skip to content
Snippets Groups Projects
Commit ffec6735 authored by Julian Rother's avatar Julian Rother
Browse files

Implemented sort/encoded api endpoint

parent a9f1d677
No related branches found
No related tags found
No related merge requests found
...@@ -19,19 +19,18 @@ def sort_log(): ...@@ -19,19 +19,18 @@ def sort_log():
LIMIT 50 LIMIT 50
'''),sorterrorlog=query('SELECT * FROM sorterrorlog ORDER BY sorterrorlog.`when` DESC')) '''),sorterrorlog=query('SELECT * FROM sorterrorlog ORDER BY sorterrorlog.`when` DESC'))
def to_ascii(inputstring): def to_ascii(inputstring):
asciistring = inputstring asciistring = inputstring
for charset in [('ä', 'ae'), ('ö', 'oe'), ('ü', 'ue'), ('ß', 'ss')]: for charset in [('ä', 'ae'), ('ö', 'oe'), ('ü', 'ue'), ('ß', 'ss')]:
asciistring = asciistring.replace(charset[0],charset[1]) asciistring = asciistring.replace(charset[0],charset[1])
return asciistring return asciistring
def insert_video(lectureid,dbfilepath,filepath,fileformatid): def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1):
video_id = modify('''INSERT INTO videos_data 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)
VALUES VALUES
(?,0,?,?,"","","",?,?,?,?,"",?)''', (?, 0, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?)''',
lectureid, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, os.stat(filepath).st_size) lectureid, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize)
query('INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now()) query('INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now())
schedule_thumbnail(lectureid) schedule_thumbnail(lectureid)
schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True}) schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})
...@@ -55,10 +54,11 @@ def schedule_thumbnail(lectureid, filePath=None): ...@@ -55,10 +54,11 @@ def schedule_thumbnail(lectureid, filePath=None):
def sort_file(filename, course=None, lectures=None): def sort_file(filename, course=None, lectures=None):
# filenames: <handle>-<sorter>-<format>.mp4 # filenames: <handle>-<sorter>-<format>.mp4
# "sorter" musst be found with fuzzy matching. "sorter" musst be one or more of the following types: (inside the loop) # "sorter" musst be found with fuzzy matching. "sorter" musst be one or more of the following types: (inside the loop)
# '_' and ' ' are handled like '-' # '_' and ' ' are handled like '-'
splitfilename = filename.replace('_','-').replace(' ','-').split('-') splitfilename = filename.replace('_','-').replace(' ','-').split('-')
if not course: if not course:
handle = splitfilename[0]
if splitfilename[0].endswith('ws') or splitfilename[0].endswith('ss'):
handle = '-'.join(splitfilename[:2]) handle = '-'.join(splitfilename[:2])
courses = query('SELECT * FROM courses WHERE handle = ?', handle) courses = query('SELECT * FROM courses WHERE handle = ?', handle)
if not courses: if not courses:
...@@ -87,7 +87,6 @@ def sort_file(filename, course=None, lectures=None): ...@@ -87,7 +87,6 @@ def sort_file(filename, course=None, lectures=None):
data['keywords'].append(s) data['keywords'].append(s)
# try to match the file on a single lecture # try to match the file on a single lecture
matches = [] matches = []
# first try date and time (if one of them is set) # first try date and time (if one of them is set)
if ('date' in data) or ('time' in data): if ('date' in data) or ('time' in data):
for lecture in lectures: for lecture in lectures:
...@@ -119,14 +118,63 @@ def sort_file(filename, course=None, lectures=None): ...@@ -119,14 +118,63 @@ def sort_file(filename, course=None, lectures=None):
if found: if found:
break break
# now we should have found exactly one match # now we should have found exactly one match
return matches # default format is "unknown", with id 0
fmt = 0
formats = query('SELECT * FROM formats ORDER BY prio DESC')
for videoformat in formats:
# we match the last part of the file name without the extension
formatstring = splitfilename[-1].split('.',1)[0].lower()
if formatstring in videoformat['keywords'].replace(',',' ').split(' '):
fmt = videoformat['id']
break
return matches, fmt
def log_sort_error(course_id, path, matches):
matches_id = []
for match in matches:
matches_id.append(str(match['id']))
query('INSERT INTO sorterrorlog_data (course_id, path, matches, `when`, time_updated, time_created) VALUES (?, ?, ?, ?, ?, ?)',
course_id, path, ','.join(matches_id), datetime.now(), datetime.now(), datetime.now())
def sort_api_token_required(func):
@wraps(func)
def decorator(*args, **kwargs):
if 'apikey' in request.values:
token = request.values['apikey']
elif request.get_json() and ('apikey' in request.get_json()):
token = request.get_json()['apikey']
else:
token = None
if not token == config.get('SORTER_API_KEY', [None]):
return 'Permission denied', 403
else:
return func(*args, **kwargs)
return decorator
@app.route('/internal/sort/encoded/<filename>')
@sort_api_token_required
def sort_encoded(filename):
matches, fmt = sort_file(filename)
if len(matches) != 1:
log_sort_error(-1, 'kodiert/'+filename, matches)
return "Could not match filename", 400
lecture = matches[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
if course['autopublish']:
schedule_job('publish_video', {'source': filename, 'path': 'pub/'+course['handle']+'/'+filename, 'lecture_id': lecture['id'], 'format_id': fmt})
return 'OK', 200
@job_handler('publish_video')
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'])
@app.route('/internal/sort/now') @app.route('/internal/sort/now')
@mod_required @mod_required
@sched_func(600) @sched_func(600)
def sort_now(): def sort_now():
courses = query('SELECT * FROM courses') courses = query('SELECT * FROM courses')
formats = query('SELECT * FROM formats ORDER BY prio')
for course in courses: for course in courses:
modify('BEGIN') modify('BEGIN')
for mountpoint in config['VIDEOMOUNT']: for mountpoint in config['VIDEOMOUNT']:
...@@ -151,34 +199,16 @@ def sort_now(): ...@@ -151,34 +199,16 @@ def sort_now():
break break
if ignore: if ignore:
continue continue
filepath = coursepath + '/' + filename
if not os.path.splitext(filename)[1] == '.mp4': if not os.path.splitext(filename)[1] == '.mp4':
continue continue
matches = sort_file(filename, course=course, lectures=lectures) matches, fmt = sort_file(filename, course=course, lectures=lectures)
dbfilepath = mountpoint['prefix']+course['handle']+'/'+filename dbfilepath = mountpoint['prefix']+course['handle']+'/'+filename
if len(matches) == 1: if len(matches) == 1:
# now match the format insert_video(matches[0]['id'], dbfilepath, fmt)
splitfilename = filename.replace('_','-').replace(' ','-').split('-')
# default format is "unknown", with id 0
fmt = 0
for videoformat in formats:
#we match the last part of the file name without the extension
formatstring = splitfilename[-1].split('.',1)[0].lower()
if formatstring in videoformat['keywords'].replace(',',' ').split(' '):
fmt = videoformat['id']
break
# insert the video into videos_data and log
insert_video( matches[0]['id'], dbfilepath, filepath, fmt)
else: else:
# if we couldn't match the video on exactly one lecture, log an error log_sort_error(course['id'], dbfilepath, matches)
matches_id = []
for match in matches:
matches_id.append(str(match['id']))
query('INSERT INTO sorterrorlog_data (course_id,path,matches,`when`,time_updated,time_created) VALUES (?,?,?,?,?,?)', course['id'], dbfilepath, ','.join(matches_id), datetime.now(), datetime.now(), datetime.now())
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
modify('COMMIT') modify('COMMIT')
if 'ref' in request.values: if 'ref' in request.values:
return redirect(request.values['ref']) return redirect(request.values['ref'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment