add render_public_edits

This commit is contained in:
j 2024-04-18 11:17:30 +02:00
parent 4dcf945a62
commit f96815c175
3 changed files with 142 additions and 0 deletions

0
edits/index.html Normal file
View File

25
edits/player.html Normal file
View File

@ -0,0 +1,25 @@
<body></body>
<script>
var mp4 = document.location.hash.substr(1)
var vtt = mp4.replace(/.mp4/, '.vtt')
var video = document.createElement('video')
video.src = mp4
video.controls = true
var track = document.createElement('track')
track.label = 'English'
track.kind = 'subtitles'
track.srclang = 'en'
track.default = true
track.src = vtt
video.appendChild(track)
document.body.appendChild(video)
document.body.style.background = 'black'
document.body.style.textAlign = 'center'
document.body.style.display = 'flex'
document.body.style.height = '100vh'
document.body.style.overflow = 'hidden'
document.body.style.margin = '0'
document.body.style.padding = '0'
video.style.width = '100%'
video.style.height = '100%'
</script>

117
render_public_edits.py Executable file
View File

@ -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] = [
'''
<style>
.light {
color: #666;
}
</style>
<h1>%s public edits</h1>
''' % 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} <span class="light">({date})</span><br><a href="{quote(mp4)}">mp4</a> - <a href="{quote(srt)}">subtitles</a> - <a href="player.html#{quote(mp4)}">play</a> - <a href="{quote(info)}">metadata</a><br>\n'
html[prefix].append(line)
for prefix, data in html.items():
data = '<br>'.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)