首页 > 解决方案 > 解析 RSS,如果日期与当前日期匹配,则继续下载

问题描述

想法是检查 RSS 提要<updated> </updated>是否更新了 RSS 更新标头与当前运行的日期匹配,然后继续下载。

示例 RSS 提要:

 <?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title type="text"> Updates</title>
    <subtitle type="html"><![CDATA[Latest  updates]]></subtitle>
    <link href="http://website/website.rss"></link>
    <id>http://website</id>
    <link rel="alternate" type="text/html" href="http://website/website.rss" ></link>
    <link rel="self" type="application/atom+xml" href="https://website/site/download" ></link>
    <logo>http://website/website</logo>
        <updated>2020-03-28 T17:32:48+00:00</updated>
        <entry>
            <author>
                <name>ueam</name>
            </author>
            <title type="text"><![CDATA[2.0.4516]]></title>
            <link rel="alternate" type="text/html" href="https://website/site/download"></link>
            <id>https://website/site/download</id>
            <summary type="html"><![CDATA[<ul>
<li>Patched for March 23rd update</li>
<li>Fixed known bug JIRA</li>
</ul>]]></summary>
            <content type="html"><![CDATA[]]></content>
            <updated>2020-03-28 17:32:48</updated>
        </entry>
        <entry>
            <author>
                <name>Team</name>
            </author>
            <title type="text"><![CDATA[2.0.4516]]></title>
            <link rel="alternate" type="text/html" href="https://website/site/download"></link>
            <id>https://website/site/download</id>
            <summary type="html"><![CDATA[<ul>
        <li>
        Patch for March 23rd update 
    </li>
    </ul>]]></summary>
            <content type="html"><![CDATA[]]></content>
            <updated>2020-03-28 17:32:48</updated>
        </entry>
        <entry>
            <author>
                <name>Team</name>
            </author>
            <title type="text"><![CDATA[2.0.4514]]></title>
            <link rel="alternate" type="text/html" href="https://website/site/download></link>
            <id>https://website/site/download</id>
            <summary type="html"><![CDATA[<ul>
<li>Fixed Bug</li>

这里

<updated>2020-03-28 17:32:48</updated>

显示如果我今天正在运行然后将继续下载,因为它与当天匹配

然后将通过调用下载

Invoke-WebRequest -uri https://website/site/download

我只是不确定如何解析 RSS 以查看“更新”并与当前日期进行比较,如果日期匹配则继续下载。

更新:我尝试了以下

   #Grab RSS
$rssString = Invoke-WebRequest "http://website/website.rss"

# convert rss to xml
$xml = [xml] $rssString

# select the <updated> node
$updateString = $xml.SelectSingleNode("/feed/updated").innerText

# convert string to date
$date = Get-Date $updateString

# compare to todays date
if($date.Date -eq (Get-Date).Date){
  # proceed to download
}

您收到此错误:获取日期:无法将参数“日期”绑定到目标。异常设置“日期”:“无法将 null 转换为类型“System.DateTime”。”

当你跑

$更新字符串

什么都没有返回并且是空白的,似乎更新的节点没有被读取

标签: powershell

解决方案


RSS 是 xml,所以这样对待它:)

<updated>根目录下的节点似乎<feed>反映了最新更新,因此请抓住:

# convert rss to xml
$xml = [xml]$rssString; 

# select the <updated> node
$updateString = $xml.SelectSingleNode("/feed/updated").innerText

# convert string to date
$date = Get-Date $updateString

# compare to todays date
if($date.Date -eq (Get-Date).Date){
  # proceed to download
}

推荐阅读