addtl commenting

pull/1/head
sloppyjosh 2024-02-23 01:21:27 -05:00
parent 6f4b05448b
commit 59a981c9f7
1 changed files with 24 additions and 5 deletions

View File

@ -2,31 +2,50 @@ import { AxiosInstance } from 'axios';
import SessionManager from '../session/SessionManager'; import SessionManager from '../session/SessionManager';
import { Comment } from '../models/Comment'; import { Comment } from '../models/Comment';
/**
* CommentFetcher is responsible for fetching comments from the r/Drama API.
* It utilizes a SessionManager instance to manage API requests with appropriate
* headers and configurations, including rate limiting and retries.
*/
export class CommentFetcher { export class CommentFetcher {
private axiosInstance: AxiosInstance; private axiosInstance: AxiosInstance;
private maxPages: number; private maxPages: number;
/**
* Creates an instance of CommentFetcher.
* @param {number} maxPages The maximum number of pages to fetch from the r/Drama API. Defaults to 10.
*/
constructor(maxPages: number = 10) { constructor(maxPages: number = 10) {
this.axiosInstance = SessionManager.getInstance().axiosInstance; this.axiosInstance = SessionManager.getInstance().axiosInstance;
this.maxPages = maxPages; this.maxPages = maxPages;
} }
/**
* Fetches comments from the r/Drama API across multiple pages, up to the specified maximum.
* Iterates through pages starting from the first page until the maximum page limit is reached
* or there are no more comments to fetch. Each page's fetch operation is timed for performance analysis.
*
* @returns {Promise<Comment[]>} A promise that resolves to an array of comments fetched from the API.
* @throws {Error} Throws an error if there is a failure in fetching comments from the API.
*/
async fetchComments(): Promise<Comment[]> { async fetchComments(): Promise<Comment[]> {
let comments: Comment[] = []; let comments: Comment[] = [];
for (let page = 1; page <= this.maxPages; page++) { for (let page = 1; page <= this.maxPages; page++) {
console.time(`page: ${page}`) console.time(`Fetching page: ${page}`);
try { try {
const response = await this.axiosInstance.get(`/comments?page=${page}`); const response = await this.axiosInstance.get(`/comments?page=${page}`);
comments = [...comments, ...response.data.data]; comments = [...comments, ...response.data.data];
console.timeEnd(`page: ${page}`) console.timeEnd(`Fetching page: ${page}`);
// Optional: Break early if the API finds existing comment // Set up logic for DB checks of existing comments
// Optional: Break early if the API returns an empty array (no more comments)
if (response.data.data.length === 0) break; if (response.data.data.length === 0) break;
} catch (error) { } catch (error) {
console.error(`Failed to fetch comments for page ${page}:`, error); console.error(`Failed to fetch comments for page ${page}:`, error);
console.timeEnd(`page: ${page}`) console.timeEnd(`Fetching page: ${page}`);
break; throw error; // Rethrow or handle as needed
} }
} }
return comments; return comments;
} }
} }