nhentai-tools
Version:
A toolset to interact with the doujin site nhentai.net
56 lines (55 loc) • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.random = exports.remove = exports.add = exports.get = void 0;
const user_1 = require("./user");
const client_1 = require("./client");
/** Get a page of the current user's favorite galleries. */
const get = (page = 1) => {
if (!user_1.isLoggedIn)
throw user_1.notLoggedInError;
return client_1.client.get('/favorites/', { params: { page } })
.then(({ data }) => {
// grab each gallery's div, extract the id, and return it as an array of numbers
const result = [...data.matchAll(/<div class="gallery-favorite" data-id="(\d{1,6})">/g)].map(([, id]) => Number(id));
// check the pagination for the last page, then return the page number
const num_pages = Number(data.match(/<a href="\/favorites\/\?page=(\d+)" class="last">/)[1]);
// find the count of favorites, then return it
const total = Number(data.match(/<span class="count">\((\d+)\)<\/span>/)[1]);
return {
result,
num_pages,
per_page: result.length,
total,
};
});
};
exports.get = get;
/** Add a gallery to the current user's favorite galleries. */
const add = (id) => {
if (!user_1.isLoggedIn)
throw user_1.notLoggedInError;
return client_1.client.post(`/api/gallery/${id}/favorite`).then(({ data }) => data);
};
exports.add = add;
/** Remove a gallery from the current user's favorite galleries. */
const remove = (id) => {
if (!user_1.isLoggedIn)
throw user_1.notLoggedInError;
return client_1.client.post(`/api/gallery/${id}/unfavorite`).then(({ data }) => data);
};
exports.remove = remove;
/** Get a random gallery from the current user's favorite galleries. */
const random = () => {
if (!user_1.isLoggedIn)
throw user_1.notLoggedInError;
return client_1.client.head('/favorites/random', { validateStatus: status => status === 302 })
.then(({ headers: { location } }) => {
if (!location)
throw new Error('Location header is missing');
const id = location.split('/')[2];
if (!id || !Number(id))
throw new Error('ID is invalid');
return Number(id);
});
};
exports.random = random;