diff --git a/CHANGELOG.md b/CHANGELOG.md index 06b13ca..8a51ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.0.0] - 2024-08-13 +### Springshare LibInsight Dataset Downloader +- Update script to download dataset if less then 10000 records. +- Add script to queue the export if more than 10000 records. +- Update blacklist. + ## [3.0.0] - 2022-12-14 ### SharePoint Permission Link - Add userscript that adds a site permission link on the right ribbon of SharePoint sites. diff --git a/README.md b/README.md index 41ca6c2..33f5a7c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Code Repository: https://github.com/gsu-library/userscripts Author: Matt Brooks Date Created: 2021-05-19 License: [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html) -Version: 3.0.0 +Version: 4.0.0 ## Description UserScripts used by GSU Library. diff --git a/springshare-libinsight-dataset-downloader.js b/springshare-libinsight-dataset-downloader.js index 01bc4b0..8991c30 100644 --- a/springshare-libinsight-dataset-downloader.js +++ b/springshare-libinsight-dataset-downloader.js @@ -3,7 +3,7 @@ // @namespace https://github.com/gsu-library/ // @author mbrooks34@gsu.edu // @license GPLv3 -// @version 2.0.0 +// @version 3.0.0 // @description Adds a download all reports item to the admin menu on the datasets page in LibInsight Lite. // @match https://gsu.libinsight.com/admin/all // @grant none @@ -14,76 +14,114 @@ // ==/UserScript== (() => { - 'use strict'; + "use strict"; // Set this array to strings of the dataset IDs you do not want to download. const blacklist = [ - // '4698', - // '4878', - // '5011', - '7927', - '22125', - '22124', - '22126', - '22123', - '22122' + '4698', + '4878', + // '5011', + '7927', ]; + const maxRecords = 10000; let datasetsRows; let datasetCounts = []; - // Process dataset ids and counts. Add menu item to download reports. - if(datasetsRows = document.querySelectorAll('#all-datasets tr')) { - let dataRows = [].slice.call(datasetsRows, 2); + if ((datasetsRows = document.querySelectorAll("#all-datasets tr"))) { + let dataRows = [].slice.call(datasetsRows, 2); + + dataRows.forEach((row) => { + datasetCounts.push({ + id: row.childNodes[1].textContent, + count: row.childNodes[7].textContent.replace(",", ""), + }); + }); - dataRows.forEach(row => { - datasetCounts.push({ - id: row.childNodes[1].textContent, - count: row.childNodes[7].textContent.replace(',', '') - }); - }); + datasetCounts = datasetCounts.filter( + (dataset) => !blacklist.includes(dataset.id) + ); - datasetCounts = datasetCounts.filter(dataset => !blacklist.includes(dataset.id)); + // Create menu item. + let downloadLink = document.createElement("a"); + downloadLink.setAttribute("class", "dropdown-item"); + downloadLink.setAttribute("href", "#"); + downloadLink.text = "Download All Reports"; + let adminMenu = document + .querySelector("#li-admin-navbar-manage") + .parentNode.querySelector("div"); + adminMenu.appendChild(downloadLink); + // Download reports. + downloadLink.onclick = (e) => { + e.preventDefault(); + e.stopPropagation(); - // Create menu item. - let downloadLink = document.createElement('a'); - downloadLink.setAttribute('class', 'dropdown-item'); - downloadLink.setAttribute('href', '#'); - downloadLink.text = 'Download All Reports'; - let adminMenu = document.querySelector('#li-admin-navbar-manage').parentNode.querySelector('div'); - adminMenu.appendChild(downloadLink); + datasetCounts.forEach((dataset) => { + // Download report if number of records less than max records + if (dataset.count < maxRecords) { + downloadReport(dataset); + } + else // Queue report if number of records more than max records + { + queueReport(dataset); + } + }); + }; + } + function downloadReport(dataset) + { + let url = 'https://gsu.libinsight.com/admin/custom-dataset/' + dataset.id + '/export' + + '?filters[page]=1' + + '&iid=294' + + '&dataset_id=' + dataset.id + + '&dataset_type=1' + + '&user_level=1' + + '&total_count=' + dataset.count + + '&from=Jan 01 0000' + + '&to=Dec 31 9999' + + '&filters[day_limit]=21' + + '&filters[hr_start]=0' + + '&filters[hr_end]=24' + + '&filters[sort]=date-desc'; + + setTimeout(() => { + let a = document.createElement('a'); + a.href = url; + document.body.appendChild(a); + a.setAttribute('target', '_blank'); + a.click(); + a.remove(); + }, 100); + } - // Download reports. - downloadLink.onclick = e => { - e.preventDefault(); - e.stopPropagation(); + function queueReport(dataset) + { + let url = "https://gsu.libinsight.com/admin/reports/" + dataset.id + "/add"; + let form = new FormData(); + form.set("name", "Dataset-" + dataset.id + "-" + new Date().toISOString()); + form.set("dataset_type", 1); + form.set("record_count", dataset.count); + form.set("date_range", "Custom Date Range"); - datasetCounts.forEach(dataset => { - let url = 'https://gsu.libinsight.com/admin/custom-dataset/' + dataset.id + '/export' + - '?filters[page]=1' + - '&iid=294' + - '&dataset_id=' + dataset.id + - '&dataset_type=1' + - '&user_level=1' + - '&total_count=' + dataset.count + - '&from=Jan 01 0000' + - '&to=Dec 31 9999' + - '&filters[day_limit]=21' + - '&filters[hr_start]=0' + - '&filters[hr_end]=24' + - '&filters[sort]=date-desc'; + let options = '?filters[page]=1' + + '&iid=294' + + '&dataset_id=' + dataset.id + + '&dataset_type=1' + + '&user_level=1' + + '&total_count=' + dataset.count + + '&from=Jan 01 0000' + + '&to=Dec 31 9999' + + '&filters[day_limit]=21' + + '&filters[hr_start]=0' + + '&filters[hr_end]=24' + + '&filters[sort]=date-desc'; + form.set("options", options); - setTimeout(() => { - let a = document.createElement('a'); - a.href = url; - document.body.appendChild(a); - a.setAttribute('target', '_blank'); - a.click(); - a.remove(); - }, 100); - }); - } + fetch(url, { + method: "POST", + body: form, + }); } -})(); +})(); \ No newline at end of file