123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #!/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.
- # 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
- # 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
- print("""╔═╗┬─┐┌─┐┌─┐╔═╗┌─┐┌┬┐┌─┐┌─┐┌┬┐┬┌┬┐┌─┐┬─┐┌─┐
- ╠╣ ├┬┘├┤ ├┤ ║ │ ││││├─┘├┤ │ │ │ │ │├┬┘└─┐
- ╚ ┴└─└─┘└─┘╚═╝└─┘┴ ┴┴ └─┘ ┴ ┴ ┴ └─┘┴└─└─┘
- Copyright (C) 2022 Jeison Yehuda Amihud and contributors.
- 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 <http://www.gnu.org/licenses/>.
- """)
- 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"]
- 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.",
- "address" : "/",
- "port" : 8080,
- "css" : "/css"
- }""")
- newConfig.close
- 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):
- def start_page(self):
- self.send_response(200)
- if "/json/" in self.path:
- self.send_header('Content-type', 'application/json')
- elif self.path == "/css":
- self.send_header('Content-type', 'text/css')
- elif self.path == "/ttf":
- self.send_header('Content-type', 'font/ttf')
- else:
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- def send(self, textin):
- textin = str(textin)
- csstext = '<link media="all" href="'+CSS+'" type="text/css" rel="stylesheet" />'
- text = "<!-- Welcome to Free Competitors Page Source!!!--> \n\n"
- text = text + "<!-- Let's add some CSS, you can edit 'config.json' to change it. -->\n"
- text = text + '<head>'+csstext+'</head>\n\n'
- text = text + "<!-- Now the body. The main part of the page, so to speak. -->\n"
- text = text + '<body>\n\n'+textin+'\n\n</body>'
-
-
- self.start_page()
- self.wfile.write(text.encode("utf-8"))
-
- def do_GET(self):
-
- if self.path.startswith("/json/"):
- # 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 == "/":
- # If the user is at the front page, let him get only the search bar
- page = "<center><br><br><br><h1>Free Competitors</h1>"
- page = render.search_widget(page, ADDRESS)
- page = page + "<p>Please search for any software to which you would like to find a Free Software replacement.</p></center>"
- page = render.source_code_link(page)
- self.send(page)
- elif self.path == "/css":
- # The css file
- cssfile = open("default.css")
- cssfile = cssfile.read()
- self.send(cssfile)
-
- elif self.path == "/font":
- # The font file
- fontfile = open("OpenSans-ExtraBold.ttf", "rb")
- fontfile = fontfile.read()
- self.send(fontfile)
- elif self.path in ["/faq", "/faq?"]:
- # The font file
- faqfile = open("faq.html")
- faqfile = faqfile.read()
- faqfile = render.search_widget(faqfile, ADDRESS)
- faqfile = render.source_code_link(faqfile)
- self.send(faqfile)
-
-
-
- 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("+", " ")
- software_data, match = search.search_app(software)
-
- 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)
- page = render.suggestions(page, software_data)
- page = render.source_code_link(page)
-
- self.send(page)
-
- serve = HTTPServer(("", PORT), handler)
- print(bcolors.OK + "⚡Now serving on port "+ str(PORT) +" at "+ ADDRESS +"." + bcolors.RESET)
- serve.serve_forever()
|