Starts to render the main page. No functionality yet.
This commit is contained in:
parent
4c3bc1329e
commit
a202e74d7a
7 changed files with 267 additions and 21 deletions
|
@ -3,9 +3,6 @@ This file could be used under both CC-BY-SA 4.0 or at your chosing GNU GPLv3 or
|
||||||
*/
|
*/
|
||||||
|
|
||||||
html {background-color: #353849;
|
html {background-color: #353849;
|
||||||
background-image: url("/pictures/new_pattern.jpg");
|
|
||||||
background-size: cover;
|
|
||||||
background-attachment: fixed;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,3 +61,44 @@ def Simplify(text):
|
||||||
ntext = ntext + "_"
|
ntext = ntext + "_"
|
||||||
|
|
||||||
return ntext
|
return ntext
|
||||||
|
|
||||||
|
def RandString(n=50):
|
||||||
|
# this will make a random string
|
||||||
|
s = ""
|
||||||
|
good = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890"
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
s = s + random.choice(good)
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
IDColors = {}
|
||||||
|
|
||||||
|
def consoleForm(obj):
|
||||||
|
|
||||||
|
# Function that prints a pretty object info about
|
||||||
|
|
||||||
|
ID = obj
|
||||||
|
NAME = obj[-10:]
|
||||||
|
|
||||||
|
c = IDcolor(ID)
|
||||||
|
|
||||||
|
return c + " " + NAME + " " + clr["norm"]
|
||||||
|
|
||||||
|
def IDcolor(ID):
|
||||||
|
|
||||||
|
# Coloring
|
||||||
|
if ID not in IDColors:
|
||||||
|
r = random.random()
|
||||||
|
g = random.random()
|
||||||
|
b = random.random()
|
||||||
|
|
||||||
|
ct = clr["tbwh"]
|
||||||
|
if max(r, g, b ) > 0.9 or sum((r, g, b))/3 > 0.5:
|
||||||
|
ct = clr["tdbl"]
|
||||||
|
|
||||||
|
c = clr["bold"] + "\033[48;2;"+str(int(r*256))+";"+str(int(g*256))+";"+str(int(b*256))+"m"+ct
|
||||||
|
IDColors[ID] = c
|
||||||
|
c = IDColors[ID]
|
||||||
|
return c
|
||||||
|
|
|
@ -4,19 +4,30 @@
|
||||||
from modules.Common import *
|
from modules.Common import *
|
||||||
|
|
||||||
def Help():
|
def Help():
|
||||||
print(clr["bold"]+" BDServer Help Page"+clr["norm"])
|
|
||||||
|
print()
|
||||||
|
print(clr["bold"]+" BDServer Help"+clr["norm"])
|
||||||
|
print()
|
||||||
|
print(clr["tdyl"]+"--help"+clr["norm"]+" - Prints this Help Page.")
|
||||||
|
print(clr["tdyl"]+"--run"+clr["norm"]+" - Run the server.")
|
||||||
|
print(clr["tdyl"]+"--set"+clr["norm"]+" - Use for changing settings.")
|
||||||
|
print(clr["tdyl"]+"--config"+clr["norm"]+" - Edit config file directly, bypassing --set.")
|
||||||
|
print(clr["tdyl"]+"--folder"+clr["norm"]+" - Open the folder of the website's data.")
|
||||||
print()
|
print()
|
||||||
print(clr["tdyl"]+"--help"+clr["norm"]+" - Prints this Help Page.")
|
|
||||||
print(clr["tdyl"]+"--run"+clr["norm"]+ " - Run the server.")
|
|
||||||
print(clr["tdyl"]+"--set"+clr["norm"]+ " - Use for changing settings.")
|
|
||||||
|
|
||||||
def Set():
|
def Set():
|
||||||
|
|
||||||
print(clr["bold"]+" BDServer --set Help Page"+clr["norm"])
|
|
||||||
print()
|
print()
|
||||||
print(" --set is used for setting up server configurations.")
|
print(clr["bold"]+" BDServer Set Help "+clr["norm"])
|
||||||
|
print()
|
||||||
|
print(" Set is used for setting up server configurations.")
|
||||||
print()
|
print()
|
||||||
print(clr["tdyl"]+"--title"+clr["norm"]+" - Set title of the website.")
|
print(clr["tdyl"]+"--title"+clr["norm"]+" - Set title of the website.")
|
||||||
|
print(clr["tdyl"]+"--tagline"+clr["norm"]+" - Set tagline of the website.")
|
||||||
print(clr["tdyl"]+"--port"+clr["norm"]+" - Set port where to run the website.")
|
print(clr["tdyl"]+"--port"+clr["norm"]+" - Set port where to run the website.")
|
||||||
|
print(clr["tdyl"]+"--css"+clr["norm"]+" - Set a CSS file.")
|
||||||
|
print(clr["tdyl"]+"--css_edit"+clr["norm"]+" - edit a CSS file.")
|
||||||
|
print(clr["tdyl"]+"--favicon"+clr["norm"]+" - Set a favicon file.")
|
||||||
print(clr["tdyl"]+"--add_tab"+clr["norm"]+" - Adds a category of articles.")
|
print(clr["tdyl"]+"--add_tab"+clr["norm"]+" - Adds a category of articles.")
|
||||||
print(clr["tdyl"]+"--edit_tab"+clr["norm"]+" - Edit the config of a category.")
|
print(clr["tdyl"]+"--edit_tab"+clr["norm"]+" - Edit the config of a category.")
|
||||||
|
print()
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
# AGPL 3 or any later version
|
# AGPL 3 or any later version
|
||||||
# (C) J.Y.Amihud ( Blender Dumbass )
|
# (C) J.Y.Amihud ( Blender Dumbass )
|
||||||
|
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
from modules import Set
|
from modules import Set
|
||||||
|
from modules.Common import *
|
||||||
|
|
||||||
def guess_type(path):
|
def guess_type(path):
|
||||||
|
|
||||||
|
@ -18,17 +22,26 @@ def guess_type(path):
|
||||||
|
|
||||||
def headers(server, code):
|
def headers(server, code):
|
||||||
|
|
||||||
|
# Basic cookie for logins to work
|
||||||
|
cookie = False
|
||||||
|
if "Cookie" not in str(server.headers):
|
||||||
|
cookie = True
|
||||||
|
|
||||||
server.send_response(code)
|
server.send_response(code)
|
||||||
server.send_header("Content-type", guess_type(server.path))
|
server.send_header("Content-type", guess_type(server.path))
|
||||||
|
|
||||||
|
if cookie:
|
||||||
|
server.send_header("Set-Cookie", RandString())
|
||||||
|
|
||||||
server.end_headers()
|
server.end_headers()
|
||||||
|
|
||||||
def head(title="", description="", image="", config={}):
|
def head(title="", description="", image="", config={}):
|
||||||
|
|
||||||
if image.startswith("/"): image = config.get("url","")+image
|
if image.startswith("/"): image = config.get("url","")+image
|
||||||
|
|
||||||
html = """
|
favicon = config.get("favicon", "/icon/internet")
|
||||||
<!DOCTYPE html>
|
|
||||||
|
|
||||||
|
html = """
|
||||||
<!-- The head. Part of the HTML code where metadata about the page is storred.
|
<!-- The head. Part of the HTML code where metadata about the page is storred.
|
||||||
Including the metadata readable by social media websites, when generating link
|
Including the metadata readable by social media websites, when generating link
|
||||||
previwews, which are called mata-tags in HTML. -->
|
previwews, which are called mata-tags in HTML. -->
|
||||||
|
@ -37,7 +50,7 @@ def head(title="", description="", image="", config={}):
|
||||||
|
|
||||||
<title>"""+title+"""</title> <!-- Title in the browser tab -->
|
<title>"""+title+"""</title> <!-- Title in the browser tab -->
|
||||||
<link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link -->
|
<link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link -->
|
||||||
<link rel="icon" href="/pictures/favicon.png"> <!-- Tiny image in the tab -->
|
<link rel="icon" href=\""""+favicon+""""> <!-- Tiny image in the tab -->
|
||||||
|
|
||||||
<!-- Now meta tags for social media -->
|
<!-- Now meta tags for social media -->
|
||||||
|
|
||||||
|
@ -59,6 +72,21 @@ def send(server, html, code):
|
||||||
headers(server, code)
|
headers(server, code)
|
||||||
server.wfile.write(html.encode("utf-8"))
|
server.wfile.write(html.encode("utf-8"))
|
||||||
|
|
||||||
|
def tabs():
|
||||||
|
|
||||||
|
folder = Set.Folder()+"/tabs"
|
||||||
|
tabs = {}
|
||||||
|
for tab in sorted(list(os.walk(folder))[0][1]):
|
||||||
|
try:
|
||||||
|
with open(folder+"/"+tab+"/config.json") as o:
|
||||||
|
data = json.load(o)
|
||||||
|
tabs[tab] = data
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
pass
|
||||||
|
|
||||||
|
return tabs
|
||||||
|
|
||||||
|
|
||||||
def MainPage(server):
|
def MainPage(server):
|
||||||
|
|
||||||
|
@ -72,9 +100,42 @@ def MainPage(server):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
html = html + "hello world"
|
html = html + """
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
<center>
|
||||||
|
<img src=\""""+config.get("favicon", "icon/internet")+"""" alt="[LOGO]" style="height:150px;vertical-align: middle">
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<h2>"""+config.get("title", "My Website")+"""</h2>
|
||||||
|
"""+config.get("tagline", "")+"""
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Tabs = tabs()
|
||||||
|
for tab in Tabs:
|
||||||
|
|
||||||
|
html = html + Button(Tabs[tab].get("title", tab),
|
||||||
|
"/"+tab,
|
||||||
|
Tabs[tab].get("icon", "folder"))
|
||||||
|
|
||||||
|
|
||||||
send(server, html, 200)
|
send(server, html, 200)
|
||||||
|
|
||||||
|
def Button(text, link, icon=""):
|
||||||
|
|
||||||
|
html = """
|
||||||
|
<a class="button" href=\""""+link+"""">"""
|
||||||
|
if icon:
|
||||||
|
html = html + """
|
||||||
|
<img src="/icon/"""+icon+"""" style="vertical-align: middle">"""
|
||||||
|
html = html + """
|
||||||
|
"""+text+"""</a>
|
||||||
|
|
||||||
|
"""
|
||||||
|
return html
|
||||||
|
|
||||||
|
|
|
@ -20,12 +20,38 @@ from modules.Common import *
|
||||||
|
|
||||||
class handler(BaseHTTPRequestHandler):
|
class handler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
|
def log_message(self, format, *args):
|
||||||
|
|
||||||
|
if "?" in self.path:
|
||||||
|
self.path = self.path[:self.path.find("?")]
|
||||||
|
|
||||||
|
q = ""
|
||||||
|
for i in self.parsed:
|
||||||
|
if i != "password":
|
||||||
|
q = q + "[ "+i+" : "+self.parsed[i][0]+" ] "
|
||||||
|
else:
|
||||||
|
q = q + "[ "+i+" : ******* ] "
|
||||||
|
|
||||||
|
toprint = consoleForm(self.cookie)+" "+"[ "+str(datetime.datetime.now())+" ] "+self.path+" "+q
|
||||||
|
print(toprint)
|
||||||
|
|
||||||
|
folder = Set.Folder()
|
||||||
|
filename = folder+"/logs/"+datetime.datetime.now().strftime('%Y-%m-%d')+".log"
|
||||||
|
logfile = open(filename, "ab")
|
||||||
|
logfile.write((toprint+"\n").encode('utf-8'))
|
||||||
|
logfile.close()
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
|
|
||||||
self.path = self.path.replace("/..", "/")
|
self.path = self.path.replace("/..", "/")
|
||||||
self.path = self.path.replace("%27", "'")
|
self.path = self.path.replace("%27", "'")
|
||||||
|
|
||||||
if self.path == "/":
|
parsed_url = urllib.parse.urlparse(self.path)
|
||||||
|
|
||||||
|
self.parsed = urllib.parse.parse_qs(parsed_url.query)
|
||||||
|
self.cookie = self.headers.get("Cookie")
|
||||||
|
|
||||||
|
if self.path[:self.path.find("?")] == "/" or self.path == "/":
|
||||||
Render.MainPage(self)
|
Render.MainPage(self)
|
||||||
|
|
||||||
elif self.path.startswith("/pictures/"):
|
elif self.path.startswith("/pictures/"):
|
||||||
|
@ -38,11 +64,24 @@ class handler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
elif self.path == "/css":
|
elif self.path == "/css":
|
||||||
|
|
||||||
cssfile = open("default.css", "rb")
|
config = Set.Load()
|
||||||
|
filename = config.get("css", "default.css")
|
||||||
|
cssfile = open(filename, "rb")
|
||||||
cssfile = cssfile.read()
|
cssfile = cssfile.read()
|
||||||
Render.headers(self, 200)
|
Render.headers(self, 200)
|
||||||
self.wfile.write(cssfile)
|
self.wfile.write(cssfile)
|
||||||
|
|
||||||
|
elif self.path.startswith("/icon/"):
|
||||||
|
|
||||||
|
folder = "icons"
|
||||||
|
icon = folder+"/"+self.path[6:]+".png"
|
||||||
|
f = open(icon, "rb")
|
||||||
|
f = f.read()
|
||||||
|
Render.headers(self, 200)
|
||||||
|
self.wfile.write(f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
config = Set.Load()
|
config = Set.Load()
|
||||||
PORT = config.get("port", 8080)
|
PORT = config.get("port", 8080)
|
||||||
serve = HTTPServer(("", PORT), handler)
|
serve = HTTPServer(("", PORT), handler)
|
||||||
|
|
|
@ -21,6 +21,13 @@ def Folder():
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Adding nessesary subcategories
|
||||||
|
for i in ["pictures", "tabs", "logs"]:
|
||||||
|
try:
|
||||||
|
os.makedirs(data_dir+"/"+i)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return data_dir
|
return data_dir
|
||||||
|
|
||||||
def Load():
|
def Load():
|
||||||
|
@ -57,6 +64,17 @@ def Set():
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Title Wasn't Specified!")
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Title Wasn't Specified!")
|
||||||
|
print('Use: $ python3 run.py --set --title "My Website"')
|
||||||
|
|
||||||
|
if "--tagline" in sys.argv:
|
||||||
|
try:
|
||||||
|
tagline = sys.argv[ sys.argv.index("--tagline") + 1]
|
||||||
|
if "--" in tagline: 1/0 # Failing this for the error message.
|
||||||
|
Tagline(tagline)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tagline Wasn't Specified!")
|
||||||
|
print('Use: $ python3 run.py --set --title "Ultimate Club for Ultimate Hackers"')
|
||||||
|
|
||||||
if "--port" in sys.argv:
|
if "--port" in sys.argv:
|
||||||
try:
|
try:
|
||||||
|
@ -64,8 +82,8 @@ def Set():
|
||||||
Port(port)
|
Port(port)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Port Wasn't Specified Correctly!", e)
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Port Wasn't Specified Correctly!")
|
||||||
|
print('Use: $ python3 run.py --set --port 8080')
|
||||||
|
|
||||||
if "--add_tab" in sys.argv:
|
if "--add_tab" in sys.argv:
|
||||||
try:
|
try:
|
||||||
|
@ -75,6 +93,7 @@ def Set():
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
|
||||||
|
print('Use: $ python3 run.py --set --add_tab "Articles"')
|
||||||
|
|
||||||
if "--edit_tab" in sys.argv:
|
if "--edit_tab" in sys.argv:
|
||||||
try:
|
try:
|
||||||
|
@ -84,6 +103,31 @@ def Set():
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
|
||||||
|
print('Use: $ python3 run.py --set --edit_tab "Articles"')
|
||||||
|
|
||||||
|
if "--css" in sys.argv:
|
||||||
|
try:
|
||||||
|
filename = sys.argv[ sys.argv.index("--css") + 1]
|
||||||
|
if "--" in filename: 1/0 # Failing this for the error message.
|
||||||
|
SetCSS(filename)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" CSS File Wasn't Specified!", e)
|
||||||
|
print('Use: $ python3 run.py --set --css ~/mycssfile.css')
|
||||||
|
|
||||||
|
if "--favicon" in sys.argv:
|
||||||
|
try:
|
||||||
|
filename = sys.argv[ sys.argv.index("--favicon") + 1]
|
||||||
|
if "--" in filename: 1/0 # Failing this for the error message.
|
||||||
|
SetFavicon(filename)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" File Wasn't Specified!", e)
|
||||||
|
print('Use: $ python3 run.py --set --css ~/favicon.png')
|
||||||
|
|
||||||
|
if "--css_edit" in sys.argv:
|
||||||
|
config = Load()
|
||||||
|
os.system("nano "+config.get("css", "default.css"))
|
||||||
|
|
||||||
def Title(title):
|
def Title(title):
|
||||||
|
|
||||||
|
@ -94,6 +138,15 @@ def Title(title):
|
||||||
|
|
||||||
print(clr["bold"]+clr["tbyl"]+title+clr["norm"]+" is set as title.")
|
print(clr["bold"]+clr["tbyl"]+title+clr["norm"]+" is set as title.")
|
||||||
|
|
||||||
|
def Tagline(tagline):
|
||||||
|
|
||||||
|
data = Load()
|
||||||
|
data["tagline"] = tagline
|
||||||
|
|
||||||
|
Save(data)
|
||||||
|
|
||||||
|
print(clr["bold"]+clr["tbyl"]+tagline+clr["norm"]+" is set as tagline.")
|
||||||
|
|
||||||
def Port(port):
|
def Port(port):
|
||||||
|
|
||||||
data = Load()
|
data = Load()
|
||||||
|
@ -131,7 +184,40 @@ def EditTab(tab):
|
||||||
tabsFolder = Folder()+"/tabs/"+Simplify(tab)
|
tabsFolder = Folder()+"/tabs/"+Simplify(tab)
|
||||||
os.system("nano "+tabsFolder+"/config.json")
|
os.system("nano "+tabsFolder+"/config.json")
|
||||||
|
|
||||||
|
def SetCSS(filename):
|
||||||
|
|
||||||
|
try:
|
||||||
|
f = open(filename, "r")
|
||||||
|
f = f.read()
|
||||||
|
except Exception as e:
|
||||||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read CSS file!", e)
|
||||||
|
return
|
||||||
|
|
||||||
|
css = open(Folder()+"/style.css", "w")
|
||||||
|
css.write(f)
|
||||||
|
css.close()
|
||||||
|
|
||||||
|
config = Load()
|
||||||
|
config["css"] = Folder()+"/style.css"
|
||||||
|
Save(config)
|
||||||
|
|
||||||
|
print("New CSS is set at "+clr["bold"]+Folder()+"/style.css"+clr["norm"])
|
||||||
|
|
||||||
|
def SetFavicon(filename):
|
||||||
|
|
||||||
|
try:
|
||||||
|
f = open(filename, "rb")
|
||||||
|
f = f.read()
|
||||||
|
except Exception as e:
|
||||||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read Image!", e)
|
||||||
|
return
|
||||||
|
|
||||||
|
css = open(Folder()+"/pictures/favicon.png", "wb")
|
||||||
|
css.write(f)
|
||||||
|
css.close()
|
||||||
|
|
||||||
|
config = Load()
|
||||||
|
config["favicon"] = "/pictures/favicon.png"
|
||||||
|
Save(config)
|
||||||
|
|
||||||
|
print("New favicon is set at "+clr["bold"]+Folder()+"/pictures/favicon.png"+clr["norm"])
|
||||||
|
|
11
run.py
11
run.py
|
@ -2,6 +2,7 @@
|
||||||
# (C) J.Y.Amihud ( Blender Dumbass )
|
# (C) J.Y.Amihud ( Blender Dumbass )
|
||||||
|
|
||||||
# Basic sys to get through to different modules
|
# Basic sys to get through to different modules
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
if len(sys.argv) < 2 or "--help" in sys.argv:
|
if len(sys.argv) < 2 or "--help" in sys.argv:
|
||||||
from modules import Help
|
from modules import Help
|
||||||
|
@ -11,5 +12,15 @@ elif "--set" in sys.argv:
|
||||||
from modules import Set
|
from modules import Set
|
||||||
Set.Set()
|
Set.Set()
|
||||||
|
|
||||||
|
elif "--config" in sys.argv:
|
||||||
|
from modules import Set
|
||||||
|
data = Set.Load() # Making sure
|
||||||
|
Set.Save(data) # that config.json exists
|
||||||
|
os.system("nano "+Set.Folder()+"/config.json")
|
||||||
|
|
||||||
elif "--run" in sys.argv:
|
elif "--run" in sys.argv:
|
||||||
from modules import Run
|
from modules import Run
|
||||||
|
|
||||||
|
elif "--folder" in sys.argv:
|
||||||
|
from modules import Set
|
||||||
|
os.system("xdg-open "+Set.Folder())
|
||||||
|
|
Loading…
Add table
Reference in a new issue