const axios = require('axios'); const freepik = { search: async (q) => { try { if (!q) throw new Error('Query is required'); const { data } = await axios.get(`https://www.freepik.com/api/regular/search?filters[ai-generated][excluded]=1&filters[content_type]=photo&locale=en&page=${Math.floor(Math.random() * 100) + 1}&term=${q}`, { headers: { 'user-agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36' } }); return data.items.map(res => ({ title: res.name, type: res.type, is_premium: res.premium, is_aigenerated: res.isAIGenerated, author: { name: res.author.name, avatar: res.author.avatar, url: `https://www.freepik.com/author/${res.author.slug}` }, previewUrl: res.preview.url, url: res.url })); } catch (error) { throw new Error(error.message); } }, detail: async (url) => { try { const id = url.match(/_(\d+)\.htm$/)[1]; if (!id) throw new Error('Invalid url'); const { data } = await axios.get(`https://www.freepik.com/api/resources/${id}?locale=en`, { headers: { 'user-agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36' } }); const d = new Date(data.created); return { title: data.name, type: data.type, mimetype: data.encodingFormat, is_premium: data.premium, is_aigenerated: data.isAIGenerated, relatedTags: data.relatedTags.map(tag => tag.name), author: { name: data.author.name, avatar: data.author.avatar, url: `https://www.freepik.com/author/${data.author.slug}` }, previewUrl: data.preview.url, licenseUrl: data.license, created: `${d.getDate().toString().padStart(2,'0')}-${(d.getMonth()+1).toString().padStart(2,'0')}-${d.getFullYear()} ${d.getHours().toString().padStart(2,'0')}:${d.getMinutes().toString().padStart(2,'0')}` }; } catch (error) { throw new Error(error.message); } }, download: async (url) => { try { const id = url.match(/_(\d+)\.htm$/)[1]; if (!id) throw new Error('Invalid url'); const { data } = await axios.get(`https://www.freepik.com/api/regular/download?resource=${id}&action=download&locale=en`, { headers: { 'user-agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36' } }); return data; } catch (error) { throw new Error(error.message); } } } // Usage: const resp = await freepik.search('field'); console.log(resp);