45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
|
from django.views.generic import TemplateView, View
|
||
|
from django.http import HttpResponse
|
||
|
|
||
|
from urllib.request import urlopen
|
||
|
|
||
|
from .static import STATION_URLS, STATION_NAMES
|
||
|
|
||
|
|
||
|
class LiveStationView(TemplateView):
|
||
|
template_name = "frontend/station.html"
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super().get_context_data(**kwargs)
|
||
|
context["station"] = self.kwargs["station"]
|
||
|
return context
|
||
|
|
||
|
|
||
|
class StationProxyView(View):
|
||
|
def get_station(self):
|
||
|
return self.kwargs["station"]
|
||
|
|
||
|
def get_filename(self):
|
||
|
return self.kwargs["filename"]
|
||
|
|
||
|
def download_file(self):
|
||
|
url = STATION_URLS[self.get_station()] + self.get_filename()
|
||
|
return urlopen(url).read()
|
||
|
|
||
|
def get(self, request, *args, **kwargs):
|
||
|
response = self.download_file()
|
||
|
|
||
|
if self.get_filename().endswith(".m3u8"):
|
||
|
response = response.replace(b"skd", b"https")
|
||
|
|
||
|
return HttpResponse(response)
|
||
|
|
||
|
|
||
|
class IndexView(TemplateView):
|
||
|
template_name = "frontend/index.html"
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super().get_context_data(**kwargs)
|
||
|
context["stations"] = STATION_NAMES
|
||
|
return context
|