How do I check whether a file is available to download?
The following code generates a link to download the HD file from a site every day, but it doesn't know how to determine whether the link is valid or not. How do I check the validity of the link?
set z to ""
set d to tid(tid(short date string of (current date), "/"), "")
set w to weekday of (current date)
set r to 10
if d > 20000000 then
set Prefix to d as text
else
set Prefix to (d + 20000000) as text
end if
on tid(input, delim)
set {oldTID, my text item delimiters} to {my text item delimiters, delim}
if class of input is list then
set output to input as text
else
set output to text items of input
end if
set my text item delimiters to oldTID
return output
end tid
set x to 1
set y to ((characters 1 thru 4 of Prefix) as string)
repeat with i from 1 to r
if i < 10 then
set x to "0" & i
else
set x to i
end if
set c to "http://streaming.hkjc.edgesuite.net/hdflash/racingfocus/" & y & "/" & Prefix & "/" & x & "/" & "chi/racingfocus_" & Prefix & "_" & x & "" & "_chi_2500kbps.mp4"
set z to z & c & "" & return & return
set i to i + 1
end repeat
display dialog z
I usually check the HTTP Code and then run from there. Since this is tagged AppleScript here is an example handler I wrote:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set foundMP4 to my checkStream(c)
on checkStream(passedStream)
try
set serverCommand to "curl -o /dev/null -Iw '%{http_code}' " & passedStream
set serverResult to do shell script serverCommand
if serverResult does not start with "2" then
return false
else
return true
end if
on error serverResult
display dialog "Ran into issue checking server status, with error: " & serverResult with title "Server Check"
end try
end checkStream
After you check the HTTP code you could write for the script to download the file.