phantasma/app/video/views.py

129 lines
4 KiB
Python
Raw Normal View History

2021-10-27 15:19:55 +00:00
import logging
2021-11-21 09:09:26 +00:00
import json
2022-01-27 14:42:48 +00:00
from collections import Counter
2021-10-27 15:19:55 +00:00
2021-09-28 13:10:22 +00:00
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
2021-09-30 18:33:47 +00:00
from django.conf import settings
2021-10-27 15:19:55 +00:00
from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
import ox.geo
2021-09-28 13:10:22 +00:00
from . import models
2021-10-27 15:19:55 +00:00
logger = logging.getLogger(__name__)
2021-11-24 11:45:15 +00:00
stream_prefix_cache = {}
2021-10-27 15:19:55 +00:00
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):
2021-11-24 11:45:15 +00:00
#domain = settings.DEFAULT_PANDORA_API.split('/')[2]
domain = 'njp.ma'
prefix = "https://media.v1.%s" % domain
2021-10-27 15:19:55 +00:00
cdn = {
2021-11-24 11:45:15 +00:00
'Eastern Asia': "https://media.v2.%s" % domain,
'Southern Asia': "https://media.v2.%s" % domain,
'Asia': "https://media.v2.%s" % domain,
2021-10-27 15:19:55 +00:00
}
ip = get_ip(request)
2021-11-24 11:45:15 +00:00
if ip in stream_prefix_cache:
return stream_prefix_cache[ip]
2021-10-27 15:19:55 +00:00
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:
2021-11-24 11:45:15 +00:00
stream_prefix_cache[ip] = cdn[location]
2021-10-27 15:19:55 +00:00
return cdn[location]
except:
logger.error('using default prefix, no geoip data found, run ./mange.py update_geoio', exc_info=True)
2021-11-24 11:45:15 +00:00
stream_prefix_cache[ip] = prefix
2021-10-27 15:19:55 +00:00
return prefix
2021-11-24 11:45:15 +00:00
stream_prefix_cache[ip] = prefix
2021-10-27 15:19:55 +00:00
return prefix
2021-09-28 13:10:22 +00:00
def films(request):
context = {}
2021-10-11 12:55:45 +00:00
context['films'] = models.Film.objects.filter(public=True).order_by('position', 'data__title')
2022-01-27 14:42:48 +00:00
types = []
for f in context['films']:
2022-01-28 16:08:31 +00:00
types += [t.capitalize() for t in f.data['type']]
2022-01-27 14:42:48 +00:00
types = Counter(types)
context['types'] = []
for t in sorted(types):
context['types'].append({
'title': '%s (%s)' % (t, types[t]), 'value': t
})
2021-11-24 11:45:15 +00:00
context['stream_prefix'] = get_stream_prefix(request)
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-09-28 13:10:22 +00:00
return render(request, 'films.html', context)
def film(request, slug):
context = {}
context['film'] = get_object_or_404(models.Film, slug=slug)
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-10-27 15:19:55 +00:00
context['stream_prefix'] = get_stream_prefix(request)
2021-11-21 09:09:26 +00:00
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
2021-09-28 13:10:22 +00:00
return render(request, 'film.html', context)
2021-11-21 09:09:26 +00:00
def film_play(request, slug, id):
2021-09-28 13:10:22 +00:00
context = {}
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-11-21 09:09:26 +00:00
context['config'] = json.dumps({
2021-11-21 09:13:02 +00:00
'layer': 'transcripts',
2021-11-21 09:09:26 +00:00
'item': id
})
2021-11-24 11:45:15 +00:00
context['stream_prefix'] = get_stream_prefix(request)
2021-11-21 09:09:26 +00:00
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
2021-09-28 13:10:22 +00:00
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)
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-09-28 13:10:22 +00:00
return render(request, 'edit.html', context)
def edit_play(request, slug, lang):
2021-09-28 13:10:22 +00:00
context = {}
context['edit'] = get_object_or_404(models.Edit, slug=slug)
context['lang'] = lang
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-09-28 13:10:22 +00:00
return render(request, 'edit_play.html', context)
def tv(request):
context = {}
2021-09-30 18:33:47 +00:00
context['settings'] = settings
2021-09-28 13:10:22 +00:00
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)