Skip to content

Commit

Permalink
Add endpoint to regenerate database
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyscarborough committed Sep 3, 2023
1 parent bee27e6 commit 5528dbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ You can add additional sources by using your own `config.json` file. For example

</details>

## Regenerating the Database

You can regenerate the database from scratch by making a `GET` request to the `/regenerate` path. If the database is regenerated properly you will get a success message returned from the API after it is complete.

## Troubleshooting

These are additional instructions and tips if something doesn't work as expected.
Expand All @@ -597,6 +601,7 @@ These are additional instructions and tips if something doesn't work as expected
You can do this by visiting [http://localhost:5050](http://localhost:5050).
If it says "Local Audio Server (version)", then the server is up and running!
* Ensure you haven't copied any files from the torrent outside of `user_files`.
* Try [regenerating the database](#regenerating-the-database).
* If all else fails, remove the `entries*` files from your `DATA_DIRECTORY` and restart the server.

## Credits & Acknowledgements
Expand Down
48 changes: 27 additions & 21 deletions plugin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sqlite3
import threading

from http import HTTPStatus
from urllib.parse import unquote
from urllib.parse import urlparse
from urllib.parse import parse_qs
Expand All @@ -19,9 +18,10 @@
)
from .consts import *
from .config import ALL_SOURCES
from .db_utils import execute_query


from .db_utils import (
execute_query,
init_db
)

class LocalAudioHandler(http.server.SimpleHTTPRequestHandler):

Expand Down Expand Up @@ -128,17 +128,28 @@ def parse_query_components(self) -> QueryComponents:

return qcomps

def write_response(self, status, payload, content_type="text/plain"):
self.send_response(status)
self.send_header("Content-type", f"{content_type}; charset=UTF-8")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
try:
self.wfile.write(payload)
except BrokenPipeError:
self.log_error("BrokenPipe when sending reply")
return

def send_version(self):
latest_version_file = get_version_file_path()
with open(latest_version_file) as f:
ver = f.read().strip()
payload = f"Local Audio Server v{ver}".encode("utf-8")
self.write_response(200, payload)

self.send_response(200)
self.send_header("Content-type", "text/plain; charset=UTF-8")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def regenerate_database(self):
init_db()
payload = "Regenerated database successfully.".encode("utf-8")
self.write_response(200, payload)

def do_GET(self):
print("GET:", self.path)
Expand All @@ -152,17 +163,21 @@ def do_GET(self):
self.send_version()
return

# regenerates the database
if full_path.strip() == "/regenerate":
self.regenerate_database()
return

if full_path.strip() == "/favicon.ico":
# skip entirely
self.send_response(400)
self.send_response(404)
return

path_parts = full_path.split("/", 2)
if len(path_parts) == 3 and (source_id := path_parts[1]) in ALL_SOURCES:
audio_source = ALL_SOURCES[source_id]
file_path = path_parts[2]
self.get_audio(audio_source.get_media_dir_path(), file_path)
# self._get_audio_android(audio_source.data.id, file_path)
return

qcomps = self.parse_query_components()
Expand Down Expand Up @@ -196,16 +211,7 @@ def do_GET(self):

# Writing the JSON contents with UTF-8
payload = bytes(json.dumps(resp), "utf8")
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "application/json")
self.send_header("Content-length", str(len(payload)))
self.end_headers()
try:
self.wfile.write(payload)
except BrokenPipeError:
self.log_error("BrokenPipe when sending reply")
return

self.write_response(200, payload, "application/json")

def run_server():
# Else, run it in a separate thread so it doesn't block
Expand Down

0 comments on commit 5528dbf

Please sign in to comment.