"""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: # I wasn't clear about trailing slashes, maybe add one on? CURRENT_URL += "/" try: CURRENT_URL_INDEX = URLS.index(CURRENT_URL) except: CURRENT_URL = MAIN_PAGE # place the index somewhere random in the list # this should alleviate the issue of what happens when someone not in the list tries to link to the list CURRENT_URL_INDEX = random.randrange(len(URLS)) 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