Comment Posting Functionality

pull/1/head
sloppyjosh 2024-03-01 01:40:34 -05:00
parent 825cdc7008
commit a1d5f75d3c
6 changed files with 68 additions and 5 deletions

1
package-lock.json generated
View File

@ -9,6 +9,7 @@
"axios-request-throttle": "^1.0.0",
"axios-retry": "^4.0.0",
"dotenv": "^16.4.5",
"form-data": "^4.0.0",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"sqlite": "^5.1.1",

View File

@ -4,6 +4,7 @@
"axios-request-throttle": "^1.0.0",
"axios-retry": "^4.0.0",
"dotenv": "^16.4.5",
"form-data": "^4.0.0",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"sqlite": "^5.1.1",
@ -15,7 +16,7 @@
"typescript": "^5.3.3"
},
"scripts": {
"build": "tsc && copyfiles -u 3 \"src/db/migrations/*.sql\" dist/db/migrations && copyfiles -u 3 \"src/db/seed/*.sql\" dist/db/seed",
"build": "tsc && copyfiles -u 3 \"src/db/migrations/*.sql\" dist/db/migrations && copyfiles -u 3 \"src/db/seed/*.sql\" dist/db/seed && copyfiles -u 2 \"src/messages/*.txt\" dist/messages",
"start": "node dist/index.js"
}
}
}

View File

@ -8,6 +8,8 @@ import { DatabaseInitializer } from './db/initializeDatabase';
import { DatabaseService } from './db/services/Database';
import { DatabaseMaintenanceService } from './db/services/DatabaseMaintenance';
import { CommentProcessor } from './rdrama/services/CommentProcessor';
import { MessageService } from './utils/MessageService';
import { CommentPoster } from './rdrama/services/CommentPoster';
// Import other necessary services or configurations
async function startApplication() {
@ -31,11 +33,15 @@ async function startApplication() {
// Initialize services with any required dependencies
const commentFetcher = new CommentProcessor(databaseService);
const commentParser = new CommentParser();
const commentPoster = new CommentPoster()
const messageService = new MessageService();
// Initialize and start your workflow
const workflowOrchestrator = new WorkflowOrchestrator(
commentFetcher,
commentParser
commentParser,
commentPoster,
messageService,
);
await workflowOrchestrator.executeWorkflow();
}

View File

@ -0,0 +1,37 @@
import SessionManager from '../session/SessionManager';
import { Comment } from '../models/Comment';
import FormData from 'form-data';
export class CommentPoster {
private sessionManager: SessionManager;
constructor() {
this.sessionManager = SessionManager.getInstance();
}
/**
* Posts a comment as a reply to a given rdrama comment.
*
* @param parentId The ID of the parent comment to reply to. Expected format: 'c_{id}'.
* @param body The body of the comment to post.
* @returns A promise resolving to the Axios response.
*/
public async postComment(parentId: string, body: string): Promise<Comment> {
const formData = new FormData();
formData.append('parent_fullname', parentId);
formData.append('body', body);
try {
const response = await this.sessionManager.axiosInstance.post('/comment', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
console.log(`Comment posted successfully to ${parentId}`);
return response.data;
} catch (error) {
console.error(`Failed to post comment to ${parentId}:`, error);
throw error; // Rethrow for handling elsewhere
}
}
}

View File

@ -11,14 +11,14 @@ export class MessageService {
private loadMessages() {
try {
const redditMessagesPath = path.join(__dirname, 'messages', 'reddit_messages.txt');
const redditMessagesPath = path.join(__dirname, '..', 'messages', 'reddit_messages.txt');
this.redditMessages = fs.readFileSync(redditMessagesPath, 'utf-8').split('---END---').filter(line => line.trim());
} catch (error) {
console.error('Failed to load Reddit messages:', error);
}
try {
const rdramaMessagesPath = path.join(__dirname, 'messages', 'rdrama_messages.txt');
const rdramaMessagesPath = path.join(__dirname, '..', 'messages', 'rdrama_messages.txt');
this.rdramaMessages = fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim());
} catch (error) {
console.error('Failed to load rDrama messages:', error);

View File

@ -1,10 +1,14 @@
import { CommentProcessor } from "../rdrama/services/CommentProcessor";
import { CommentParser } from "../rdrama/services/CommentParser";
import { CommentPoster } from "../rdrama/services/CommentPoster";
import { MessageService } from "../utils/MessageService";
class WorkflowOrchestrator {
constructor(
private commentProcessor: CommentProcessor,
private commentParser: CommentParser,
private commentPoster: CommentPoster,
private messageService: MessageService,
//private redditNotifier: RedditNotifier // Handles notifications to Reddit users
) { }
@ -22,6 +26,20 @@ class WorkflowOrchestrator {
const uniqueUsernames = [...new Set(allUsernames)];
console.log(`Extracted ${uniqueUsernames.length} unique usernames`);
for (const comment of comments) {
const redditUsers = this.commentParser.extractUsernames(comment)
if (redditUsers.length === 0) continue
console.log('found:', redditUsers)
const placeholders = {
author_name: comment.author_name,
};
const commentResponse = this.messageService.getRandomRdramaMessage(placeholders)
const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, `##### TEST MESSAGE NO REDDITOR PINGED (YET...)\n${commentResponse}`)
//const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, ${commentResponse}`) //TODO make this live
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4))
//FOR now, only reply to one user and we would check against DB with a random interval, we should only message between 2-3 people daily?
}
//// Query user information based on usernames
//const userInfo = await this.databaseService.queryUsersInfo(uniqueUsernames);
//console.log(`Queried information for ${userInfo.length} users`);