Advertisement
RemcoE33

GET PHOTOS unsplash

Mar 12th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Get you're ACCESS_KEY here: https://unsplash.com/developers
  3. */
  4.  
  5. /**
  6. * Returns image url(s)
  7. *
  8. * @param {A1:A5} range - Input range.
  9. * @return {array} ImageUrls.
  10. * @customfunction
  11. */
  12. function GETPHOTOS(searchQuery) {
  13.   const ACCES_KEY = 'Nl-yyyyyy1wYTS0Laxxxxxx'
  14.   if (!Array.isArray(searchQuery)) {
  15.     searchQuery = [searchQuery]
  16.   }
  17.  
  18.   const urls = searchQuery.flat().map(q => {
  19.     return {
  20.       url: `https://api.unsplash.com/search/photos?per_page=1&query=${q}`,
  21.       headers: {
  22.         Authorization: `Client-ID ${ACCES_KEY}`
  23.       }
  24.     }
  25.   });
  26.  
  27.   const responses = UrlFetchApp.fetchAll(urls);
  28.   const imageUrls = responses.map(res => {
  29.     const json = JSON.parse(res.getContentText());
  30.     const imageUrl = json.results[0].urls.small;
  31.     return imageUrl
  32.   });
  33.  
  34.   return imageUrls
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement