mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-07 03:35:24 +00:00
feat(plugin): create and list plugins
This commit is contained in:
parent
8b77d823d1
commit
64dd3a50f1
@ -13,14 +13,21 @@ from tqdm import tqdm
|
|||||||
|
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
lib_app = typer.Typer()
|
lib_app = typer.Typer()
|
||||||
|
plugin_app = typer.Typer()
|
||||||
|
app.add_typer(plugin_app, name="plugin")
|
||||||
app.add_typer(lib_app, name="lib")
|
app.add_typer(lib_app, name="lib")
|
||||||
|
|
||||||
BASE_URL = "http://localhost:8080"
|
BASE_URL = "http://localhost:8080"
|
||||||
|
|
||||||
|
|
||||||
def format_timestamp(timestamp):
|
def format_timestamp(timestamp):
|
||||||
if isinstance(timestamp, str):
|
if isinstance(timestamp, str):
|
||||||
return timestamp
|
return timestamp
|
||||||
return datetime.fromtimestamp(timestamp, tz=timezone.utc).replace(tzinfo=None).isoformat()
|
return (
|
||||||
|
datetime.fromtimestamp(timestamp, tz=timezone.utc)
|
||||||
|
.replace(tzinfo=None)
|
||||||
|
.isoformat()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def display_libraries(libraries):
|
def display_libraries(libraries):
|
||||||
@ -111,7 +118,9 @@ def scan(library_id: int):
|
|||||||
pbar.update(1)
|
pbar.update(1)
|
||||||
file_path = Path(root) / file
|
file_path = Path(root) / file
|
||||||
absolute_file_path = file_path.resolve() # Get absolute path
|
absolute_file_path = file_path.resolve() # Get absolute path
|
||||||
scanned_files.add(str(absolute_file_path)) # Add to scanned files set
|
scanned_files.add(
|
||||||
|
str(absolute_file_path)
|
||||||
|
) # Add to scanned files set
|
||||||
file_stat = file_path.stat()
|
file_stat = file_path.stat()
|
||||||
file_type = (
|
file_type = (
|
||||||
mimetypes.guess_type(file_path)[0] or "application/octet-stream"
|
mimetypes.guess_type(file_path)[0] or "application/octet-stream"
|
||||||
@ -195,7 +204,7 @@ def scan(library_id: int):
|
|||||||
while True:
|
while True:
|
||||||
existing_files_response = httpx.get(
|
existing_files_response = httpx.get(
|
||||||
f"{BASE_URL}/libraries/{library_id}/folders/{folder['id']}/entities",
|
f"{BASE_URL}/libraries/{library_id}/folders/{folder['id']}/entities",
|
||||||
params={"limit": limit, "offset": offset}
|
params={"limit": limit, "offset": offset},
|
||||||
)
|
)
|
||||||
if existing_files_response.status_code != 200:
|
if existing_files_response.status_code != 200:
|
||||||
tqdm.write(
|
tqdm.write(
|
||||||
@ -214,7 +223,9 @@ def scan(library_id: int):
|
|||||||
f"{BASE_URL}/libraries/{library_id}/entities/{existing_file['id']}"
|
f"{BASE_URL}/libraries/{library_id}/entities/{existing_file['id']}"
|
||||||
)
|
)
|
||||||
if 200 <= delete_response.status_code < 300:
|
if 200 <= delete_response.status_code < 300:
|
||||||
tqdm.write(f"Deleted file from library: {existing_file['filepath']}")
|
tqdm.write(
|
||||||
|
f"Deleted file from library: {existing_file['filepath']}"
|
||||||
|
)
|
||||||
total_files_deleted += 1
|
total_files_deleted += 1
|
||||||
else:
|
else:
|
||||||
tqdm.write(
|
tqdm.write(
|
||||||
@ -228,5 +239,39 @@ def scan(library_id: int):
|
|||||||
print(f"Total files deleted: {total_files_deleted}")
|
print(f"Total files deleted: {total_files_deleted}")
|
||||||
|
|
||||||
|
|
||||||
|
def display_plugins(plugins):
|
||||||
|
table = []
|
||||||
|
for plugin in plugins:
|
||||||
|
table.append(
|
||||||
|
[plugin["id"], plugin["name"], plugin["description"], plugin["webhook_url"]]
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
tabulate(
|
||||||
|
table,
|
||||||
|
headers=["ID", "Name", "Description", "Webhook URL"],
|
||||||
|
tablefmt="plain",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@plugin_app.command("ls")
|
||||||
|
def ls():
|
||||||
|
response = httpx.get(f"{BASE_URL}/plugins")
|
||||||
|
plugins = response.json()
|
||||||
|
display_plugins(plugins)
|
||||||
|
|
||||||
|
|
||||||
|
@plugin_app.command("create")
|
||||||
|
def create(name: str, webhook_url: str, description: str = ""):
|
||||||
|
response = httpx.post(
|
||||||
|
f"{BASE_URL}/plugins",
|
||||||
|
json={"name": name, "description": description, "webhook_url": webhook_url},
|
||||||
|
)
|
||||||
|
if 200 <= response.status_code < 300:
|
||||||
|
print("Plugin created successfully")
|
||||||
|
else:
|
||||||
|
print(f"Failed to create plugin: {response.status_code} - {response.text}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app()
|
app()
|
||||||
|
@ -122,6 +122,10 @@ def create_plugin(newPlugin: NewPluginParam, db: Session) -> Plugin:
|
|||||||
return db_plugin
|
return db_plugin
|
||||||
|
|
||||||
|
|
||||||
|
def get_plugins(db: Session) -> List[Plugin]:
|
||||||
|
return db.query(PluginModel).all()
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_by_name(plugin_name: str, db: Session) -> Plugin | None:
|
def get_plugin_by_name(plugin_name: str, db: Session) -> Plugin | None:
|
||||||
return (
|
return (
|
||||||
db.query(PluginModel)
|
db.query(PluginModel)
|
||||||
|
@ -200,6 +200,13 @@ def new_plugin(new_plugin: NewPluginParam, db: Session = Depends(get_db)):
|
|||||||
return plugin
|
return plugin
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/plugins", response_model=List[Plugin])
|
||||||
|
def list_plugins(db: Session = Depends(get_db)):
|
||||||
|
plugins = crud.get_plugins(db)
|
||||||
|
return plugins
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/libraries/{library_id}/plugins", status_code=status.HTTP_204_NO_CONTENT)
|
@app.post("/libraries/{library_id}/plugins", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
def add_library_plugin(
|
def add_library_plugin(
|
||||||
library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db)
|
library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user