2021-02-09 19:30:45 +01:00
|
|
|
#!/bin/powershell
|
2021-01-19 09:28:59 +01:00
|
|
|
<#
|
2021-03-22 20:10:18 +01:00
|
|
|
.SYNTAX ./speak-epub.ps1 [<filename>]
|
|
|
|
.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
|
2021-01-19 09:28:59 +01:00
|
|
|
#>
|
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
param($Filename = "")
|
2021-01-23 15:36:32 +01:00
|
|
|
|
2021-01-27 15:12:17 +01:00
|
|
|
function Speak { param([string]$Text)
|
2021-01-23 15:36:32 +01:00
|
|
|
write-output "$Text"
|
|
|
|
$Voice = new-object -ComObject SAPI.SPVoice
|
|
|
|
$Voices = $Voice.GetVoices()
|
|
|
|
foreach ($OtherVoice in $Voices) {
|
|
|
|
$Description = $OtherVoice.GetDescription()
|
|
|
|
if ($Description -like "*- English (United States)") {
|
|
|
|
$Voice.Voice = $OtherVoice
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[void]$Voice.Speak($Text)
|
|
|
|
}
|
2021-01-19 09:28:59 +01:00
|
|
|
|
2021-01-23 15:36:32 +01:00
|
|
|
function ReadBook() { param([string]$book, [string]$bookPath, [int]$lineNumber = 0)
|
2021-01-19 09:28:59 +01:00
|
|
|
$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("<title>")) {
|
|
|
|
$line = $line -Replace "<.+?>",""
|
2021-01-23 15:36:32 +01:00
|
|
|
Speak $line
|
2021-01-19 09:28:59 +01:00
|
|
|
}
|
|
|
|
if ($line.contains("<p")) {
|
|
|
|
$line = $line -Replace "<.+?>",""
|
2021-01-23 15:36:32 +01:00
|
|
|
Speak $line
|
2021-01-19 09:28:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Set-Content -Path $bookPath"\progress.txt" -Value ("")
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:36:32 +01:00
|
|
|
function UnzipFile() { param([string]$file, [string]$dest)
|
2021-01-19 09:28:59 +01:00
|
|
|
$shell = new-object -com shell.application
|
|
|
|
$zip = $shell.NameSpace($file)
|
|
|
|
foreach($item in $zip.items()) {
|
|
|
|
$shell.Namespace($dest).copyhere($item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:36:32 +01:00
|
|
|
if ($Filename -eq "") {
|
|
|
|
$Filename = read-host "Enter path to .epub file"
|
|
|
|
}
|
|
|
|
write-output "Reading $Filename ..."
|
|
|
|
$file = get-item $Filename
|
2021-02-22 08:19:03 +01:00
|
|
|
if (-not(Test-Path $file.DirectoryName+"\"+$file.Name+".zip")) {
|
2021-01-19 09:28:59 +01:00
|
|
|
$zipFile = $file.DirectoryName+"\"+$file.Name+".zip"
|
|
|
|
$file.CopyTo($zipFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
$destination = $file.DirectoryName+"\"+$file.Name.Replace($file.Extension,"")
|
2021-02-22 08:19:03 +01:00
|
|
|
if (-not(Test-Path $destination)) {
|
2021-01-19 09:28:59 +01:00
|
|
|
md $destination
|
2021-01-23 15:36:32 +01:00
|
|
|
UnzipFile -file $zipFile -dest $destination
|
2021-01-19 09:28:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[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") {
|
2021-02-22 08:19:03 +01:00
|
|
|
if (Test-Path $bookPath+"\progress.txt") {
|
2021-01-19 09:28:59 +01:00
|
|
|
$progress = Get-Content $bookPath"\progress.txt"
|
|
|
|
$progress = $progress.Split(",")
|
|
|
|
}
|
|
|
|
$bookFileName = $item.href
|
|
|
|
if ($progress.Count -eq 2) {
|
|
|
|
if ($progress[0] -eq $bookPath+"\"+$bookFileName) {
|
2021-01-23 15:36:32 +01:00
|
|
|
ReadBook -book $bookPath"\"$bookFileName -bookPath $bookPath -lineNumber $progress[1]
|
2021-01-19 09:28:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-01-23 15:36:32 +01:00
|
|
|
ReadBook -book $bookPath"\"$bookFileName -bookPath $bookPath
|
2021-01-19 09:28:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|