const axios = require('axios'); function formatDate(isoString) { const date = new Date(isoString); const day = String(date.getDate()).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0'); const year = date.getFullYear(); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${day}-${month}-${year} ${hours}:${minutes}`; } async function pexels(q, type) { try { const typeList = ['photos', 'videos']; if (!q) throw new Error('Query is required'); if (!typeList.includes(type)) throw new Error(`List available type: ${typeList.join(', ')}`); const { data } = await axios.get(`https://www.pexels.com/en-us/api/v3/search/${type}?query=${q}&page=${Math.floor(Math.random() * 100) + 1}&per_page=24&orientation=all&size=all&color=all&sort=popular&seo_tags=true`, { headers: { 'secret-key': 'H2jk9uKnhRmL6WPwh89zBezWvr' } }); return data.data.map(res => ({ title: res.attributes.title || 'No Title', ...(type === 'photos' ? { alt: res.attributes.alt } : {}), type: res.type, tags: res.attributes.tags.map(tag => tag.name), author: `${res.attributes.user.first_name.trim()}${res.attributes.user.last_name ? ` ${res.attributes.user.last_name.trim()}` : ''}`, license: res.attributes.license, ...(type === 'photos' ? { image: res.attributes.image } : { thumbnail: res.attributes.video.thumbnail, video_files: res.attributes.video.video_files.map(vid => ({ quality: vid.quality, link: vid.link, download_link: vid.download_link })) }), created_at: formatDate(res.attributes.created_at), updated_at: formatDate(res.attributes.updated_at), url: `https://www.pexels.com/${res.type}/${res.attributes.slug}-${res.id}/` })); } catch (error) { throw new Error(error.message); } } // Usage: const resp = await pexels('field', 'photos'); console.log(resp);