server.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
  2. # THE FOLLOWING SOURCE CODE I UNDER THE GNU
  3. # AGPL LICENSE V3 OR ANY LATER VERSION.
  4. # This project is not for simple users, but for
  5. # web-masters and a like, so we are counting on
  6. # your ability to set it up and running.
  7. # Server side
  8. from http.server import BaseHTTPRequestHandler, HTTPServer
  9. from subprocess import *
  10. import json
  11. import os
  12. from modules import search
  13. from modules import render
  14. CSS = "https://zortazert.codeberg.page/style/styles.css"
  15. try:
  16. with open("port.json","r") as f:
  17. json_stuff = json.load(f)
  18. PORT = json_stuff["port"]
  19. except:
  20. PORT = input("Port: ")
  21. try:
  22. PORT = int(PORT)
  23. except:
  24. print("Port should be a number")
  25. exit()
  26. # Who fucking made this http.server so I need to use classes?
  27. # I fucking hate who ever thought that it was a good idea...
  28. class handler(BaseHTTPRequestHandler):
  29. def start_page(self):
  30. self.send_response(200)
  31. if "/json/" not in self.path:
  32. self.send_header('Content-type', 'text/html')
  33. else:
  34. self.send_header('Content-type', 'application/json')
  35. self.end_headers()
  36. def send(self, text):
  37. text = str(text)
  38. csstext = '<link media="all" href="'+CSS+'" type="text/css" rel="stylesheet" />'
  39. text = '<head>'+csstext+'</head><body>'+text+'</body>'
  40. self.start_page()
  41. self.wfile.write(text.encode("utf-8"))
  42. def do_GET(self):
  43. if self.path.startswith("/json/"):
  44. # API CALL
  45. term = self.path[6:]
  46. software_data, match = search.search_app(term)
  47. data = {"found":{"data":software_data,"match":match}}
  48. data["suggestions"] = search.suggest(software_data)
  49. text = json.dumps(data, indent = 2)
  50. self.start_page()
  51. self.wfile.write(text.encode("utf-8"))
  52. elif self.path == "/":
  53. # If the user is at the front page, let him get only the search bar
  54. page = "<center><br><br><br><h1>Free Competitors</h1>"
  55. page = render.search_widget(page)
  56. page = page + "<p>Please search for any software to which you would like to find a Free Software replacement.</p></center>"
  57. page = render.source_code_link(page)
  58. self.send(page)
  59. elif "/search?item=" in self.path:
  60. # Clearing the url
  61. # instead of it being /search?item=softwarename
  62. # it will be just /softwarename
  63. item = self.path[self.path.find("item=")+5:].lower()
  64. self.send('<meta http-equiv="Refresh" content="0; url=\'/'+item+'\'" />')
  65. else:
  66. # This is when the fun is
  67. software = self.path.replace("/", "").replace("+", " ")
  68. software_data, match = search.search_app(software)
  69. page = render.search_widget("")
  70. page = page +"Search match: "+ str(int(match*100))+"%"
  71. # Let's add the found software to the page
  72. page = render.html(page, software_data)
  73. page = render.suggestions(page, software_data)
  74. page = render.source_code_link(page)
  75. self.send(page)
  76. serve = HTTPServer(("", PORT), handler)
  77. serve.serve_forever()