From 7b2630a817c8c3933f2bb3345a759641d537fc8e Mon Sep 17 00:00:00 2001 From: Matthew Evan Date: Sun, 3 Apr 2022 12:27:21 -0400 Subject: [PATCH 1/4] Config file addition --- .gitignore | 1 + modules/render.py | 7 +++++-- server.py | 24 ++++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 96138b6..862112c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ port.json +config.json *.pyc diff --git a/modules/render.py b/modules/render.py index bb71f4b..b56a10b 100644 --- a/modules/render.py +++ b/modules/render.py @@ -145,16 +145,19 @@ def suggestions(page, json): page = page + "" return page -def search_widget(page): +def search_widget(page, address): # Adds a search bar to the page page = page + """ -
+
""" + #page = page.format(ADDRESS) return page diff --git a/server.py b/server.py index ef457b6..c85b27b 100644 --- a/server.py +++ b/server.py @@ -18,11 +18,23 @@ from modules import render CSS = "https://zortazert.codeberg.page/style/styles.css" try: - with open("port.json","r") as f: - json_stuff = json.load(f) - PORT = json_stuff["port"] + if os.path.exists("config.json"): + with open("config.json","r") as f: + srvConfig = json.load(f) + ADDRESS = srvConfig["address"] + PORT = srvConfig["port"] + CSS = srvConfig["css"] except: - PORT = input("Port: ") + newConfig = open("config.json", "w") + newConfig.write("""{ + "INFO" : "You would be better off setting address to the full URL of the FreeCompetitors instance.", + "address" : "/", + "port" : "8080", + "css" : "https://zortazert.codeberg.page/style/styles.css" +}""") + newConfig.close + print("Please edit the configuration file \"config.json\".") + exit() try: PORT = int(PORT) except: @@ -73,7 +85,7 @@ class handler(BaseHTTPRequestHandler): # If the user is at the front page, let him get only the search bar page = "



Free Competitors

" - page = render.search_widget(page) + page = render.search_widget(page, ADDRESS) page = page + "

Please search for any software to which you would like to find a Free Software replacement.

" page = render.source_code_link(page) self.send(page) @@ -95,7 +107,7 @@ class handler(BaseHTTPRequestHandler): software_data, match = search.search_app(software) - page = render.search_widget("") + page = render.search_widget("", ADDRESS) page = page +"Search match: "+ str(int(match*100))+"%" # Let's add the found software to the page page = render.html(page, software_data) From cdd4f0a052f94a1f0e00b1ef9b989eccbabedfb3 Mon Sep 17 00:00:00 2001 From: Matthew Evan Date: Sun, 3 Apr 2022 12:31:38 -0400 Subject: [PATCH 2/4] Added config explaination --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fc4813..7f134ce 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The software is built on Python. So you will need to have python3 installed. The `python3 server.py` -It will prompt you to select a `PORT` to which on your end to bind the server. For now it binds it to your localhost. You can edit that part. We are using a standard SimpleHTTPServer python module for it. +On first run, you will be prompted to edit the newly created `config.json`. You should set the URL to be the URL in which you are hosting **Free Competitors** (ex. `http://5wxkwfl6n4abwjsqv5r6roebwbc7fnc22dq3clpooojzdmdnzzo3amad.onion/`). If you do not wish to set a URL, you should set it to `/`. Then, you should specify the port to serve on, and the CSS to use. Please tell us if you start an instance. We want to add it's link here. From 9c87b660b86059a6fd64db44b42a18a846d6bd95 Mon Sep 17 00:00:00 2001 From: jyamihud Date: Sun, 3 Apr 2022 20:35:04 +0300 Subject: [PATCH 3/4] Fix the PR from earlier. --- server.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index c85b27b..1cc6415 100644 --- a/server.py +++ b/server.py @@ -18,28 +18,24 @@ from modules import render CSS = "https://zortazert.codeberg.page/style/styles.css" try: - if os.path.exists("config.json"): - with open("config.json","r") as f: - srvConfig = json.load(f) - ADDRESS = srvConfig["address"] - PORT = srvConfig["port"] - CSS = srvConfig["css"] + + with open("config.json","r") as f: + srvConfig = json.load(f) + ADDRESS = srvConfig["address"] + PORT = srvConfig["port"] + CSS = srvConfig["css"] except: newConfig = open("config.json", "w") newConfig.write("""{ "INFO" : "You would be better off setting address to the full URL of the FreeCompetitors instance.", "address" : "/", - "port" : "8080", + "port" : 8080, "css" : "https://zortazert.codeberg.page/style/styles.css" }""") newConfig.close print("Please edit the configuration file \"config.json\".") exit() -try: - PORT = int(PORT) -except: - print("Port should be a number") - exit() + # Who fucking made this http.server so I need to use classes? # I fucking hate who ever thought that it was a good idea... From 20e43552a47b3b5b4c0536ace1421d4723b28726 Mon Sep 17 00:00:00 2001 From: jyamihud Date: Sun, 3 Apr 2022 21:04:47 +0300 Subject: [PATCH 4/4] Manually merging MattMadness' PR --- server.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/server.py b/server.py index 1cc6415..1b0cb69 100644 --- a/server.py +++ b/server.py @@ -1,3 +1,5 @@ +#!/usr/bin/python3 + # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT # THE FOLLOWING SOURCE CODE I UNDER THE GNU # AGPL LICENSE V3 OR ANY LATER VERSION. @@ -15,16 +17,50 @@ import os from modules import search from modules import render -CSS = "https://zortazert.codeberg.page/style/styles.css" +# Author: https://www.delftstack.com/howto/python/python-print-colored-text/ +class bcolors: + OK = '\033[92m' #GREEN + WARNING = '\033[93m' #YELLOW + FAIL = '\033[91m' #RED + RESET = '\033[0m' #RESET COLOR -try: +print("""╔═╗┬─┐┌─┐┌─┐╔═╗┌─┐┌┬┐┌─┐┌─┐┌┬┐┬┌┬┐┌─┐┬─┐┌─┐ +╠╣ ├┬┘├┤ ├┤ ║ │ ││││├─┘├┤ │ │ │ │ │├┬┘└─┐ +╚ ┴└─└─┘└─┘╚═╝└─┘┴ ┴┴ └─┘ ┴ ┴ ┴ └─┘┴└─└─┘ +Copyright (C) 2022 Jeison Yehuda Amihud + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +""") + +print("Loading config...") +if os.path.exists("config.json"): with open("config.json","r") as f: srvConfig = json.load(f) ADDRESS = srvConfig["address"] PORT = srvConfig["port"] CSS = srvConfig["css"] -except: + print(bcolors.OK + "Loaded Configuration: " + bcolors.RESET) + print("Address: "+ ADDRESS) + print("Port: "+ str(PORT)) # Do not think people are smart at syntax + print("CSS: "+ CSS) + try: + PORT = int(PORT) + except: + print(bcolors.FAIL + "ERR: Port should be a number" + bcolors.RESET) + exit() +else: newConfig = open("config.json", "w") newConfig.write("""{ "INFO" : "You would be better off setting address to the full URL of the FreeCompetitors instance.", @@ -33,10 +69,9 @@ except: "css" : "https://zortazert.codeberg.page/style/styles.css" }""") newConfig.close - print("Please edit the configuration file \"config.json\".") + print(bcolors.WARNING + "Please edit the configuration file \"config.json\"." + bcolors.RESET) exit() - # Who fucking made this http.server so I need to use classes? # I fucking hate who ever thought that it was a good idea... class handler(BaseHTTPRequestHandler): @@ -113,4 +148,5 @@ class handler(BaseHTTPRequestHandler): self.send(page) serve = HTTPServer(("", PORT), handler) +print(bcolors.OK + "⚡Now serving on port "+ str(PORT) +" at "+ ADDRESS +"." + bcolors.RESET) serve.serve_forever()