Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions read.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<#
.SYNOPSIS
Convenience wrapper for reader3: process a book, (re)start the server, and open it.

.EXAMPLE
.\read.ps1 dracula.epub # process (if new), start server if needed, open the book
.EXAMPLE
.\read.ps1 # just open the library, starting the server if needed
.EXAMPLE
.\read.ps1 -Stop # stop the server
#>
param(
[string]$EpubPath,
[switch]$Stop
)

$ErrorActionPreference = "Stop"

function Get-ServerPid {
$line = netstat -ano | Select-String "127.0.0.1:8123\s.*LISTENING"
if ($line) {
return ($line -split "\s+")[-1]
}
return $null
}

Push-Location $PSScriptRoot
try {
$serverPid = Get-ServerPid

if ($Stop) {
if ($serverPid) {
Stop-Process -Id $serverPid -Force
Write-Host "Server stopped."
} else {
Write-Host "Server was not running."
}
return
}

$dataDir = $null
if ($EpubPath) {
if (-not (Test-Path $EpubPath)) {
Write-Error "File not found: $EpubPath"
return
}
$bookName = [System.IO.Path]::GetFileNameWithoutExtension($EpubPath)
$dataDir = "${bookName}_data"
if (-not (Test-Path $dataDir)) {
Write-Host "Processing $EpubPath..."
uv run reader3.py $EpubPath
}
}

if (-not $serverPid) {
Write-Host "Starting server..."
Start-Process -WindowStyle Hidden -FilePath "uv" -ArgumentList "run","server.py" -WorkingDirectory $PSScriptRoot
Start-Sleep -Seconds 2
}

if ($dataDir) {
Start-Process "http://localhost:8123/read/$dataDir"
} else {
Start-Process "http://localhost:8123/"
}
}
finally {
Pop-Location
}
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ async def library_view(request: Request):
return templates.TemplateResponse("library.html", {"request": request, "books": books})

@app.get("/read/{book_id}", response_class=HTMLResponse)
async def redirect_to_first_chapter(book_id: str):
async def redirect_to_first_chapter(request: Request, book_id: str):
"""Helper to just go to chapter 0."""
return await read_chapter(book_id=book_id, chapter_index=0)
return await read_chapter(request=request, book_id=book_id, chapter_index=0)

@app.get("/read/{book_id}/{chapter_index}", response_class=HTMLResponse)
async def read_chapter(request: Request, book_id: str, chapter_index: int):
Expand Down