From f96815c17540c8e3ff9eadd2e69c488145e1e04d Mon Sep 17 00:00:00 2001 From: j Date: Thu, 18 Apr 2024 11:17:30 +0200 Subject: [PATCH] add render_public_edits --- edits/index.html | 0 edits/player.html | 25 +++++++++ render_public_edits.py | 117 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 edits/index.html create mode 100644 edits/player.html create mode 100755 render_public_edits.py diff --git a/edits/index.html b/edits/index.html new file mode 100644 index 0000000..e69de29 diff --git a/edits/player.html b/edits/player.html new file mode 100644 index 0000000..64bce76 --- /dev/null +++ b/edits/player.html @@ -0,0 +1,25 @@ + + diff --git a/render_public_edits.py b/render_public_edits.py new file mode 100755 index 0000000..5896a92 --- /dev/null +++ b/render_public_edits.py @@ -0,0 +1,117 @@ +#!/usr/bin/python3 + +from datetime import datetime, timedelta +from urllib.parse import quote +import hashlib +import json +import os +import subprocess +import sys +import unicodedata + + +import ox +import ox.api + + +sort_edits = {} + + +def normalize(id): + name = id.split(":")[0] + id = id.replace(':', '_').replace('/', '_') + prefix = hashlib.sha1(('firends' + name).encode()).hexdigest()[:8] + return '%s_%s' % (prefix, id) + + +if __name__ == '__main__': + site = sys.argv[1] + users = sys.argv[2:] + api = ox.api.signin('https://%s/api/' % site) + + public_names = [] + + r = api.findEdits({ + 'query': { + 'conditions': [{'key': 'status', 'operator': '!==', 'value': 'private'}] + }, + 'range': [0, 1000], + 'keys': ['id', 'status', 'modified', 'type'] + }) + for edit in r['data']['items']: + id = edit['id'] + if users and id.split(':')[0] not in users: + continue + for sort in sort_edits.get(id, ['index']): + if edit['type'] == 'smart' and sort == 'index': + sort = 'year' + edit_name = 'edits/%s_%s.json' % (normalize(id), sort) + modified = int(datetime.strptime(edit['modified'], '%Y-%m-%dT%H:%M:%SZ').timestamp()) + if not os.path.exists(edit_name) or os.path.getmtime(edit_name) < modified: + print('update', id, sort) + url = 'https://%s/edits/%s/list/%s' % (site, quote(id.replace('_', '\t')), sort) + print(url) + cmd = ['../edit.py', url, '-r', '720', '-s', 'local', '-o', edit_name] + subprocess.call(cmd) + cmd = ['../ffmpeg.py', '-r', '720', edit_name] + subprocess.call(cmd) + srt = edit_name.replace('.json', '.srt') + vtt = edit_name.replace('.json', '.vtt') + cmd = [ + 'ffmpeg', + '-hide_banner', + '-nostats', '-loglevel', 'error', + '-y', '-i', srt, vtt + ] + subprocess.call(cmd) + public_names.append(edit_name.replace('.json', '')) + + #print('\n'.join(public_names)) + #print('--') + for f in os.listdir('edits'): + if f.endswith('.html'): + continue + f = 'edits/' + f + if '.' in f: + name, ext = f.rsplit('.', 1) + if ext not in ('json', 'mp4', 'srt', 'vtt'): + print('skip', ext, f) + continue + if name not in public_names: + print('remove', f, name, ext) + os.unlink(f) + + mp4s = [f for f in os.listdir('edits') if f.endswith('.mp4')] + html = {} + for mp4 in sorted(mp4s): + prefix = mp4.split('_')[0] + if prefix not in html: + html[prefix] = [ + ''' + +

%s public edits

+ ''' % mp4.split('_')[1] + ] + title = ' '.join(mp4.replace('.mp4', '').split('_')[2:]) + info = mp4.replace('.mp4', '.json') + srt = mp4.replace('.mp4', '.srt') + date = datetime.fromtimestamp(os.path.getmtime('edits/' + mp4)) + date = date + timedelta(hours=2) + date = date.strftime('%Y-%m-%d %H:%M:%S') + line = F'{title} ({date})
mp4 - subtitles - play - metadata
\n' + html[prefix].append(line) + for prefix, data in html.items(): + data = '
'.join(data) + current = '' + filename = 'edits/%s.html' % prefix + if os.path.exists(filename): + with open(filename) as fd: + current = fd.read() + if data != current: + with open(filename, 'w') as fd: + fd.write(data) +