Show the Local Weather - API problem with weather icon

The API:

'https://fcc-weather-api.glitch.me/api/current?lat=' + lat + '&lon=' + lon

Can cause problems when trying to get the weather icon code. Depending on the weather, the weather:{} object’s array length can can either be 1 or 2. When weather:{} length is 1, the icon value is a URL (example):

https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F01n.png?1499366020783

Where 01n is the icon code and needs to be extracted from the URL. But when weather:{} length is 2, the icon is in the second index and its value is (example):

01n

Although it’s possible to write code to extract the icon depending on weather:{} length, I think the API should be more consistent so that we can receive predictable results for every request.

I believe the FCC weather API is just a pass-through for another API. It prevents us from having to use API keys. So what your getting is just what FCC is getting from the other API.

I was unable to duplicate your getting two weather objects in an array. From what I see, weather is always an array of objects. The information pages says, “Images links are included in the JSON under weather[0].icon.”

So just always use the first object in the array. I’m not sure why there’d ever be two, but if there is, just ignore it.

You can error check if there is an icon or not. That is just part of APIs. You’re trusting someone else’s code. But as Reagan famously said, “Trust but verify.” API’s are commonly not well documented, have errors, and change without warning.

1 Like

I just tried looping though a few thousand different weather requests for different locations (sorry, API :frowning: - I have this image of the lights dimming and flickering for a few seconds over at FCC headquarters) and failed to get anything with a weather array bigger than 1.

Perhaps there are a few edge cases where you get an odd number. I would just do as the FCC page suggests - use the first object in the array. And it’s a good idea to check if the icon exists anyway, because not all requests returned an icon.

I got lucky this morning and it happened again. (@ksjazzguitar haha, I tried the same thing to duplicate results with no luck, until today).

It looks like when there is more than one type of weather condition, the API responds with both (or maybe more). So I just wrote a function that gathers them all and comma-separates them on the screen.

Notice that the icon ID is in the second index and is shortened, instead of displaying the full URL. That’s fine with me because I’m using my own icons.

Also, I’d give you the coordinates but by the time you read this the weather will have changed! Just search Canada as we are going into horrible weather until May :frowning:

{
    "coord": {
        "lon": x,
        "lat": x
    },
    "weather": [{
        "id": 500,
        "main": "Rain",
        "description": "light rain"
    }, {
        "id": 701,
        "main": "Mist",
        "description": "mist",
        "icon": "50d"
    }],
    "base": "stations",
    "main": {
        "temp": 6,
        "pressure": 1017,
        "humidity": 100,
        "temp_min": 6,
        "temp_max": 6
    },
    "visibility": 2414,
    "wind": {
        "speed": 3.1,
        "deg": 70
    },
    "clouds": {
        "all": 90
    },
    "dt": 1509631200,
    "sys": {
        "type": 1,
        "id": x,
        "message": 0.1704,
        "country": "x",
        "sunrise": 1509623068,
        "sunset": 1509659221
    },
    "id": x,
    "name": "x",
    "cod": x
}
1 Like

OK, I’m not sure why you would go to the trouble of removing the location information, but OK.

So, I did a search of over 10,000 locations and couldn’t generate one. And you’ve gotten at least two, maybe more. It might be your location. For whatever reason, the weather service generates two data sets for you. Maybe you’re at a point where areas overlap. But FCC is not generating this data

I don’t know. But I would just say, deal with it. That’s part of web dev. I suppose one could insist that FCC put in some error checking on their passthrough API, but why not do it yourself.

Like I said, some of the weather reports don’t have icons anyway so just check the first element in the array and deal with it if it doesn’t have an icon. If you want to check other elements in the array in that case, then that’s bonus points.

Dealing with troublesome APIs is part of the job. It’s easier to put on a pair of slippers than it is to carpet the world.

1 Like

Ya, it’s fine I solved it yesterday. Just had another look and I’m getting 3 now ha!: Rain,Mist,Drizzle. Looks like my solution is working well.

Glad I found this post. This explains the issues I’ve been experiencing with the icon code in place of the url. I went with an on-error image that makes some kind of sense given the similarity of the problematic weather descriptions.