[玩转系统] 周五乐趣:给我读一个故事
作者:精品下载站 日期:2024-12-14 07:43:30 浏览:13 分类:玩电脑
周五乐趣:给我读一个故事
该函数使用 .NET System.Speech 类,该类似乎比传统 COM 替代方案更容易使用。这是您可以测试的片段。
Add-Type -AssemblyName System.speech
$voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
$voice.Speak("Hello. I am $($voice.voice.Name)")
所以基本上,我所要做的就是获取博客文章的内容并将文本传递给语音对象。说起来容易做起来难,当您查看这段代码时您会发现这一点。
#Requires -version 3.0
Function Invoke-BlogReader {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the blog post URL")]
[ValidateNotNullorEmpty()]
[string]$Url,
[ValidateScript({$_ -ge 1})]
[int]$Count = 5,
[switch]$ShowText,
[validateSet("Male","Female")]
[string]$Gender="Male"
)
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Try {
Write-Verbose "Requesting $url"
$web = Invoke-WebRequest -Uri $url -DisableKeepAlive -ErrorAction Stop
Write-Verbose "Selecting content"
$content = $web.ParsedHtml.getElementsByTagName("div") |
where {$_.classname -match'entry-content|post-content'} |
Select -first 1
}
Catch {
Write-Warning "Failed to retrieve content from $url"
#bail out
Return
}
if ($content) {
#define a regex to match sentences
[regex]$rx="(\S.+?[.!?])(?=\s+|$)"
Write-Verbose "Parsing sentences"
$sentences = $rx.matches($content.innertext)
Write-Verbose "Adding System.Speech assembly"
Add-Type -AssemblyName System.speech
Write-Verbose "Initializing a voice"
$voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
$voice.SelectVoiceByHints($gender)
Write-Verbose "Speaking"
$voice.Speak($($web.ParsedHtml.title))
$voice.speak("Published on $($web.ParsedHtml.lastModified)")
Write-Verbose "Here are the first $count lines"
for ($i=0 ;$i -lt $count;$i++) {
$text = $sentences[$i].Value
if ($ShowText) {
write-host "$text " -NoNewline
}
$voice.speak($text)
}
write-host "`n"
}
else {
Write-Warning "No web content found"
}
#clean up
$voice.Dispose()
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end function
该函数使用 Invoke-WebRequest cmdlet 从指定网页检索内容,然后解析 HTML 来查找内容。
$web = Invoke-WebRequest -Uri $url -DisableKeepAlive -ErrorAction Stop
Write-Verbose "Selecting content"
$content = $web.ParsedHtml.getElementsByTagName("div") |
where {$_.classname -match'entry-content|post-content'} |
Select -first 1
这是最棘手的部分,因为不同的博客和网站使用不同的标签和类。有一个 getElementsbyClassName 方法,但这对我来说似乎是时好时坏,所以我选择了使用Where-Object 的较慢但一致的过程。
一旦我有了内容,我就可以简单地传递文本,但我意识到我可能不想听整个帖子。特别是对于我自己的,经常有脚本示例。这些听起来不太好听。所以我意识到我需要将内容解析成句子。正则表达式来救援。
[regex]$rx="(\S.+?[.!?])(?=\s+|$)"
Write-Verbose "Parsing sentences"
$sentences = $rx.matches($content.innertext)
不要让我解释正则表达式模式。我“找到”了它并且它有效。这才是最重要的。 $Sentences 是正则表达式匹配对象的数组。我需要做的就是将每个匹配的值传递给语音对象。另一个好处是我可以包含一个参数来显示正在读取的文本。
for ($i=0 ;$i -lt $count;$i++) {
$text = $sentences[$i].Value
if ($ShowText) {
write-host "$text " -NoNewline
}
$voice.speak($text)
}
这里的所有都是它的!如果您想尝试所有选项,这里有一个示例命令:
$paramHash = @{
Url = "http://blogs.msdn.com/b/powershell/archive/2014/08/27/powershell-summit-europe-2014.aspx"
Count = 5
Gender = "Female"
ShowText = $True
Verbose = $True
}
Invoke-BlogReader @paramHash
真正有趣的地方是使用另一个函数(我不确定我是否曾在这里发布过该函数)从 RSS 提要中获取项目。
#requires -version 3.0
Function Get-RSSFeed {
Param (
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("^http")]
[Alias('url')]
[string[]]$Path="http://feeds.feedburner.com/JeffsScriptingBlogAndMore"
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
} #begin
Process {
foreach ($item in $path) {
$data = Invoke-RestMethod -Uri $item
foreach ($entry in $data) {
#create a custom object
[pscustomobject][ordered]@{
Title = $entry.title
Published = $entry.pubDate -as [datetime]
Link = $entry.origLink
} #hash
} #foreach entry
} #foreach item
} #process
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end Get-RSSFeed
将所有这些放在一起,您可以获得提要,将其通过管道传输到 Out-Gridview,选择一个并让博客读给您听!
get-rssfeed | out-gridview -OutputMode Single | foreach { Invoke-BlogReader $_.link -Count 5}
所以现在您可以拥有您的博客并收听!可能有多种方法可以增强这一点。或者您可能想将其中一些概念和技术转向另一个方向。如果是这样,我希望你能让我知道你的结局。周末愉快。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag