2

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
  • Hi Varela… I'm sorry, but I don't really understand what you're asking. Can you try to make it a little more clear what part you need help with? – TJ Luoma Nov 1 '18 at 3:08
  • @TJLuoma I've (hopefully) made OP's question more clear. – abc Nov 1 '18 at 3:46
  • 2
    You could try downloading the file and if it downloads, the link is valid. – CJK Nov 1 '18 at 4:44
  • If you need specific coding advice, please reduce your code sample to the part where you want do download the file. How you build the URL isn‘t relevant for the question. – nohillside Nov 1 '18 at 6:15
  • An easy way is to use curl and then trap for the 404 error if the file doesn't exist. – Allan Nov 1 '18 at 10:53
4

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.

  • Nice solution. +1 – CJK Nov 1 '18 at 17:41
  • +1 but sometimes websites are stupid and don't feature the correct error codes. Better solution would be to test if there is an actual file available to download. – JBis Nov 1 '18 at 20:05
0

If the server supports it, you should be able to do something like this:

curl -sfLS --head http://www,whatever.tld/this/path/here.ext

(That’s supposed to be two - before head even if it got turned into an em-dash!)

And then you want to check the HTTP status code.

Something like this would work:

URL="http://streaming.hkjc.edgesuite.net/hdflash/racingfocus/2018/20181101/01/chi/racingfocus_20181101_01_chi_2500kbps.mp4"

    # the 'tail -1' means "If there are more than one, just give me the last one"
    # which you'll need in case of redirects.
CODE=$(curl -sL --head "$URL" | awk -F' ' '/^HTTP/{print $2}' | tail -1)

case "$CODE" in
    404)
        echo "Not found: $URL"
    ;;

    200)
        echo "Found! $URL"
    ;;

    *)
        echo "Unknown Response ($CODE) for $URL"
    ;;

esac
  • 1
    This would be a pretty good answer if you hadn't supplied the solution in a completely different scripting language than the one the OP is using with no details on how to adapt that code into her language of choice. – CJK Nov 1 '18 at 17:39
  • True. I did not recognize it as AppleScript at the time when I read it (nor did I see the tag that's there now.) – TJ Luoma Nov 1 '18 at 23:07
-1

You can‘t know whether a remote file exists unless you try to access it. So basically you need to try to download the file and then check for any error codes returned.

  • 3
    Wouldn't this be best served as a comment? – ʀ2ᴅ2 Nov 1 '18 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .