Created
February 5, 2024 14:47
-
-
Save aaronjorbin/4ab7dda95b0532075fdff515dcf1994c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const baseURL = 'https://wordpress.org/photos/wp-json/wp/v2/photos'; | |
const params = { | |
after: '2024-02-01T00:00:00-04:00', | |
per_page: 100 | |
}; | |
async function fetchPhotosWithKeyword(keyword) { | |
try { | |
// Initial request to get total pages | |
const initialResponse = await axios.get(baseURL, { params }); | |
const totalPages = parseInt(initialResponse.headers['x-wp-totalpages'], 10); | |
const matchingIds = []; | |
// Process the first page | |
processPhotos(initialResponse.data, keyword, matchingIds); | |
// Process subsequent pages | |
for (let page = 2; page <= totalPages; page++) { | |
const response = await axios.get(baseURL, { params: { ...params, page } }); | |
processPhotos(response.data, keyword, matchingIds); | |
} | |
if ( matchingIds.length === 0 ) { | |
console.log('No photos found'); | |
return; | |
} | |
console.log('Total Photos: ', matchingIds.length); | |
for (let i = 0; i < matchingIds.length; i++) { | |
console.log(`https://wordpress.org/photos/photo/?p=${matchingIds[i]}`); | |
} | |
} catch (error) { | |
console.error('Error fetching photos:', error); | |
} | |
} | |
function processPhotos(photos, keyword, matchingIds) { | |
photos.forEach(photo => { | |
if (photo.content.rendered.toLowerCase().includes(keyword)) { | |
matchingIds.push(photo.id); | |
} | |
}); | |
} | |
// Replace 'wpkeralaphotos' with any keyword you're looking for | |
fetchPhotosWithKeyword('wpkeralaphotos'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "photo-wpkeralaphotos-alt", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^1.6.7" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment