please have multiple JSON files, which contain URLs to images. There are three formats for each image:
- SD version - standard quality ("isThumbNail": false, "isHdImage": false)
- thumbnail - lowest quality ("isThumbNail": true, "isHdImage": false)
- HD - highest quality ("isThumbNail": false, "isHdImage": true)
It looks like this:
{
"objectId": 1234,
"imgCount": 3,
"dataImages": [
{
"sequence": 1,
"link": [
{
"url": "http://example.com/SD/image_1.jpg",
"isThumbNail": false,
"isHdImage": false
},
{
"url": "http://example.com/THUMB/image_1.jpg",
"isThumbNail": true,
"isHdImage": false
},
{
"url": "http://example.com/HD/image_1.jpg",
"isThumbNail": false,
"isHdImage": true
}
]
},
{
"sequence": 2,
"link": [
{
"url": "http://example.com/SD/image_2.jpg",
"isThumbNail": false,
"isHdImage": false
},
{
"url": "http://example.com/THUMB/image_2.jpg",
"isThumbNail": true,
"isHdImage": false
},
{
"url": "http://example.com/HD/image_2.jpg",
"isThumbNail": false,
"isHdImage": true
}
]
},
{
"sequence": 3,
"link": [
{
"url": "http://example.com/SD/image_3.jpg",
"isThumbNail": false,
"isHdImage": false
},
{
"url": "http://example.com/THUMB/image_3.jpg",
"isThumbNail": true,
"isHdImage": false
},
{
"url": "http://example.com/HD/image_3.jpg",
"isThumbNail": false,
"isHdImage": true
}
]
}
]
}
I am trying to get an HD version of the image's URL and append it to images list. It may happen, that there is no HD version of the image, so if it's not present in JSON, I want to download an SD version. And of course, it may also happen, that there will be only a thumbnail version of the image or no image at all - so it should return some empty value, or something safe, that will not break the program.
With this code, I am able to get all isHdImage:
def get_images(url):
try:
images = []
response = requests.get(url)
response.raise_for_status()
data = response.json()
for sequence in data['lotImages']:
for link in sequence['link']:
if link['isHdImage'] is True:
images.append(['url'])
return images
except requests.exceptions.HTTPError as err:
print('HTTPError:', err)
But I am not sure, how I can reach a solution, which I have described above. Thank you for any advice.