index.cgi - gopher-web-gateway - A simple gopher gateway for the web generating TXT or PDF output.
 (HTM) git clone git://bitreich.org/gopher-web-gateway git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/gopher-web-gateway
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       index.cgi (1995B)
       ---
            1 #!/usr/bin/env python
            2 # coding=utf-8
            3 #
            4 # © 2026 Christoph Lohmann <20h@r-36.net>
            5 #
            6 # This file is published under the terms of the GPLv3.
            7 #
            8 
            9 import os
           10 import sys
           11 import getopt
           12 import requests
           13 
           14 from selenium import webdriver
           15 from selenium.webdriver.chrome.options import Options as chromeoptions
           16 from selenium.webdriver.chrome.service import Service as chromeservice
           17 from selenium.webdriver.common.print_page_options import PrintOptions
           18 import base64
           19 
           20 def uri2pdf(uri):
           21         options = chromeoptions()
           22         chromearguments = [
           23                 "headless",
           24                 "no-sandbox",
           25                 "disable-extensions",
           26                 "disable-dev-shm-usage",
           27                 "start-maximized",
           28                 "window-size=1900,1080",
           29                 "disable-gpu"
           30         ]
           31         for carg in chromearguments:
           32                 options.add_argument(carg)
           33 
           34         service = chromeservice(executable_path="/usr/bin/chromedriver")
           35         driver = webdriver.Chrome(service=service, options=options)
           36         driver.implicitly_wait(2)
           37         driver.get(uri)
           38 
           39         print_options = PrintOptions()
           40         pdf = driver.print_page(print_options)
           41         driver.quit()
           42         return base64.b64decode(pdf)
           43 
           44 def main(args):
           45         if len(args) != 7:
           46                 sys.stderr.write("Must be run as geomyidae dcgi.\n")
           47                 return 1
           48         g_search = args[1]
           49         g_arguments = args[2]
           50         g_host = args[3]
           51         g_port = args[4]
           52         g_traversal = args[5]
           53         g_base = g_selector = args[6]
           54         if len(g_traversal) > 0:
           55                 g_base = g_selector[:-len(g_traversal)]
           56         g_usetls = os.getenv("GOPHERS", "off")
           57         if g_usetls == "on":
           58                 g_uri = "gophers://"
           59         else:
           60                 g_uri = "gopher://"
           61         g_uri += g_host
           62         if g_port != "70":
           63                 g_uri += ":" + g_port
           64         g_uri += g_base
           65 
           66         if len(g_search) > 0:
           67                 page = g_search
           68         else:
           69                 page = "/".join(g_traversal.split("/")[1:])
           70 
           71         if len(page) == 0:
           72                 sys.stdout.write("3No URI given\t\t\r\n")
           73                 sys.stdout.flush()
           74                 return 1
           75 
           76         pdf = uri2pdf(page)
           77         # TODO replace links to web gateway.
           78         #g_content = pdf.content.replace(mw_replace.encode("utf-8"),\
           79         #      g_uri.encode("utf-8"))
           80         g_content = pdf
           81         sys.stdout.buffer.write(g_content)
           82         sys.stdout.flush()
           83         return 0
           84 
           85 if __name__ == "__main__":
           86         sys.exit(main(sys.argv))
           87