cnbeta rss 内容解析

2021年6月20日 0 条评论 746 次阅读 0 人点赞

shell脚本解析cnbeta的rss内容

#!/bin/bash
#for cnbeta rss
#source https://rss.cnbeta.com/rss
#example
#
#/rss/channel/item/pubDate=Sun, 20 Jun 2021 03:57:12 GMT
#/rss/channel/item/author=raymon725
#/rss/channel/item/description= <p>尽管 LG 早在今年 4 月份就证实要淡出移动设备市场,并且很快就停掉了新机的生产研发。<strong>不过近日又有传闻称,LG 仍将很快推出几款智能机,其中包括了几款K 系列、Stylo 7、以及 Velvet 2 Pro 。</strong>现在,谷歌 Play 控制台已经证实了它们的存在。那么问题来了 ?? LG 是否打算再次回归智能手机市场呢?</p> <a href="https://www.cnbeta.com/articles/tech/1142819.htm" target="_blank"><strong>阅读全文</strong></a>
#/rss/channel/item/link=https://www.cnbeta.com/articles/tech/1142819.htm
#/rss/channel/item/title=Google Play控制台曝光LG Velvet 2 Pro、Stylo 7、K33与K35智能机

#发送间隔
sleepTime=15
#保留的记录行数
logLine=600
#每次清理的行数
cleanLine=100
#工作目录
path0="/rss/cnbeta"
if [ ! -d "${path0}" ];then mkdir -p "${path0}";fi
rssFile="${path0}/rss"
if [ ! -f "${rssFile}" ];then touch "${rssFile}";fi
rssSend="${path0}/rssSend"
if [ ! -f "${rssSend}" ];then touch "${rssSend}";fi
sendResult="${path0}/sendResult"
if [ ! -f "${sendResult}" ];then touch "${sendResult}";fi
rssUrl="https://rss.cnbeta.com/rss"

if [ -f "${path0}/.lock" ];then exit 0;fi
touch "${path0}/.lock"

checkBin="xml2 jq"
for check in $(echo "${checkBin}");do
	which "${check}" >/dev/null 2>&1 || yum install "${check}" -y >/dev/null 2>&1
	which "${check}" >/dev/null 2>&1 || yum install "${check}" -y >/dev/null 2>&1
done


function messageSend() {
    title=$1
    description=$2
    linePC=$3
    lineWAP=4
    #your messageSend code
}

#检测发送是否成功,例上方发送完成后的返回内容: {"ok": true}
#发送结果检测位
resultFlag="ok"
#发送结果成功内容
resultOK="true"


curl -s "${rssUrl}" | xml2 | tail -n +12 | tac | grep -vE '/rss/channel/item/guid|/rss/channel/item/source' > "${rssFile}"
if [ $? == 0 ];then
	echo "/rss/channel/item" >> "${rssFile}"
	cat "${rssFile}" | while read line;do
		#action
		if [ "$line" == "/rss/channel/item" ];then
			if ! $(grep -q "${link}" "${rssSend}");then
				messageSend "${title}" "${description}" "${linkPC}" "${linkWAP}" > "${sendResult}"
				resultStatu=$(jq ."${resultFlag}" "${sendResult}")
				if [ "$resultStatu" == "${resultOK}" ];then
					echo "${link}" >> "${rssSend}"
					sleep "$sleepTime"
				fi
			fi
		else
			#date
			if $(echo $line | grep -q "/rss/channel/item/pubDate");then
				pubDate=$(echo $line | awk -F '=' '{print $2}')
				pubDate=$(date -d "${pubDate}" "+%Y-%m-%d %H:%M:%S")
			fi
			#author
			if $(echo $line | grep -q "/rss/channel/item/author");then
				author=$(echo $line | awk -F '=' '{print $2}')
			fi
			#description
			if $(echo $line | grep -q "/rss/channel/item/description");then
				description=$(echo $line | awk -F '=' '{print $2}' | sed 's#<[^>]*>##g;s#&[^;]*;##g' | awk -F '<' '{print $1}')
			fi
			#link
			if $(echo $line | grep -q "/rss/channel/item/link");then
				link=$(echo $line | awk -F '=' '{print $2}')
				linkID=$(echo $link | awk -F '[./]+' '{print $7}')
				linkPC="${link}"
				linkWAP="https://m.cnbeta.com/view/${linkID}.htm"
			fi
			#title
			if $(echo $line | grep -q "/rss/channel/item/title");then
				title=$(echo $line | awk -F '=' '{print $2}')
			fi
		fi
	done
fi
if [ "$(wc -l "${rssSend}" | awk '{print $1}')" -gt "${logLine}" ];then sed -i "1,${cleanLine}d" "${rssSend}";fi
unlink "${path0}/.lock"

Sevenfal

这个人太懒什么东西都没留下

文章评论(0)