Downloading .JPG from web

Post your Gambas programming questions here.
Post Reply
Diverod
Posts: 28
Joined: Friday 12th November 2021 1:31am
Location: North Central Florida

Downloading .JPG from web

Post by Diverod »

I'm trying to download files / images from the web. I test them first in my browser to make sure they are accessible. I can get some to work with no problems, but there is one that's kick'n me down the road.

This text file works fine:

Code: Select all

File.save(ImageFilePath &/ "test.txt", HttpClient.Download("https://moz.com/robots.txt"))
This jpg works fine:

Code: Select all

File.save(ImageFilePath &/ "testPic.jpg", HttpClient.Download("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG"))
This one is thumb'n it's nose at me:

Code: Select all

File.save(ImageFilePath &/ "test4.jpg", HttpClient.Download("https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/15/20213631716_GOES16-ABI-se-15-1200x1200.jpg"))
I can't get it to work with curl or wget from terminal either, all it returns is:

Code: Select all

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
I've also assigned a UserAgent but it didn't help. Is the returned text telling me something? Any guidance or direction would be appreciated. Thanks
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Downloading .JPG from web

Post by vuott »

Diverod wrote: Monday 3rd January 2022 10:31pm I can't get it to work with curl or wget
...if you want to try extern Curl library :) with Gambas:
Library "libcurl:4.6.0"

Private Const CURLOPT_WRITEDATA As Integer = 10001
Private Const CURLOPT_URL As Integer = 10002
   
' CURL *curl_easy_init(void)
' Start a libcurl easy session.
Private Extern curl_easy_init() As Pointer

' CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...)
' Set options for a curl easy handle
Private Extern curl_easy_setopt(curl As Pointer, optionI As Integer, optionP As Pointer) As Integer
 
' CURLcode curl_easy_perform(CURL *curl)
' Perform a blocking file transfer.
Private Extern curl_easy_perform(curl As Pointer) As Pointer

' const char *curl_easy_strerror(CURLcode)
' Turn a CURLcode value into the equivalent human readable error string.
Private Extern curl_easy_strerror(CURLcode As Integer) As String

' void curl_easy_cleanup(CURL *curl)
' End a libcurl easy handle.
Private Extern curl_easy_cleanup(curl As Pointer)


Library "libc:6"

' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(__filename As String, __modes As String) As Pointer

' int fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(__stream As Pointer) As Integer


Public Sub Main()

 Dim cu, fl As Pointer
 Dim url, final_file As String
 Dim ris As Integer

' Web address of JPG image-file, that you want to download:
 url = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG"

' Sets the path (and file name) where JPG image-file will be re-created:
 final_file = "/tmp/file.jpg"

 cu = curl_easy_init()
 If cu == 0 Then Error.Raise("Error !")

 curl_easy_setopt(cu, CURLOPT_URL, url)

 fl = fopen(final_file, "wb")
 If fl == 0 Then Error.Raise("Error !")

 curl_easy_setopt(cu, CURLOPT_WRITEDATA, fl)

' Writes new JPG image file:
 ris = curl_easy_perform(cu)
 If ris <> 0 Then Error.Raise("Error writing image-file: " & curl_easy_strerror(ris))

' Free memory:
 fclose(fl)
 curl_easy_cleanup(cu)

End

...if you want to try "wget" with Gambas:
Public Sub Main()

' It will download the image file to the "/tmp" folder.
' The Process of the "wget" command, launched by the "Shell" instruction, will be opened for "Read" to obtain the messages sent by the aforementioned Process in the console/Terminal.
  Shell "wget https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG --directory-prefix /tmp"  For Input As "Processus" 

End

Public Sub Processus_Read()

 Dim s As String

 Line Input #Last, s

 Print s

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Downloading .JPG from web

Post by BruceSteers »

If you look at this page...
https://cdn.star.nesdis.noaa.gov/GOES16 ... TOR/se/15/

you'll see the file does not exist
Last edited by BruceSteers on Tuesday 4th January 2022 2:19am, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
Diverod
Posts: 28
Joined: Friday 12th November 2021 1:31am
Location: North Central Florida

Re: Downloading .JPG from web

Post by Diverod »

vuott wrote: Tuesday 4th January 2022 12:55am ...if you want to try "wget" with Gambas:
Public Sub Main()

' It will download the image file to the "/tmp" folder.
' The Process of the "wget" command, launched by the "Shell" instruction, will be opened for "Read" to obtain the messages sent by the aforementioned Process in the console/Terminal.
  Shell "wget https://upload.wikimedia.org/wikipedia/ ... -Gamba.JPG --directory-prefix /tmp"  For Input As "Processus" 

End

Public Sub Processus_Read()

 Dim s As String

 Line Input #Last, s

 Print s

End
Thank you very much vuott! The wget code worked perfect in Gambas for all cases tried.
I've been reading the "man" page on curl and find it a bit overwhelming at this point for me but your code using a curl library is very helpful in understanding some of it. Thanks, RodG.
Diverod
Posts: 28
Joined: Friday 12th November 2021 1:31am
Location: North Central Florida

Re: Downloading .JPG from web

Post by Diverod »

BruceSteers wrote: Tuesday 4th January 2022 1:29am If you look at this page...
https://cdn.star.nesdis.noaa.gov/GOES16 ... TOR/se/05/

you'll see the file does not exist
Okay, Bruce, now I feel stupid :o I swear I could get it to display in my browser when testing this. I know these do expire after a certain time so I should have cleared my browser cache. Lesson learned after leaving my computer on for 2 days! I got a new one

Code: Select all

https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/15/20220032041_GOES16-ABI-se-15-300x300.jpg
and now everything works that I had previously tried :!:
Thanks for letting me know because I might have been still chasing that one for a while. RodG
Post Reply