index.dcgi - mediawiki-gopher - mediawiki gopher frontend
(HTM) git clone git://bitreich.org/mediawiki-gopher git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/mediawiki-gopher
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Tags
(DIR) README
(DIR) LICENSE
---
index.dcgi (2626B)
---
1 #!/usr/bin/env python
2 # coding=utf-8
3 #
4 # © 2025-2-26 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 def main(args):
15 if len(args) != 7:
16 sys.stderr.write("Must be run as geomyidae dcgi.\n")
17 return 1
18 g_search = args[1]
19 g_arguments = args[2]
20 g_host = args[3]
21 g_port = args[4]
22 g_traversal = args[5]
23 g_base = g_selector = args[6]
24 if len(g_traversal) > 0:
25 g_base = g_selector[:-len(g_traversal)]
26
27 mw_base = os.getenv("MEDIAWIKI_BASE", "https://en.wikipedia.org")
28 mw_rest_api = os.getenv("MEDIAWIKI_REST", "https://en.wikipedia.org/w/rest.php")
29 mw_search = "%s/v1/search/page" % (mw_rest_api)
30 mw_page = "%s/v1/page" % (mw_rest_api)
31
32 # Mandatory for wikipedia.
33 headers = { "User-Agent": "gopher mediawiki frontend/0.1" }
34
35 def name2gph(name):
36 return name.replace("|", "\\|")
37
38 def search2gph(search, mount="txt"):
39 gph = ""
40 itgph = "0"
41 if mount == "pdf":
42 itgph = "9"
43 if "errorKey" in search:
44 gph += "[3|%s||server|port]\n" % \
45 (name2gph(search["messageTranslations"]["en"]))
46 return gph
47 for r in search["pages"]:
48 title = r["title"]
49 if r["description"] != None:
50 title = "%s - %s" % (title, r["description"])
51 gph += "[%s|%s|%s/%s/%s.%s|server|port]\n" % \
52 (itgph, name2gph(title), g_base, mount,\
53 r["key"], mount)
54 return gph
55
56 def index2gph():
57 gph = "= %s =\n" % (mw_base)
58 gph += "[0|main page|%s/txt/Main_Page.txt|server|port]\n" % (g_base)
59 gph += "[7|txt output search|%s/search|server|port]\n" % (g_base)
60 gph += "[7|pdf output search|%s/pdfsearch|server|port]\n" % (g_base)
61 return gph
62
63 cmds = g_traversal.split("/")
64 if len(cmds) == 0:
65 gph = index2gph()
66 elif len(cmds) == 1:
67 gph = index2gph()
68 elif cmds[1] == "":
69 gph = index2gph()
70 elif cmds[1] == "wiki":
71 page = ""
72 if len(cmds) > 2:
73 page = cmds[2]
74 else:
75 gph = index2gph()
76 if page != "":
77 html = requests.get("%s/%s/html" % (mw_page, \
78 page), headers=headers)
79 gph = html.text
80 elif cmds[1] == "search" or cmds[1] == "pdfsearch":
81 params = {"limit": 5}
82 if len(cmds) > 2:
83 params = {"q": cmds[2]}
84 elif len(g_search) > 0:
85 params = {"q": g_search}
86 else:
87 gph = "[3|query missing||serverr|port]\n"
88 if "q" in params:
89 search = requests.get(mw_search, headers=headers,\
90 params=params).json()
91 if cmds[1] == "pdfsearch":
92 gph = search2gph(search, "pdf")
93 else:
94 gph = search2gph(search)
95 else:
96 gph = "[3|Not found.||server|port]\n"
97
98 sys.stdout.write(gph)
99 sys.stdout.flush()
100
101 return 0
102
103 if __name__ == "__main__":
104 sys.exit(main(sys.argv))
105