Skip to content

Instantly share code, notes, and snippets.

@annguyenwasd
Last active March 30, 2020 08:26
Show Gist options
  • Save annguyenwasd/2ee48a8e3a21169160103939a350ece6 to your computer and use it in GitHub Desktop.
Save annguyenwasd/2ee48a8e3a21169160103939a350ece6 to your computer and use it in GitHub Desktop.
(function() {
const hasCode = location.pathname.indexOf('/folder/') > -1;
if (!hasCode) {
alert('Not a Fshare folder, Please navigate to FShare folder');
return;
}
let code = location.pathname.replace('/folder/', '');
let getLastPage = last => Number(last.split("&page=")[1].split("&")[0]) + 1;
alert('Press OK to start download JSON file');
console.info('Starting download...');
function saveToFile(data) {
const url = URL.createObjectURL(new Blob([JSON.stringify(data)], {type: 'application/json'}));
const a = document.createElement('a');
console.log(url)
a.href = url;
a.download = `list-${Date.now()}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
function getFirstPage(code) {
const url = `https://www.fshare.vn/api/v3/files/folder?linkcode=${code}`;
return fetch(url)
.then(data => data.json())
.then(data => getLastPage(data._links.last));
}
function getData(code) {
return getFirstPage(code).then(lastPage =>
Promise.all(
Array.from({ length: lastPage })
.map(
(undifined, page) =>
`https://www.fshare.vn/api/v3/files/folder?linkcode=${code}&sort=type%2Cname&page=${page}&per-page=50`
)
.map(url => fetch(url))
)
.then(ress => Promise.all(ress.map(res => res.json())))
.then(data => {
let links = [];
let ids = {};
data.forEach(({ items }) => {
items.forEach(item => {
if (!ids[item.id]) {
ids[item.id] = 1;
const {id, name, size, linkcode} = item;
links.push({id, name, size, linkcode});
}
})
});
saveToFile(links);
})
);
}
getData(code);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment