diff --git a/.gitignore b/.gitignore index 85dbdb3..2b24304 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ venv *.swo secret.txt app/local_settings.py +geo/GeoLite2-City.mmdb diff --git a/app/settings.py b/app/settings.py index c2c6edd..a39ea9c 100755 --- a/app/settings.py +++ b/app/settings.py @@ -143,7 +143,7 @@ STATICFILES_FINDERS = [ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -TIMELINE_PREFIX = "https://aab21.pad.ma/" +GEOIP_PATH = BASE_DIR / 'geo' DEFAULT_PANDORA_API = "https://pad.ma/api/" diff --git a/app/static/js/film.js b/app/static/js/film.js index de873d4..ce33cd7 100644 --- a/app/static/js/film.js +++ b/app/static/js/film.js @@ -30,7 +30,8 @@ document.querySelector('a#play-fullscreen').addEventListener('click', event => { video = document.createElement('video') video.classList.add('player') video.classList.add('fullscreen') - setVideoSrc(video, 'https://pad.ma/' + film.id + '/480p.webm') + //setVideoSrc(video, 'https://pad.ma/' + film.id + '/480p.webm') + video.src = `${film.prefix}/stream/${film.id}.mp4` video.controls = true document.querySelector('main').appendChild(video) video.style.display = 'none' @@ -78,6 +79,6 @@ document.querySelector('a#play-fullscreen').addEventListener('click', event => { video.remove() video = null }); - } + } } }) diff --git a/app/templates/film.html b/app/templates/film.html index 8991711..00f25cc 100755 --- a/app/templates/film.html +++ b/app/templates/film.html @@ -33,16 +33,16 @@ body {
- + +

{{ film.data.summary|safe }}

@@ -86,6 +86,7 @@ body { {% block end %} {% endblock %} diff --git a/app/video/management/commands/update_geoip.py b/app/video/management/commands/update_geoip.py new file mode 100644 index 0000000..df78a29 --- /dev/null +++ b/app/video/management/commands/update_geoip.py @@ -0,0 +1,29 @@ +import os +import re + +import ox + +from django.core.management.base import BaseCommand +from django.conf import settings + + +class Command(BaseCommand): + """ + """ + help = 'update geoip database' + args = '' + + def handle(self, **options): + force = False + path = settings.GEOIP_PATH / 'GeoLite2-City.mmdb' + index = ox.net.read_url('https://db-ip.com/db/download/ip-to-city-lite').decode() + match = re.compile('href=[\'"](http.*.mmdb.gz)').findall(index) + if match: + url = match[0] + print('download', url) + ox.net.save_url(url, "%s.gz" % path) + if os.path.exists(path): + os.unlink(path) + os.system('gunzip "%s.gz"' % path) + else: + print('failed to download GeoLite2-City.mmdb') diff --git a/app/video/views.py b/app/video/views.py index ce0068e..7bb7c6e 100644 --- a/app/video/views.py +++ b/app/video/views.py @@ -1,9 +1,48 @@ +import logging + from django.shortcuts import render, redirect, get_object_or_404 from django.views.decorators.csrf import csrf_exempt from django.conf import settings +from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception +import ox.geo from . import models +logger = logging.getLogger(__name__) + +def get_ip(request): + if 'HTTP_X_FORWARDED_FOR' in request.META: + ip = request.META['HTTP_X_FORWARDED_FOR'].split(',')[0] + else: + ip = request.META['REMOTE_ADDR'] + if ip.startswith('::ffff:'): + ip = ip[len('::ffff:'):] + return ip + +def get_stream_prefix(request): + prefix = "https://v2.phantas.ma/" + cdn = { + 'Eastern Asia': "https://v1.phantas.ma/", + 'Southern Asia': "https://v1.phantas.ma/", + 'Asia': "https://v1.phantas.ma/", + } + ip = get_ip(request) + try: + g = GeoIP2() + country = g.country(ip) + if country: + country = ox.get_country_name(country['country_code']) + info = ox.geo.get_country(country) + for key in ('name', 'region', 'continent'): + location = info.get(key) + #print(location) + if location in cdn: + return cdn[location] + except: + logger.error('using default prefix, no geoip data found, run ./mange.py update_geoio', exc_info=True) + return prefix + return prefix + def films(request): context = {} context['films'] = models.Film.objects.filter(public=True).order_by('position', 'data__title') @@ -14,6 +53,7 @@ def film(request, slug): context = {} context['film'] = get_object_or_404(models.Film, slug=slug) context['settings'] = settings + context['stream_prefix'] = get_stream_prefix(request) return render(request, 'film.html', context) def film_play(request, slug, lang): diff --git a/geo b/geo new file mode 120000 index 0000000..a0abaf7 --- /dev/null +++ b/geo @@ -0,0 +1 @@ +/srv/pandora/data/geo/ \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index eb04912..15c1aab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ Django libsass django-compressor django-sass-processor +geoip2 ox lxml