RandomFavoritePhoto 2
@raymondcamden
code:
data:privatelast updated:2 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
favicon_end
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_photos)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
}
3
if(steps.trigger.event.url.indexOf('favicon.ico') > 0) $end("favicon");
steps.
get_favorites_album
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_photos)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
}
20

if($checkpoint && $checkpoint.favoriteAlbum) return $checkpoint.favoriteAlbum;

let result = await require("@pipedreamhq/platform").axios(this, {
  url: 'https://photoslibrary.googleapis.com/v1/albums',
  headers: {
    Authorization: `Bearer ${auths.google_photos.oauth_access_token}`,
  },
});

let favorite = result.albums.find(a => {
  return a.title === 'Favorites';
});

if(!$checkpoint) $checkpoint = { };
$checkpoint.favoriteAlbum = favorite.id;

return favorite.id;
steps.
get_photos
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_photos)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
}
33

/*
Fetch photos once every six hours
changed to 1 on 5/19/2022
*/
let cacheDuration = 1 * 60 * 60 * 1000;
let now = Date.now();

if($checkpoint && $checkpoint.photoCacheTime && (now - $checkpoint.photoCacheTime < cacheDuration) && $checkpoint.photoCache) return $checkpoint.photoCache;
console.log('not cached'); 

let result = await require("@pipedreamhq/platform").axios(this, {
  url: 'https://photoslibrary.googleapis.com/v1/mediaItems:search',
  headers: {
    Authorization: `Bearer ${auths.google_photos.oauth_access_token}`,
  },
  method:'post',
  data: {
    albumId:steps.get_favorites_album.$return_value,
    pageSize:100
  }
});

// cache baseUrl
let photos = result.mediaItems.map(m => m.baseUrl);
$checkpoint.photoCacheTime = now;
$checkpoint.photoCache = photos;
console.log('stored cache time of ', $checkpoint.photoCacheTime);

return photos;
steps.
select_photo
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
}
12

// goes from min to max-1
const getRandomInt = function(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

return steps.get_photos.$return_value[getRandomInt(0, steps.get_photos.$return_value.length)];
steps.
get_photo
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_photos)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
}
18

const result = await require("@pipedreamhq/platform").axios(this, {
  url: steps.select_photo.$return_value,
  headers: {
    Authorization: `Bearer ${auths.google_photos.oauth_access_token}`,
  },
  responseType:'arraybuffer'
});
console.log('got here ok');
await $respond({
  status:200,
  headers: {
    'Content-Type':'image/jpeg'
  },
  body:result
})