Clean code

pull/75/head
MillenniumEarl 2021-03-04 11:41:50 +01:00
parent 2c4e63d97e
commit 75345718be
1 changed files with 13 additions and 10 deletions

View File

@ -17,31 +17,34 @@ import { urls } from "../constants/url.js";
*/ */
export default async function fetchLatestHandiworkURLs(query: LatestSearchQuery, limit: number = 30): Promise<string[]> { export default async function fetchLatestHandiworkURLs(query: LatestSearchQuery, limit: number = 30): Promise<string[]> {
// Local variables // Local variables
const shallowQuery: LatestSearchQuery = Object.assign(new LatestSearchQuery(), query);
const resultURLs = []; const resultURLs = [];
let fetchedResults = 0; let fetchedResults = 0;
let page = 1;
let noMorePages = false; let noMorePages = false;
do { do {
// Fetch the response (application/json) // Fetch the response (application/json)
const response = await query.execute(); const response = await shallowQuery.execute();
// Save the URLs // Save the URLs
if (response.isSuccess()) { if (response.isSuccess()) {
//@ts-ignore // In-loop variables
for (const result of response.value.data.msg.data) { const data: [{ thread_id: number}] = response.value.data.msg.data;
const totalPages: number = response.value.data.msg.pagination.total;
data.map((e, idx) => {
if (fetchedResults < limit) { if (fetchedResults < limit) {
const gameURL = new URL(result.thread_id, urls.F95_THREADS).href;
const gameURL = new URL(e.thread_id.toString(), urls.F95_THREADS).href;
resultURLs.push(gameURL); resultURLs.push(gameURL);
fetchedResults += 1; fetchedResults += 1;
} }
} });
// Increment page and check for it's existence // Increment page and check for it's existence
page += 1; shallowQuery.page += 1;
noMorePages = shallowQuery.page > totalPages;
//@ts-ignore
if (page > response.value.data.msg.pagination.total) noMorePages = true;
} else throw response.value; } else throw response.value;
} }
while (fetchedResults < limit && !noMorePages); while (fetchedResults < limit && !noMorePages);