You are viewing our Forum Archives. To view or take place in current topics click here.
Visual Basic | Getting image url from api code
Posted:

Visual Basic | Getting image url from api codePosted:

5FDP_Jekyll
  • TTG Addict
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
So I am currently creating a simple program that changes the desktop background to the current Astronomy Picture of the Day. While I have a method that works for today's image, as far as fetching the url from the api code, I highly doubt it would work for tomorrow or any other day. I am doing this in visual studio.

Here is the code from the api for today's image:

{
  "copyright": "Sergei Makurin",
  "date": "2018-04-03",
  "explanation": "You may have heard of the Seven Sisters in the sky, but have you heard about the Seven Strong Men on the ground? Located just west of the Ural Mountains, the unusual Manpupuner rock formations are one of the Seven Wonders of Russia. How these ancient 40-meter high pillars formed is yet unknown.  The persistent photographer of this featured image battled rough terrain and uncooperative weather to capture these rugged stone towers in winter at night, being finally successful in February of 2014.  Utilizing the camera's time delay feature, the photographer holds a flashlight in the foreground near one of the snow-covered pillars.  High above, millions of stars shine down, while the band of our Milky Way Galaxy crosses diagonally down from the upper left.",
  "hdurl": "https://apod.nasa.gov/apod/image/1804/SevenStrongSky_Makurin_960.jpg",
  "media_type": "image",
  "service_version": "v1",
  "title": "The Milky Way over the Seven Strong Men Rock Formations",
  "url": "https://apod.nasa.gov/apod/image/1804/SevenStrongSky_Makurin_960.jpg"
}


And here is the code I am currently using to get the link to the image, please not I have starred out the actual api address, after all, that's supposed to stay private to you only.

Dim request As WebRequest = WebRequest.Create("https://api.nasa.gov/planetary/apod?api_key=*************")
    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
    Dim datastream As Stream = response.GetResponseStream
    Dim reader As New StreamReader(datastream)
    Dim strData As String = reader.ReadToEnd
    Dim pos1 As Long = InStr(strData, """hdurl"": """)
    Dim pos2 As Long = InStr(pos1 - 2, strData, ".jpg", vbTextCompare)
    Dim strLink As String = strData.Substring(pos1 + 9, pos2 - pos1 - 6)
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.