From 426d023753f21eb784c821f780acaea9feefa76f Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Tue, 19 Jan 2021 09:28:59 +0100 Subject: [PATCH] Added speak-epub.ps1 --- Data/scripts.csv | 1 + README.md | 1 + Scripts/speak-epub.ps1 | 81 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 Scripts/speak-epub.ps1 diff --git a/Data/scripts.csv b/Data/scripts.csv index 1aa3aeee..ee8ec43d 100644 --- a/Data/scripts.csv +++ b/Data/scripts.csv @@ -70,6 +70,7 @@ SHA256.ps1, prints the SHA256 checksum of the given file simulate-matrix.ps1, simulates the Matrix (fun) simulate-presence.ps1, simulates the human presence against burglars speak-date.ps1, speaks the current date by text-to-speech (TTS) +speak-epub.ps1, speaks the content of the given Epub file by text-to-speech (TTS) speak-file.ps1, speaks the content of the given text file by text-to-speech (TTS) speak-test.ps1, performs a speak test by text-to-speech (TTS) speak-text.ps1, speaks the given text by text-to-speech (TTS) diff --git a/README.md b/README.md index 86b50be5..ed70e8fb 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol * [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun) * [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars * [speak-date.ps1](Scripts/speak-date.ps1) - speaks the current date by text-to-speech (TTS) +* [speak-epub.ps1](Scripts/speak-epub.ps1) - speaks the content of the given Epub file by text-to-speech (TTS) * [speak-file.ps1](Scripts/speak-file.ps1) - speaks the content of the given text file by text-to-speech (TTS) * [speak-test.ps1](Scripts/speak-test.ps1) - performs a speak test by text-to-speech (TTS) * [speak-text.ps1](Scripts/speak-text.ps1) - speaks the given text by text-to-speech (TTS) diff --git a/Scripts/speak-epub.ps1 b/Scripts/speak-epub.ps1 new file mode 100644 index 00000000..86d03a6f --- /dev/null +++ b/Scripts/speak-epub.ps1 @@ -0,0 +1,81 @@ +#!/snap/bin/powershell +<# +.SYNTAX ./speak-epub.ps1 [] +.DESCRIPTION speaks the content of the given Epub file by text-to-speech (TTS) +.LINK https://github.com/fleschutz/PowerShell +.NOTES Author: Markus Fleschutz / License: CC0 +#> + +$read = New-Object -com SAPI.SpVoice + +function readBook() { param([string]$book, [string]$bookPath, [int]$lineNumber = 0) + $data = Get-Content $book + for([int]$i=$lineNumber;$i -lt $data.Count;$i++) { + Set-Content -Path $bookPath"\progress.txt" -Value ($book+","+$i) + $line = $data[$i] + if ($line.Contains("")) { + $line = $line -Replace "<.+?>","" + $read.Speak($line) + } + if ($line.contains("<p")) { + $line = $line -Replace "<.+?>","" + $read.Speak($line) + } + } + Set-Content -Path $bookPath"\progress.txt" -Value ("") +} + +function Get-FileName() { + [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null + $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog + #$OpenFileDialog.initialDirectory = "c:\" + $OpenFileDialog.filter = "ePub files (*.epub)| *.epub" + $OpenFileDialog.ShowDialog() | Out-Null + return $OpenFileDialog.filename +} + +function unzip() { param([string]$file, [string]$dest) + $shell = new-object -com shell.application + $zip = $shell.NameSpace($file) + foreach($item in $zip.items()) { + $shell.Namespace($dest).copyhere($item) + } +} + +$epubFile = Get-FileName +$file = get-item $epubFile +if (!(Test-Path($file.DirectoryName+"\"+$file.Name+".zip"))) { + $zipFile = $file.DirectoryName+"\"+$file.Name+".zip" + $file.CopyTo($zipFile) +} + +$destination = $file.DirectoryName+"\"+$file.Name.Replace($file.Extension,"") +if (!(Test-Path($destination))) { + md $destination + unzip -file $zipFile -dest $destination +} + +[xml]$container = Get-Content $destination"\META-INF\container.xml" +$contentFilePath = $container.container.rootfiles.rootfile."full-path" +[xml]$content = Get-Content $destination"\"$contentFilePath +$tmpPath = Get-Item $destination"\"$contentFilePath +$bookPath = $tmpPath.DirectoryName +$progress = $null + +foreach($item in $content.package.manifest.Item) { + if ($item."media-type" -eq "application/xhtml+xml") { + if (Test-Path($bookPath+"\progress.txt")) { + $progress = Get-Content $bookPath"\progress.txt" + $progress = $progress.Split(",") + } + $bookFileName = $item.href + if ($progress.Count -eq 2) { + if ($progress[0] -eq $bookPath+"\"+$bookFileName) { + readBook -book $bookPath"\"$bookFileName -bookPath $bookPath -lineNumber $progress[1] + } + } + else { + readBook -book $bookPath"\"$bookFileName -bookPath $bookPath + } + } +}