tunic-tracker-redux/tunictracker/tracker/views.py

45 lines
1.3 KiB
Python

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from json import loads
from math import floor
import requests
# Create your views here.
def index(request):
request_data = None
try:
request_data = requests.get('http://localhost:8000/spoiler').text
except:
with open('empty_spoiler.json', 'r') as t:
try:
tracker_output = loads(t.read())
except:
return
try:
tracker_output = loads(request_data)
except:
with open('empty_spoiler.json', 'r') as t:
try:
tracker_output = loads(t.read())
except:
return
tracker_debug = tracker_output["Debug"]
tracker_totals = tracker_output["Totals"]
tracker_current_scene = tracker_output["Current"]["Scene"]
tracker_current_scane_data = tracker_output["Scenes"][tracker_current_scene]
tracker_scenes = tracker_output["Scenes"]
template = loader.get_template("tracker/index.html")
context = {
"debug": tracker_debug,
"totals": tracker_totals,
"scenes": tracker_scenes,
"current_scene": {
"title": tracker_current_scene,
"data": tracker_current_scane_data
}
}
return HttpResponse(template.render(context, request))