This commit is contained in:
j 2021-10-27 16:19:55 +01:00
commit 6f7053ed03
8 changed files with 81 additions and 7 deletions

View file

@ -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')

View file

@ -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):