2022-03-29 14:43:19 +02:00
|
|
|
# THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
|
|
|
|
# THE FOLLOWING SOURCE CODE I UNDER THE GNU
|
|
|
|
# AGPL LICENSE V3 OR ANY LATER VERSION.
|
|
|
|
|
|
|
|
# This project is not for simple users, but for
|
|
|
|
# web-masters and a like, so we are counting on
|
|
|
|
# your ability to set it up and running.
|
|
|
|
|
|
|
|
# Server side
|
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
from subprocess import *
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
from modules import search
|
|
|
|
from modules import render
|
|
|
|
|
2022-03-31 06:05:05 +02:00
|
|
|
CSS = "https://zortazert.codeberg.page/style/styles.css"
|
2022-03-31 19:16:16 +02:00
|
|
|
|
2022-03-31 06:05:05 +02:00
|
|
|
try:
|
|
|
|
with open("port.json","r") as f:
|
|
|
|
json_stuff = json.load(f)
|
|
|
|
PORT = json_stuff["port"]
|
|
|
|
except:
|
|
|
|
PORT = input("Port: ")
|
|
|
|
try:
|
|
|
|
PORT = int(PORT)
|
|
|
|
except:
|
|
|
|
print("Port should be a number")
|
|
|
|
exit()
|
|
|
|
|
2022-03-29 14:43:19 +02:00
|
|
|
# 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):
|
|
|
|
|
|
|
|
def start_page(self):
|
|
|
|
self.send_response(200)
|
2022-04-02 09:43:52 +02:00
|
|
|
if "/json/" not in self.path:
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
else:
|
|
|
|
self.send_header('Content-type', 'application/json')
|
2022-03-29 14:43:19 +02:00
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def send(self, text):
|
|
|
|
text = str(text)
|
|
|
|
csstext = '<link media="all" href="'+CSS+'" type="text/css" rel="stylesheet" />'
|
|
|
|
text = '<head>'+csstext+'</head><body>'+text+'</body>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.start_page()
|
|
|
|
self.wfile.write(text.encode("utf-8"))
|
|
|
|
|
|
|
|
def do_GET(self):
|
2022-03-30 19:52:14 +02:00
|
|
|
|
2022-04-02 09:43:52 +02:00
|
|
|
if self.path.startswith("/json/"):
|
2022-03-29 14:43:19 +02:00
|
|
|
|
2022-04-02 09:43:52 +02:00
|
|
|
# API CALL
|
|
|
|
|
|
|
|
|
|
|
|
term = self.path[6:]
|
|
|
|
software_data, match = search.search_app(term)
|
|
|
|
data = {"found":{"data":software_data,"match":match}}
|
|
|
|
data["suggestions"] = search.suggest(software_data)
|
|
|
|
text = json.dumps(data, indent = 2)
|
|
|
|
|
|
|
|
self.start_page()
|
|
|
|
self.wfile.write(text.encode("utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
elif self.path == "/":
|
2022-03-29 14:43:19 +02:00
|
|
|
|
|
|
|
# If the user is at the front page, let him get only the search bar
|
2022-03-30 18:11:55 +02:00
|
|
|
|
2022-04-01 21:59:32 +02:00
|
|
|
page = "<center><br><br><br><h1>Free Competitors</h1>"
|
2022-03-30 18:11:55 +02:00
|
|
|
page = render.search_widget(page)
|
2022-04-01 21:59:32 +02:00
|
|
|
page = page + "<p>Please search for any software to which you would like to find a Free Software replacement.</p></center>"
|
2022-03-30 18:11:55 +02:00
|
|
|
page = render.source_code_link(page)
|
|
|
|
self.send(page)
|
2022-03-29 14:43:19 +02:00
|
|
|
|
|
|
|
elif "/search?item=" in self.path:
|
|
|
|
|
|
|
|
# Clearing the url
|
|
|
|
# instead of it being /search?item=softwarename
|
|
|
|
# it will be just /softwarename
|
|
|
|
|
|
|
|
item = self.path[self.path.find("item=")+5:].lower()
|
|
|
|
self.send('<meta http-equiv="Refresh" content="0; url=\'/'+item+'\'" />')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# This is when the fun is
|
|
|
|
|
|
|
|
software = self.path.replace("/", "").replace("+", " ")
|
|
|
|
|
2022-04-01 21:29:15 +02:00
|
|
|
software_data, match = search.search_app(software)
|
|
|
|
|
|
|
|
page = render.search_widget("")
|
2022-04-01 21:59:32 +02:00
|
|
|
page = page +"Search match: "+ str(int(match*100))+"%"
|
2022-03-29 14:43:19 +02:00
|
|
|
# Let's add the found software to the page
|
|
|
|
page = render.html(page, software_data)
|
|
|
|
page = render.suggestions(page, software_data)
|
2022-03-30 18:11:55 +02:00
|
|
|
page = render.source_code_link(page)
|
2022-03-29 14:43:19 +02:00
|
|
|
|
|
|
|
self.send(page)
|
|
|
|
|
|
|
|
serve = HTTPServer(("", PORT), handler)
|
|
|
|
serve.serve_forever()
|