Added add and search functionality. Lost my soul.

This commit is contained in:
Michael Rodin 2022-11-16 09:12:37 +01:00
parent aecf237d65
commit 5d67424b6e
4 changed files with 267 additions and 22 deletions

4
.gitignore vendored
View file

@ -1,7 +1,9 @@
/**
!/.gitignore
#!/default
!/func.py
!/index.db
#!/index.db
#!/Animals
!/main.py
!/vars.py

199
func.py
View file

@ -13,7 +13,7 @@ class database():
self.connection = sql.connect(filepath)
self.crsr = self.connection.cursor()
def add_index(self,vallist):
print("Parent method!")
# compile the options into a command for the SQLite database
colstring=",".join(self.collist)
valstring="'{}'".format("','".join(vallist))
self.crsr.execute("""INSERT INTO {table} ({cols})
@ -22,6 +22,7 @@ class database():
self.connection.commit()
def create_database(self, filepath):
# create the database and tables
self.connection = sql.connect(filepath)
self.crsr = self.connection.cursor()
sqlcommand = """CREATE TABLE FILES(
@ -39,11 +40,41 @@ class database():
); """
self.crsr.execute(sqlcommand)'''
def get_col(self,column = "*"):
# get the column of some table. If no options given, return all columns
self.crsr.execute("SELECT {} FROM {}".format(column,self.name))
res=self.crsr.fetchall()
tres=self.crsr.fetchall()
res=[]
for i in tres:
res.append(i)
# if the table is empty, return ".".
#print(column,"res ",res)
if not res:
res="."
return res
def get_item(self,column,where = "."):
if column == "*":
for col in self.collist:
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,col,where))
else:
self.crsr.execute("SELECT * FROM {} WHERE {} GLOB '*{}*'".format(self.name,column,where))
tres=self.crsr.fetchall()
#print("Tres: ",tres)
n=0
res=[]
for i in tres:
m=0
for j in tres:
if i in j and n!=m:
continue
else:
res.append(i)
m+=1
n+=1
#print("RES: ",tres)
# if the table is empty, return ".".
if not res:
return ["."]
return res
class metatable(database):
def __init__(self,filepath = os.getcwd() + "/index.db"):
self.name="META"
@ -57,38 +88,180 @@ class filestable(database):
self.name="FILES"
self.collist=["FILE","HASH","TITLE","SOURCE","CATEGORY","TAGS","CONTENT"]
super().__init__(filepath)
def add_index(self,filepath,category="default",title="",source="",tags="",content=""):
filehash=self.get_hash(filepath)
for i in self.get_col("HASH"):
print(i[0])
if filehash in self.get_col("HASH")[0]:
def add_index(self,filepath,category,title="",source="",tags="",content=""):
filehash=self.get_hash(filepath) # make of hash of file before copy
n=0 # TODO: show the duplicate file (Title, Category and filename)
category="default" if not category else category
if filehash in self.get_col("HASH"):
print("This file already exists!")
return
filetype=os.path.splitext(filepath)[1]
filename=str(uuid4()) + filetype
if not os.path.exists("{}/{}".format(ROOT_DIR,category)):
os.makedirs("{}/{}".format(ROOT_DIR,category))
# try to copy the file, return if error.
try:
shutil.copy(filepath,"{}/{}/{}".format(ROOT_DIR,category,filename))
except Exception as e:
print(e)
print("COULDN'T COPY FILE TO DESTINATION!")
return
print("Executing")
vallist=[filename,filehash,title,source,category,tags,content]
super().add_index(vallist)
def sql_compare_list(self,typ,firstlist,secondlist): # TODO: Fix (fixed?)
if firstlist:
n=0
temp_list=[]
if not secondlist:
n=0
for i in self.get_col(typ):
#print(i)
success=0
for j in firstlist:
#print(j)
if j in str(i):
#print("Yay")
if not success == -1:
success=1
else:
success=-1
if success > 0:
#print("Hey",i[0])
if typ == "*":
temp_list.append(self.get_item("FILE",i[0])[0])
else:
#print("TE",n,self.get_item(typ,i[0])[1])
for j in self.get_item(typ,i[0]):
temp_list.append(i)
#print("Self: ",self.get_item(typ,i[0])[n])
print(temp_list)
n+=1
else:
#print("Second: ",secondlist)
if not secondlist[0] == ".":
for i in secondlist:
for j in firstlist:
if j in i:
temp_list.append(secondlist[n])
n+=1
else:
#print("secondlist")
return secondlist
print("Temp_list: ",temp_list)
if not temp_list:
return ["."]
return temp_list
return secondlist
def get_hash(self,filepath):
#https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
md5_hash = hashlib.md5()
# hash selected file in chunks of 4KiB, read the link above if you ask why.
with open(filepath,"rb") as f:
# Read and update hash in chunks of 4K
for byte_block in iter(lambda: f.read(4096),b""):
md5_hash.update(byte_block)
f.close()
return str(md5_hash.hexdigest())
def search_index(self,*args):
for arg in args:
if not re.match('^[-]\w{1}$', arg) == None:
print("Heureka!")
print(args)
def search_index(self,arglist):
#print(arglist)
####
## WARNING!!!!!! UGLY CODE INCOMING!!!!!!
####
snext="all"
shash=[]
alle=[]
category=[]
sfile=[]
content=[]
source=[]
title=[]
tags=[]
for arg in arglist:
if re.match('^[-]\w{1}$', arg):
if arg == "-h":
snext="hash"
elif arg == "-a": # technically unneeded because of else at the end
snext="all"
elif arg == "-c":
snext="category"
elif arg == "-f":
snext="file"
elif arg == "-i": # Inhalt
snext="content"
elif arg == "-s":
snext="source"
elif arg == "-t":
snext="title"
elif arg == "-g": # Gruppe
snext="tags"
else:
snext="all"
continue
if snext == "hash":
shash.append(arg)
elif snext == "all":
alle.append(arg)
elif snext == "category":
category.append(arg)
elif snext == "file":
sfile.append(arg)
elif snext == "content":
content.append(arg)
elif snext == "source":
source.append(arg)
elif snext == "title":
title.append(arg)
elif snext == "tags":
tags.append(arg)
else:
alle.append(arg)
# search for the right items
selection=[]
selection=self.sql_compare_list("*",alle,selection)
selection=self.sql_compare_list("HASH",shash,selection)
selection=self.sql_compare_list("CATEGORY",category,selection)
selection=self.sql_compare_list("FILE",sfile,selection)
selection=self.sql_compare_list("CONTENT",content,selection)
selection=self.sql_compare_list("SOURCE",source,selection)
selection=self.sql_compare_list("TITLE",title,selection)
selection=self.sql_compare_list("TAGS",tags,selection)
#print("Selection: ",selection)
if selection:
res=[]
if len(selection) > 1:
n=0
#print(selection)
for tup in selection:
temp_list=[]
for j in tup:
temp_list.append(j)
print("Match [{}]".format(n))
print("\tTitle:\t ",temp_list[2])
print("\tCategory:",temp_list[4])
print("\tTags:\t ",temp_list[5])
n+=1
eingabe=input("Enter number 0-{}: ".format(n-1))
if int(eingabe) < n:
for i in selection[int(eingabe)]:
res.append(i)
print("\n\nFinal match:")
else:
print(type(eingabe))
# TODO: Fix
else:
if selection[0] == ".":
print("No matching entry found!")
return ["."]
tempres=selection[0]
for i in tempres:
res.append(i)
print("\nMatch found!")
print("Title:\t ",res[2])
print("Category:",res[4])
print("Filename:",res[0])
print("Tags:\t ",res[5])
return res
return 1

82
main.py
View file

@ -1,16 +1,86 @@
#!/bin/python3
import sys,os
import sys,os,re
os.chdir("ii-py")
from func import *
from vars import *
def add():
args=[]
for i in ["Filepath","Category","Title","Source","Tags","Content"]:
eingabe = input("Enter {}: ".format(i))
if i in ["Filepath"] and not eingabe:
print("{} has to not be empty!".format(i))
return 1
if i in ["Category"] and not eingabe:
print("{} set to 'default'".format(i))
eingabe="default"
args.append(eingabe)
print(args)
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
def delete():
df="Hi"
def help():
print("SYNTAX:\n\t'image-index' displays this text")
print("\t'image-index <option> [args]' executes the command")
print("\nOPTIONS:\n\thelp:\tdisplays this prompt")
print("\tadd:\tadds a new entry;\n\t\tInstant: image-index add <filepath> <category> <title> <source> <tags> <content>")
print("\t\tPrompt: image-index add")
print("\tsearch:\tsearches through the index (enter prompt for options);\n\t\tInstant: image-index search <words/filter>")
print("\t\tPrompt: image-index search")
print("\tdelete:\tdeletes an entry based on a search query;\n\t\tInstant: image-index delete <words>")
print("\t\tPrompt: image-index delete")
input("Press return...")
def search():
print("Separate the items with spaces ( )")
print("FILTER: -a: All types")
print("\t -c: Category")
print("\t -f: Filename")
print("\t -g: Tags")
print("\t -h: Hash")
print("\t -i: Content")
print("\t -s: Source")
print("\t -t: Title")
query=input("Query: ")
tb.search_index(query.split(' '))
def main():
if len(sys.argv) > 1:
for i in sys.argv:
print("Arg:" + i)
if len(sys.argv) <= 1:
help()
exit()
else:
command=sys.argv[1]
args=[]
for i in sys.argv[2:]:
args.append(i)
if re.match('[aA].*',command):
if len(sys.argv) == 2:
add()
exit()
elif len(sys.argv) >= 8:
tb.add_index(args[0],args[1],args[2],args[3],args[4],args[5])
elif re.match('[dD].*',command):
if len(sys.argv) == 2:
delete()
exit()
tb.del_index()
elif re.match('[sS].*',command):
if len(sys.argv) == 2:
search()
exit()
tb.search_index(args)
else:
print("No such option!")
exit()
#for i in sys.argv:
# print("Arg:" + i)
exit()
#tb.add_index("/home/marcel/Downloads/froggy.jpg","Tiere","Fifel","https://youtu.be","Tier,Meme_template","Ein sitzender Frosch. Ist er nicht süß?")
tb.search_index()
#tb.add_index("/home/marcel/Downloads/people.jpg","Leute","Rote Leute","https://www.pexels.com","Menschen,Uniform","Leute in roten Uniformen. Warum stehen sie so? Wer weiß.")
#sys.argv+=("-t","Fifel")
sys.argv+=("-g","Meme")
#sys.argv+=("-f","ea892d6e-a20a-4784-930e-cabceb7b98ab")
print(tb.search_index(sys.argv[1:]))
if __name__ == "__main__":

View file

@ -1,4 +1,4 @@
import os
ROOT_DIR=os.getcwd()
CONFIG_DIR=ROOT_DIR
ROOT_DIR=os.getcwd() # The directory where all directories and files of the index are located
CONFIG_DIR=ROOT_DIR # The directory where the file 'index.db' is located