Downloading pdfs

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
Jellicle
Posts: 448
Joined: Thu Feb 24, 2011 11:07 am

Downloading pdfs

Post by Jellicle » Thu Jan 18, 2018 11:32 am

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
27" iMac, 27" Cinema Display, OS X Mavericks
iPhone 5 (plus a bunch of older devices for testing)
LiveCode Commercial license. Developer Program member.

Klaus
Posts: 10327
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Downloading pdfs

Post by Klaus » Thu Jan 18, 2018 1:20 pm

HI Gerry,

welcome back! :D

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! :D


Best

Klaus

Jellicle
Posts: 448
Joined: Thu Feb 24, 2011 11:07 am

Re: Downloading pdfs

Post by Jellicle » Thu Jan 18, 2018 10:46 pm

Klaus wrote:
Thu Jan 18, 2018 1:20 pm
HI Gerry,

welcome back! :D
Thanks, but it's not for long :)

And thanks for your help. It's amazing what you forget in such a short time :)

Gerry
27" iMac, 27" Cinema Display, OS X Mavericks
iPhone 5 (plus a bunch of older devices for testing)
LiveCode Commercial license. Developer Program member.

Jellicle
Posts: 448
Joined: Thu Feb 24, 2011 11:07 am

Re: Downloading pdfs

Post by Jellicle » Fri Jan 19, 2018 1:23 am

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
27" iMac, 27" Cinema Display, OS X Mavericks
iPhone 5 (plus a bunch of older devices for testing)
LiveCode Commercial license. Developer Program member.

Jellicle
Posts: 448
Joined: Thu Feb 24, 2011 11:07 am

Re: Downloading pdfs

Post by Jellicle » Fri Jan 19, 2018 5:06 am

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
27" iMac, 27" Cinema Display, OS X Mavericks
iPhone 5 (plus a bunch of older devices for testing)
LiveCode Commercial license. Developer Program member.

AndyP
Posts: 367
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Downloading pdfs

Post by AndyP » Fri Jan 19, 2018 12:02 pm

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.
Andy Piddock

http://2108.co.uk/tinyide/ - TinyIDE gives you more space to develop your ideas

http://2108.co.uk/script-editor-themer/ Script Editor Themer - Your code has never looked so good

My software never has bugs. It just develops random features

AndyP
Posts: 367
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Downloading pdfs

Post by AndyP » Fri Jan 19, 2018 12:27 pm

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
Andy Piddock

http://2108.co.uk/tinyide/ - TinyIDE gives you more space to develop your ideas

http://2108.co.uk/script-editor-themer/ Script Editor Themer - Your code has never looked so good

My software never has bugs. It just develops random features

Klaus
Posts: 10327
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Downloading pdfs

Post by Klaus » Fri Jan 19, 2018 1:01 pm

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 8) ) 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...

Jellicle
Posts: 448
Joined: Thu Feb 24, 2011 11:07 am

Re: Downloading pdfs

Post by Jellicle » Sat Jan 20, 2018 11:59 pm

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
27" iMac, 27" Cinema Display, OS X Mavericks
iPhone 5 (plus a bunch of older devices for testing)
LiveCode Commercial license. Developer Program member.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”