server.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # THIS SOFTWARE IS A PART OF FASTLBRY PROJECT
  2. # THIS IS A SERVER SOFTWARE FOR FASTLBRY HTML
  3. # THE FOLLOWING SOURCE CODE I UNDER THE GNU
  4. # AGPL LICENSE V3 OR ANY LATER VERSION.
  5. # This project is not for simple users, but for
  6. # web-masters and a like, so we are counting on
  7. # your ability to install an LBRY SDK by your
  8. # self.
  9. SDK = "/home/vcs/Software/FastLBRY-GTK/flbry/lbrynet"
  10. CSS = "https://zortazert.codeberg.page/style/styles.css"
  11. PORT = 8026
  12. # Server side
  13. from http.server import BaseHTTPRequestHandler, HTTPServer
  14. from subprocess import *
  15. import json
  16. import os
  17. from flbry import markdown
  18. # Who fucking made this http.server so I need to use classes?
  19. # I fucking hate who ever thought that it was a good idea...
  20. class handler(BaseHTTPRequestHandler):
  21. def start_page(self):
  22. self.send_response(200)
  23. self.send_header('Content-type', 'text/html')
  24. self.end_headers()
  25. def send(self, text):
  26. text = str(text)
  27. csstext = '<link media="all" href="'+CSS+'" type="text/css" rel="stylesheet" />'
  28. text = '<head>'+csstext+'</head><body>'+text+'</body>'
  29. self.start_page()
  30. self.wfile.write(text.encode("utf-8"))
  31. def do_GET(self):
  32. print(self.path)
  33. url = self.path[1:]
  34. out = check_output([SDK,
  35. "resolve", url])
  36. # Now we want to parse the json
  37. try:
  38. out = json.loads(out)
  39. except:
  40. self.send("Connect to LBRY first.")
  41. return
  42. out = out[url]
  43. # If article in md
  44. if out.get("value", {}).get("source", {}).get("media_type") == "text/markdown":
  45. print("MARKDOWN DETECTED")
  46. # Then we want to tell the SDK to start downloading.
  47. playout = check_output([SDK,
  48. "get", url])
  49. # Parsing the Json
  50. playout = json.loads(playout)
  51. # Present the article to the user.
  52. self.send(markdown.convert(playout['download_path']))
  53. serve = HTTPServer(("", PORT), handler)
  54. serve.serve_forever()