BDServer/modules/Create.py

106 lines
3 KiB
Python
Raw Normal View History

# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )
import os
import json
import time
from modules.Common import *
from modules import Set
def Create(name):
# Creates a base directory before publishing.
if not name.count("/") == 1:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Wrong Format!")
print('Use: $ python3 run.py --create articles/My_Article')
name = Simplify(name.split("/")[0])+"/"+Simplify(name.split("/")[1])
folder = Set.Folder()+"/editing"
try:
os.makedirs(folder+"/"+name)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Can't Create!", e)
# The text file
text = open(folder+"/"+name+"/text.md", "w")
text.close()
metadata = {
"title":name.split("/")[1],
"timestamp":time.time(),
"description":"",
"author":"",
"thumbnail":"",
"views":{
"amount":0,
"viewers":[],
"dates":{}
},
"recording":"",
"comments":{
"comments":[],
"requests":[]
}
}
with open(folder+"/"+name+"/metadata.json", "w") as save:
json.dump(metadata, save, indent=4)
print(clr["bold"]+clr["tbyl"]+name+clr["norm"]+" is created of editing.")
print(' To write text use: $ python3 run.py --write '+name)
print('To edit metadata use: $ python3 run.py --metadata '+name)
print(' To publish use: $ python3 run.py --publish '+name)
def Write(name):
if not name.count("/") == 1:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Wrong Format!")
print('Use: $ python3 run.py --write articles/My_Article')
name = Simplify(name.split("/")[0])+"/"+Simplify(name.split("/")[1])
folder = Set.Folder()+"/editing/"+name
os.system(Set.Load().get("editor", "nano")+" "+folder+"/text.md")
def Metadata(name):
if not name.count("/") == 1:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Wrong Format!")
print('Use: $ python3 run.py --metadata articles/My_Article')
name = Simplify(name.split("/")[0])+"/"+Simplify(name.split("/")[1])
folder = Set.Folder()+"/editing/"+name
os.system(Set.Load().get("editor", "nano")+" "+folder+"/metadata.json")
def Publish(name):
if not name.count("/") == 1:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Wrong Format!")
print('Use: $ python3 run.py --publish articles/My_Article')
name = Simplify(name.split("/")[0])+"/"+Simplify(name.split("/")[1])
folder = Set.Folder()
source = folder+"/editing/"+name
destination = folder+"/tabs/"+name
try:
os.makedirs(folder+"/tabs/"+name)
os.rename(source+"/text.md", destination+"/text.md")
os.rename(source+"/metadata.json", destination+"/metadata.json")
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Could not publish!", e)