From 1ba6b1790ca0441ef3393cce48b091fc4d7ba557 Mon Sep 17 00:00:00 2001 From: sslinkyy Date: Tue, 11 Jun 2024 22:01:54 -0700 Subject: [PATCH] Add files via upload --- scripts/fetch_images.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 scripts/fetch_images.js diff --git a/scripts/fetch_images.js b/scripts/fetch_images.js new file mode 100644 index 0000000..a531bd6 --- /dev/null +++ b/scripts/fetch_images.js @@ -0,0 +1,29 @@ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); + +const ACCESS_TOKEN = process.env.INSTAGRAM_ACCESS_TOKEN; +const USER_ID = process.env.INSTAGRAM_USER_ID; + +const fetchImages = async () => { + try { + const response = await axios.get(`https://graph.instagram.com/${USER_ID}/media?fields=id,media_url,media_type,caption,timestamp&access_token=${ACCESS_TOKEN}`); + const images = response.data.data; + + images.forEach(image => { + if (image.media_type === 'IMAGE') { + const imagePath = path.join(__dirname, '..', 'images', `${image.id}.jpg`); + axios({ + url: image.media_url, + responseType: 'stream', + }).then(response => { + response.data.pipe(fs.createWriteStream(imagePath)); + }); + } + }); + } catch (error) { + console.error('Error fetching images:', error); + } +}; + +fetchImages();