Skip to content

Commit

Permalink
Merge pull request #5 from gsu-library/develop
Browse files Browse the repository at this point in the history
Update script to download/queue LibInsight dataset
  • Loading branch information
brooks1man authored Aug 13, 2024
2 parents 734526a + 1444078 commit 231aae2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 58 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Code Repository: https://github.com/gsu-library/userscripts
Author: Matt Brooks <[email protected]>
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.
152 changes: 95 additions & 57 deletions springshare-libinsight-dataset-downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @namespace https://github.com/gsu-library/
// @author [email protected]
// @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
Expand All @@ -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,
});
}
})();
})();

0 comments on commit 231aae2

Please sign in to comment.