use streaming proxy

This commit is contained in:
j 2021-11-24 12:45:15 +01:00
parent 16a9439152
commit c230b57dca
9 changed files with 34 additions and 25 deletions

View file

@ -211,13 +211,13 @@ function renderAnnotation(config, video, ascroll, clip) {
}
var values = []
clip.annotations.forEach(a => {
values.push(a.value.replace(/src="\//g, `src="${pandoraURL}/`).replace(/href="\//g, `href="${pandoraURL}/`))
values.push(a.value.replace(/src="\//g, `src="${streamPrefix}/`).replace(/href="\//g, `href="${pandoraURL}/`))
})
values = values.join('<br><br>')
div.innerHTML = `
<div class="frame">
<figure>
<img src="${pandoraURL}/${annotation.id.split('/')[0]}/${imageResolution}p${annotation.in}.jpg">
<img src="${streamPrefix}/${annotation.id.split('/')[0]}/${imageResolution}p${annotation.in}.jpg">
${figcaption}
</figure>
</div>
@ -228,7 +228,7 @@ function renderAnnotation(config, video, ascroll, clip) {
document.addEventListener('scroll', onVisibilityChange(div.querySelector('.frame'), function(visible) {
var src
if (config.edit) {
src = `${pandoraURL}/${annotation.id.split('/')[0]}/480p.webm`
src = `${streamPrefix}/${annotation.id.split('/')[0]}/480p.webm`
}
if (config.loaded && visible) {
updatePlayer(video, frame, annotation['in'], annotation['out'], src)
@ -248,7 +248,7 @@ function renderAnnotations(config) {
video.WebKitPlaysInline = true
video.muted = true
if (config.item) {
setVideoSrc(video, `${pandoraURL}/${config.item}/480p.webm`)
setVideoSrc(video, `${streamPrefix}/${config.item}/480p.webm`)
}
video.addEventListener('timeupdate', timeupdate)
video.addEventListener('touchstart', showOverlay)
@ -268,7 +268,7 @@ function renderAnnotations(config) {
if (frame) {
var src
if (config.edit) {
src = `${pandoraURL}/${config.first.id.split('/')[0]}/480p.webm`
src = `${streamPrefix}/${config.first.id.split('/')[0]}/480p.webm`
}
updatePlayer(video, frame, config.first['in'], config.first['out'], src)
}

View file

@ -200,8 +200,8 @@ function renderPlayer(config) {
video.muted = false
video.controls = true
if (config.item) {
setVideoSrc(video, `${pandoraURL}/${config.item}/480p.webm`)
video.poster = `${pandoraURL}/${config.item}/480p.jpg`
setVideoSrc(video, `${streamPrefix}/${config.item}/480p.webm`)
video.poster = `${streamPrefix}/${config.item}/480p.jpg`
}
video.addEventListener('timeupdate', timeupdate)
var box = document.createElement('div')

View file

@ -59,11 +59,11 @@ body {
</div>
<div class="video-block">
<img src="https://archive.njp.ma/{{ film.data.items.0.id }}/timelineslitscan64pframe.jpg" class="video-fallback-block">
<img src="{{ stream_prefix }}/{{ film.data.items.0.id }}/timelineslitscan64pframe.jpg" class="video-fallback-block">
<video
id="timeline-video"
src="https://archive.njp.ma/{{ film.data.items.0.id }}/timelineslitscan64p.mp4"
poster="https://archive.njp.ma/{{ film.data.items.0.id }}/timelineslitscan64pframe.jpg"
src="{{ stream_prefix }}/{{ film.data.items.0.id }}/timelineslitscan64p.mp4"
poster="{{ stream_prefix }}/{{ film.data.items.0.id }}/timelineslitscan64pframe.jpg"
controlsList="nodownload"
autoplay
loop
@ -82,7 +82,7 @@ body {
<div class="overlay">
<img src="{% static 'svg/play.svg' %}">
</div>
<img src="https://archive.njp.ma/{{ item.id }}/480p.jpg">
<img src="{{ stream_prefix }}/{{ item.id }}/480p.jpg">
<figcaption>
{{ item.title | safe }}
{% if item.date %}({{item.date | safe }}){% endif %}

View file

@ -6,6 +6,7 @@
<script>
var config = {{ config | safe }};
var pandoraURL = "{{ pandora_url }}";
var streamPrefix = "{{ stream_prefix }}";
</script>
<script src="{% static 'js/api.js' %}"></script>
<script src="{% static 'js/play.js' %}"></script>

View file

@ -30,11 +30,11 @@
<figure>
<div>
{% for item in film.data.items %}
<img src="https://archive.njp.ma/{{ item.id }}/480p.jpg">
<img src="{{ stream_prefix }}/{{ item.id }}/480p.jpg">
{% endfor %}
</div>
<figcaption>
<img src="https://archive.njp.ma/{{ film.data.items.0.id }}/timeline64p.jpg">
<img src="{{ stream_prefix }}/{{ film.data.items.0.id }}/timeline64p.jpg">
</figcaption>
<figcaption>
{{ film.data.title | safe }}

View file

@ -22,11 +22,11 @@
<div class="index">
{% if featured %}
<div class="video-block">
<img src="https://archive.njp.ma/{{ featured }}/timelineslitscan64pframe.jpg" class="video-fallback-block">
<img src="{{ stream_prefix }}{{ featured }}/timelineslitscan64pframe.jpg" class="video-fallback-block">
<video
id="timeline-video"
src="https://archive.njp.ma/{{ featured }}/timelineslitscan64p.mp4"
poster="https://archive.njp.ma/{{ featured }}/timelineslitscan64pframe.jpg"
src="{{ stream_prefix }}{{ featured }}/timelineslitscan64p.mp4"
poster="{{ stream_prefix }}{{ featured }}/timelineslitscan64pframe.jpg"
controlsList="nodownload"
autoplay
loop

View file

@ -38,6 +38,7 @@
<script>
var config = {{ text.json | safe }};
var pandoraURL = "{{ pandora_url }}";
var streamPrefix = "{{ stream_prefix }}";
</script>
<script src="/static/js/api.js?2021112223"></script>
{% if text.data.view == 'player' %}

View file

@ -18,6 +18,7 @@ def index(request):
context = {}
context['films'] = Film.objects.filter(public=True).count()
context['texts'] = Text.objects.filter(public=True).count()
context['stream_prefix'] = get_stream_prefix(request)
featured = Film.objects.filter(featured=True)
featured_count = featured.count()
if featured_count:

View file

@ -11,6 +11,8 @@ from . import models
logger = logging.getLogger(__name__)
stream_prefix_cache = {}
def get_ip(request):
if 'HTTP_X_FORWARDED_FOR' in request.META:
ip = request.META['HTTP_X_FORWARDED_FOR'].split(',')[0]
@ -21,17 +23,17 @@ def get_ip(request):
return ip
def get_stream_prefix(request):
return settings.DEFAULT_PANDORA_API.replace('/api/', '/')
'''
domain = settings.DEFAULT_PANDORA_API.split('/')[3]
prefix = "https://v2.%s/" % domain
#domain = settings.DEFAULT_PANDORA_API.split('/')[2]
domain = 'njp.ma'
prefix = "https://media.v1.%s" % domain
cdn = {
'Eastern Asia': "https://v1.%s/" % domain,
'Southern Asia': "https://v1.%s/" % domain,
'Asia': "https://v1.%s/" % domain,
'Eastern Asia': "https://media.v2.%s" % domain,
'Southern Asia': "https://media.v2.%s" % domain,
'Asia': "https://media.v2.%s" % domain,
}
'''
ip = get_ip(request)
if ip in stream_prefix_cache:
return stream_prefix_cache[ip]
try:
g = GeoIP2()
country = g.country(ip)
@ -42,15 +44,19 @@ def get_stream_prefix(request):
location = info.get(key)
#print(location)
if location in cdn:
stream_prefix_cache[ip] = cdn[location]
return cdn[location]
except:
logger.error('using default prefix, no geoip data found, run ./mange.py update_geoio', exc_info=True)
stream_prefix_cache[ip] = prefix
return prefix
stream_prefix_cache[ip] = prefix
return prefix
def films(request):
context = {}
context['films'] = models.Film.objects.filter(public=True).order_by('position', 'data__title')
context['stream_prefix'] = get_stream_prefix(request)
context['settings'] = settings
return render(request, 'films.html', context)
@ -66,10 +72,10 @@ def film_play(request, slug, id):
context = {}
context['settings'] = settings
context['config'] = json.dumps({
'stream_prefix': get_stream_prefix(request),
'layer': 'transcripts',
'item': id
})
context['stream_prefix'] = get_stream_prefix(request)
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'film_play.html', context)