WP Log Media

// Define the base URL for the media endpoint const baseUrl = 'https://online.denis_test.com.au/wp-json/wp/v2/media'; // Function to fetch media data with pagination async function fetchMedia(baseUrl) { let allData = []; let per_page = 100; //100 max let page = 1; const totalPages = 100; try { // Function to extract mime_type and source_url function extractMediaInfo(data) { return data.map(item => ({ mime_type: item.mime_type, filesize: item.media_details.filesize, source_url: item.source_url, link: item.link, date: item.date, id: item.id, link: item.link, modified: item.modified, slug: item.slug, //date_gmt: item.date_gmt, //guid: item.guid,//Object //modified_gmt: item.modified_gmt, //generated_slug: item.generated_slug, //status: item.status, //type: item.type, //permalink_template: item.permalink_template, //title: item.title,//Object //author: item.author, //comment_status: item.comment_status, //ping_status: item.ping_status, //meta: item.meta, //template: item.template, //associated_postid: item.post, //source_url: item.source_url })); } // Loop to fetch data from multiple pages while (page <= totalPages) { const response = await fetch(`${baseUrl}?page=${page}&per_page=${per_page}`); if (response.status == '400' ){ console.log('handle 400'); page = totalPages +1; break; } // Check if the response is ok (status code 200-299) if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } // Parse the JSON response const data = await response.json(); // Extract and accumulate data const extractedData = extractMediaInfo(data); allData = allData.concat(extractedData); // Log the data for each page console.log(`Page ${page} data:`, extractedData); if (data.length < per_page){ page = totalPages + 1; } // Increment the page number page++; } // Sort the accumulated data by file size in descending order allData.sort((a, b) => b.filesize - a.filesize); // Log the sorted data //console.log('All pages sorted data:', allData); console.table(allData); console.log(allData.length + ' records'); exportToCSV(allData, 'data.csv'); // Return the sorted data return allData; } catch (error) { // Handle any errors that occurred during the fetch console.error('Enable Permalinks or REST API in your WordPress setup ', error); } } function exportToCSV(arr, filename) { if (!arr.length) { return; } // Extract the headers from the keys of the first object const headers = Object.keys(arr[0]); const csvContent = [ headers.join(','), // Add the headers row ...arr.map(obj => headers.map(header => obj[header]).join(',')) ].join('\n'); // Create a Blob from the CSV string const blob = new Blob([csvContent], { type: 'text/csv' }); // Create a link element and set its href to the Blob URL const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = filename; // Append the link to the document, click it to trigger the download, and remove it document.body.appendChild(link); link.click(); document.body.removeChild(link); } // Call the function to fetch, sort, and log the media data fetchMedia();

Posted 20/04/2025 in Analytics , Javascript , WordPress by Denis Tags:

Posted 20/04/2025 in Analytics , Javascript , WordPress by Denis

Posted 20/04/2025 in Analytics , Javascript , WordPress

Analytics , Javascript , WordPress

Recommended articles