molniya/molniya.py

42 lines
1.0 KiB
Python

"""The utility library for the Molniya CGI pages.
Handles loading the orbit, as well as next/previous/random links."""
import json, random, os, urllib.parse
# stolen from AV-98
urllib.parse.uses_relative.append("gemini")
urllib.parse.uses_netloc.append("gemini")
# The URL of the main page of the orbit
MAIN_PAGE = "gemini://tilde.team/~khuxkm/leo/"
URLS = [MAIN_PAGE]
try:
with open("orbit.json") as f:
URLS = json.load(f)["urls"]
except:
pass
CURRENT_URL = urllib.parse.unquote(os.environ.get("QUERY_STRING",MAIN_PAGE))
CURRENT_URL_PARSED = urllib.parse.urlparse(CURRENT_URL)
try:
CURRENT_URL_INDEX = URLS.index(CURRENT_URL)
except:
CURRENT_URL = MAIN_PAGE
try:
CURRENT_URL_INDEX = URLS.index(CURRENT_URL)
except:
# give up and just start at 0
CURRENT_URL_INDEX = 0
def next_url():
return URLS[(CURRENT_URL_INDEX+1)%len(URLS)]
def prev_url():
return URLS[(CURRENT_URL_INDEX-1)%len(URLS)]
def rand_url():
ret = random.choice(URLS)
while ret==CURRENT_URL or ret==MAIN_PAGE:
ret = random.choice(URLS)
return ret