Compare commits

...

3 Commits

3 changed files with 25 additions and 10 deletions

View File

@ -1,22 +1,32 @@
import { Comment } from '../models/Comment'; import { Comment } from '../models/Comment';
/**
* A utility class for parsing Reddit comments.
*/
export class CommentParser { export class CommentParser {
/** /**
* Extracts Reddit usernames from the body of a single comment. * Extracts Reddit usernames from the body of a single comment.
* @param comment A single Comment object to be processed. * This method looks for patterns that match Reddit usernames, which start with "/u/" or "u/",
* @returns An array of unique Reddit usernames found in the comment. * and collects them into a Set to ensure uniqueness. It then returns an array of these unique usernames,
* all in lowercase to maintain consistency.
*
* @param {Comment} comment - A single Comment object to be processed.
* @returns {string[]} An array of unique Reddit usernames found in the comment.
*/ */
public static extractUsernames(comment: Comment): string[] { public static extractUsernames(comment: Comment): string[] {
const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_]+)/g; // Define the regex pattern to match Reddit usernames. It looks for the "/u/" prefix,
// optionally preceded by certain characters (like whitespace or punctuation),
// followed by the username consisting of alphanumeric characters and underscores.
const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_\-]+)/g;
const foundUsernames: Set<string> = new Set(); const foundUsernames: Set<string> = new Set();
const matches = comment.body.match(regexPattern); const matches = comment.body.match(regexPattern);
if (matches) { if (matches) {
matches.forEach(match => { matches.forEach(match => {
// Ensure the username is captured in a standardized format // Extract the username part from the match, ensuring it's in a consistent format.
const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/); const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_\-]+)/);
if (usernameMatch) { if (usernameMatch) {
// Standardize to "username" format // Convert the username to lowercase and add it to the Set to ensure uniqueness.
const username = `${usernameMatch[1].toLowerCase()}`; const username = `${usernameMatch[1].toLowerCase()}`;
foundUsernames.add(username); foundUsernames.add(username);
} }

View File

@ -26,7 +26,12 @@ class SessionManager {
enableOfflineQueue: true enableOfflineQueue: true
}, },
maxConcurrent: 1, // Maximum number of concurrent requests maxConcurrent: 1, // Maximum number of concurrent requests
minTime: 1000 // Minimum time between dispatches of requests in milliseconds minTime: 1000, // Minimum time between dispatches of requests in milliseconds
reservoir: 41, // Initial reservoir value for the first hour
reservoirRefreshAmount: 41, // Reservoir value to reset to every hour
reservoirRefreshInterval: 60 * 60 * 1000, // Interval to reset the reservoir (1 hour in milliseconds)
highWater: 5, // Maximum number of queued jobs.
strategy: Bottleneck.strategy.OVERFLOW_PRIORITY // Strategy to drop the oldest jobs in the queue when highWater is reached
}); });
this.axiosInstance = axios.create({ this.axiosInstance = axios.create({
@ -82,13 +87,13 @@ class SessionManager {
// Wrap the post method // Wrap the post method
const originalPost = instance.post; const originalPost = instance.post;
instance.post = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => { instance.post = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalPost.apply(instance, [url, data, config])) as Promise<R>; return this.limiter.schedule({ priority: 1 }, () => originalPost.apply(instance, [url, data, config])) as Promise<R>;
}; };
// Wrap the put method // Wrap the put method
const originalPut = instance.put; const originalPut = instance.put;
instance.put = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => { instance.put = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R> => {
return this.limiter.schedule(() => originalPut.apply(instance, [url, data, config])) as Promise<R>; return this.limiter.schedule({ priority: 1 }, () => originalPut.apply(instance, [url, data, config])) as Promise<R>;
}; };
// Wrap the delete method // Wrap the delete method

View File

@ -32,7 +32,7 @@ class WorkflowOrchestrator {
async processComment(comment: Comment) { async processComment(comment: Comment) {
const redditUsers = CommentParser.extractUsernames(comment); const redditUsers = CommentParser.extractUsernames(comment);
if (redditUsers.length === 0) return; if (redditUsers.length === 0) return;
console.log('found:', redditUsers); console.log(`${comment.permalink} found:`, redditUsers);
await this.postCommentAndNotify(comment, redditUsers[0]); await this.postCommentAndNotify(comment, redditUsers[0]);
} }