-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "DNtp" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions
-- Get the selection
tell application id "DNtp" to set thisSelection to the selection
-- Error handling
if thisSelection is {} then error localized string "Please select a document or group, then try again."
-- if (length of thisSelection) > 1 then error localized string "Please select only one document or group, then try again."
tell application id "DNtp"
set theTag404 to {"荒原"}
repeat with theRecord in thisSelection
set originalURL to URL of theRecord
set theTags to tags of theRecord
set theTitle to name of theRecord
if (originalURL contains "zhihu.com") and (not (theTags contains theTag404)) then
-- 如有必要, 调整 delay 参数以减缓触发 Zhihu 的反爬
delay 1
-- Extract itemID
set itemID to do shell script "echo " & quoted form of originalURL & " | awk -F'/' '{print $NF}'"
-- display dialog itemID
-- Convert to zhihuAPI
if (originalURL contains "question") then
if (originalURL contains "answer") then
-- 回答
set convertedURL to "https://www.zhihu.com/api/v4/answers/" & itemID
else
-- 问题
set convertedURL to "https://www.zhihu.com/api/v4/questions/" & itemID
end if
else if (originalURL contains "zhuanlan") then
-- 专栏
set convertedURL to "https://www.zhihu.com/api/v4/articles/" & itemID
else if (originalURL contains "pin") then
-- 想法
set convertedURL to "https://www.zhihu.com/api/v4/pins/" & itemID
end if
-- display dialog convertedURL
-- check the URL using Zhihu API
set urlContent to do shell script "curl " & quoted form of convertedURL
-- display dialog urlContent
if (urlContent contains "系统监测到您的网络环境存在异常") then
display dialog "⚠️检测到「知乎反爬验证」,请前往 zhihu.com 人工验证。
程序终止于 《" & theTitle & "》条目"
exit repeat
else if (urlContent contains "没有知识存在的荒原") then
set tags of theRecord to theTags & theTag404
-- display dialog "The original post is dead. " & originalURL
else
-- display dialog "the URL is still alive"
end if
end if
end repeat
display dialog "Check zhihu 404 Completed"
end tell