visit
dataset =
{
"name": "My animals",
"desc": "My animals dataset descrtiption",
"items": [
{"id": 0,
"title": "My dog image",
"thumbnail": "//loremflickr.com/150/150/dog",
"src": "//loremflickr.com/1680/1050/dog"
},
{"id": 1,
"title": "My cat image",
"thumbnail": "//loremflickr.com/150/150/cat",
"src": "//loremflickr.com/1680/1050/cat"
},
{"id": 2,
"title": "My parrot image",
"thumbnail": "//loremflickr.com/150/150/parrot",
"src": "//loremflickr.com/1680/1050/parrot"
}
]
}
response =
{
"photos": {
"page": 1,
"pages": 89071,
"perpage": 25,
"total": 2226767,
"photo": [
{
"id": "12312362",
"title": "Cat",
"description": {
"_content": "My favorite cat..."
},
"datetaken": "2015-06-05 19:23:19",
"ownername": "User512341234",
"latitude": "57.142993",
"longitude": "33.699757",
"place_id": "uQBm12323UrxpE99h",
"url_q": "//live.staticflickr.com/1957/45543214321_abcabcabac_q.jpg",
"url_l": "//live.staticflickr.com/1957/45543214321_abcabcabac_b.jpg",
},
...
]
},
"stat": "ok"
}
}
Here the array of image elements is in the response.photos.photo element.
The image url is in response.photos.photo [index].url_l.
The thumbnail URL (image preview) is in response.photos.photo [index].url_q.
And the description will be contained in response.photos.photo [index].description._content.
response =
{
"total": 23858,
"totalHits": 500,
"hits": [
{
"id": 736877,
"pageURL": "//pixabay.com/photos/tree-cat-736877/",
"type": "photo",
"tags": "tree, cat, silhouette",
"previewURL": "//cdn.pixabay.com/photo/2015/.../tree-736877_150.jpg",
"largeImageURL": "//pixabay.com/get/g07531348f4443b076ebc0567_1280.jpg",
},
...
]
}
The array of image elements should be taken from the response.hits element.
The image url is in response.hits[index].largeImageURL
The thumbnail URL is in response.hits[index].previewURL
The description will have to be taken from some other elements.
class Item {
constructor(object, schema) {
this.id = getProperty(object, schema.item_id);
this.title = getProperty(object, schema.item_title);
this.desc = getProperty(object, schema.item_desc);
this.thumbnail = getProperty(object, schema.item_thumbnail);
this.src = getProperty(object, schema.item_src);
}
}
class Dataset {
constructor(object, schema) {
this.name = "";
this.desc = "";
schema.ds_name.forEach(e => { this.name += getProperty(object, e); });
schema.ds_desc.forEach(e => { this.desc += getProperty(object, e); });
this.items = getProperty(object, schema.items).map(
i => new Item(i, schema)
);
}
}
The key point here is that we pass 2 parameters to the constructors: object and schema.
object is the original formatted dataset or image element that the API service returned to us.
schema - an object containing the mapping of object elements to elements of our dataset.
We can put the above code in datasets.js file as a module.
export const FLICKR_SCHEMA = {
ds_name: ["My Flickr dataset. Page#: ", "photos.page", ", total pages: ", "photos.total"],
ds_desc: ["Photos per page: ", "photos.perpage"],
items: "photos.photo",
item_id: "id",
item_title: "title",
item_desc: "description._content",
item_thumbnail: "url_q",
item_src: "url_l"
};
export const PIXABAY_SCHEMA = {
ds_name: ["My Pixabay dataset. Total: ", "total"],
ds_desc: ["Total hits: ", "totalHits"],
items: "hits",
item_id: "id",
item_title: "title",
item_desc: "tags",
item_thumbnail: "previewURL",
item_src: "largeImageURL"
};
export const PEXELS_SCHEMA = {
ds_name: ["My Pexels dataset. Page: ", "page"],
ds_desc: ["Per page: ", "per_page", " Total results: ", "total_results"],
items: "photos",
item_id: "id",
item_title: "photographer",
item_desc: "url",
item_thumbnail: "src.tiny",
item_src: "src.original"
};
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Image APIs Schemas</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script type="module" src='main.js'></script>
</head>
<body>
<header>
<input id="searchText" type="text" placeholder="Input search text" value="skyline">
<select id="sourceType">
<option value="flickr">Flickr</option>
<option value="pixabay">Pixabay</option>
<option value="pexels">Pexels</option>
</select>
<button id="searchImages">Search Images</button></header>
<main></main>
</body>
</html>
export const FLICKR_API_KEY = "abcdef123456789";
export const PIXABAY_API_KEY = "abcdef123456789";
export const PEXELS_API_KEY = "abcdef123456789";
import { Dataset } from "./datasets.js";
import * as Schemas from "./dataschemas.js";
import * as Keys from "./api_keys.js";
let myDataset;
let flickrSearchURL = "//api.flickr.com/services/rest/?method=flickr.photos.search&format=json&nojsoncallback=1&sort=interestingness-desc&per_page=5&page=1&extras=o_dims,url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o,description,geo,date_upload,date_taken,owner_name&per_page=25";
let pixabaySearchURL = "//pixabay.com/api/?image_type=photo&per_page=25";
let pexelsSearchURL = "//api.pexels.com/v1/search?per_page=25";
window.onload = () => {
document.querySelector("#searchImages").onclick = () => {
let queryString = document.querySelector("#searchText").value;
let sourceType = document.querySelector("#sourceType").value;
let searchUrl, options, schema;
switch ( sourceType ) {
case 'flickr':
searchUrl = `${flickrSearchURL}&api_key=${Keys.FLICKR_API_KEY}&text=${queryString}`;
schema = Schemas.FLICKR_SCHEMA;
options = {mode: "cors"};
break;
case 'pixabay':
searchUrl = `${pixabaySearchURL}&key=${Keys.PIXABAY_API_KEY}&q=${queryString}`;
schema = Schemas.PIXABAY_SCHEMA;
options = {mode: "cors"};
break;
case 'pexels':
searchUrl = `${pexelsSearchURL}&query=${queryString}`;
schema = Schemas.PEXELS_SCHEMA;
options = {headers: {Authorization: `${Keys.PEXELS_API_KEY}` }, mode: "cors"};
break;
}
fetch(searchUrl, options)
.then( response => response.json())
.then( json => {
console.log(json);
myDataset = new Dataset(json, schema);
myDataset.draw(document.querySelector("main"));
console.log(myDataset);
});
}
}
npm install http-server –g
To start the web server, we will use the command (you can specify any other port):
http-server -p 3000
Let's search on Flickr:
By adding the output of the response received from the Flickr service and the resulting dataset to the developer console, we can see how the results were converted to the desired format:
And for the Pexels service: