Page 1 of 1
Downloading pdfs
Posted: Thu Jan 18, 2018 11:32 am
by Jellicle
I used to be a LiveCode developer but I've been away from the platform for a few years.
I have 524 pdf files on a web server, at
http://webserver.com/1 to
http://webserver.com/524. I want a script that will download them all to a folder on my desktop (one at a time is fine, and it doesn't have to be quick).
Can someone suggest a strategy for doing that?
Cheers
Gerry
Re: Downloading pdfs
Posted: Thu Jan 18, 2018 1:20 pm
by Klaus
HI Gerry,
welcome back!
Use a simple repeat loop:
Code: Select all
...
put specialfolderpath("desktop") & "/" into tTargetFolder
put "http://webserver.com/" into tSourceFolder
repeat with i = 1 to 524
put url(tSourceFolder & i) into url("binfile:" & tTargetfolder & i & ".pdf")
## Give engine some time to "breathe":
wait 1 with messages
end repeat
...
Of course some errorchecking (if the result <> emtpy then...) after each "put" will not hurt!
Best
Klaus
Re: Downloading pdfs
Posted: Thu Jan 18, 2018 10:46 pm
by Jellicle
Klaus wrote: ↑Thu Jan 18, 2018 1:20 pm
HI Gerry,
welcome back!
Thanks, but it's not for long
And thanks for your help. It's amazing what you forget in such a short time
Gerry
Re: Downloading pdfs
Posted: Fri Jan 19, 2018 1:23 am
by Jellicle
Ok, seems the server is funky in the way it's handling the pdfs. When you go to
https://websever.com/1 or
https://websever.com/1.pdf in a browser, a pdf loads. But when I run your code, it creates an empty pdf. The error I get when I try to open it in Preview is "The file “2.pdf” could not be opened because it is empty."
Oh well
Cheers
Gerry
Re: Downloading pdfs
Posted: Fri Jan 19, 2018 5:06 am
by Jellicle
Hi again
I tried again with a known, valid .pdf. The following returns "can't open file":
Code: Select all
put specialfolderpath("desktop") & "/" into tTargetFolder
put "http://www.africau.edu/images/default/sample.pdf" into tSourceFolder
put url(tSourceFolder) into url("binfile:" & tTargetfolder)
answer the result
Sigh.
Gerry
Re: Downloading pdfs
Posted: Fri Jan 19, 2018 12:02 pm
by AndyP
Try this method:
Code: Select all
on mouseUp pMouseButton
put specialfolderpath("desktop") & "/" into tTargetFolder
put "http://www.africau.edu/images/default/sample.pdf" into tSourceFolder
libUrlDownloadToFile tSourceFolder, tTargetFolder & "sample.pdf", "downloadEnd"
end mouseUp
on downloadEnd tSourceFolder, pStatus
if pStatus is not "downloaded" then
answer "Download failed"
end if
unload url tSourceFolder
end downloadEnd
Using libUrlDownloadToFile give you a call back message for the download status which really helps to tie down what is happening.
Also note that the name of the file and not just the location to save to MUST be specified.
Re: Downloading pdfs
Posted: Fri Jan 19, 2018 12:27 pm
by AndyP
A variation on the previous method
Code: Select all
on mouseUp pMouseButton
set the itemdel to "/" --new
put specialfolderpath("desktop") & "/" into tTargetFolder
put "http://www.africau.edu/images/default/sample.pdf" into tSourceFolder
put item -1 of tSourceFolder into tFileName --new
libUrlDownloadToFile tSourceFolder, tTargetFolder & tFileName, "downloadEnd" --amended
end mouseUp
on downloadEnd tSourceFolder, pStatus
if pStatus is not "downloaded" then
answer "Download failed"
end if
unload url tSourceFolder
end downloadEnd
This will automatically retrieve the the file name of the source and add it to the target file location
Re: Downloading pdfs
Posted: Fri Jan 19, 2018 1:01 pm
by Klaus
This worked fine for me:
Code: Select all
on mouseUp
put specialfolderpath("desktop") & "/sample.pdf" into tTargetFolder
put "http://www.africau.edu/images/default/sample.pdf" into tSourceFolder
put url(tSourceFolder) into url("binfile:" & tTargetfolder)
answer IT & CR & the result
end mouseUp
IT and THE RESULT were both empty.
I suspect some HTML "tricks", like redirect or whatever, involved in the URLs ->
http://webserver.com/1
since that is obviously NOT the "direct" url to the PDF.
I can see the same on the LC webpage for Datagrid lessons:
http://lessons.livecode.com/m/datagrid
There you can download the PDF with this link:
http://lessons.livecode.com/m/datagrid/pdf
The tiny (must be 2 point textsize
) link below the topics on the left side.
However:
put url("
http://lessons.livecode.com/m/datagrid/pdf") into ttt
Give an EMPTY variable ttt?
Sorry, no ideas so far...
Re: Downloading pdfs
Posted: Sat Jan 20, 2018 11:59 pm
by Jellicle
AndyP wrote: ↑Fri Jan 19, 2018 12:27 pm
A variation on the previous method
Your script worked perfectly! This forum saved my bacon years ago, and again today
Thanks Andy, Klaus!
Gerry