njpma/app/video/views.py
2021-11-21 10:13:02 +01:00

111 lines
3.5 KiB
Python

import logging
import json
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):
return settings.DEFAULT_PANDORA_API.replace('/api/', '/')
'''
domain = settings.DEFAULT_PANDORA_API.split('/')[3]
prefix = "https://v2.%s/" % domain
cdn = {
'Eastern Asia': "https://v1.%s/" % domain,
'Southern Asia': "https://v1.%s/" % domain,
'Asia': "https://v1.%s/" % domain,
}
'''
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')
context['settings'] = settings
return render(request, 'films.html', context)
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)
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'film.html', context)
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['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'film_play.html', context)
def edits(request):
context = {}
context['edits'] = models.Edit.objects.filter(public=True).order_by('created')
return render(request, 'edits.html', context)
def edit(request, slug):
context = {}
context['edit'] = get_object_or_404(models.Edit, slug=slug)
context['settings'] = settings
return render(request, 'edit.html', context)
def edit_play(request, slug, lang):
context = {}
context['edit'] = get_object_or_404(models.Edit, slug=slug)
context['lang'] = lang
context['settings'] = settings
return render(request, 'edit_play.html', context)
def tv(request):
context = {}
context['settings'] = settings
return render(request, 'tv.html', context)
@csrf_exempt
def pandoraAPI(request):
import ox
from .utils import render_to_json_response
import json
data = json.loads(request.body.decode())
print('pandora request', data)
api = ox.api.signin('https://pad.ma/api/')
data = getattr(api, data['action'])(**data['data'])
print('response', data)
return render_to_json_response(data)