use cdn
This commit is contained in:
parent
9ffb421ba3
commit
6f7053ed03
8 changed files with 81 additions and 7 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue