PowerShell/Scripts/speak-epub.ps1

97 lines
2.7 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-08-29 17:50:03 +02:00
Speaks the content of the given Epub file by text-to-speech (TTS).
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script speaks the content of the given Epub file by text-to-speech (TTS).
2021-10-16 16:50:10 +02:00
.PARAMETER Filename
Specifies the path to the Epub file
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-27 08:35:45 +02:00
PS> ./speak-epub C:\MyBook.epub
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-01-19 09:28:59 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$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
}
}
}