Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Puppeteer student discussion posts to Canvas #254

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions canvas-discussion-bot/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_TOKEN=my_api_token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this into the canvas-seeder directory so they can share the same .env file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't they be independent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's still a canvas seeder, we don't want to pollute the top level directory, plus it's the same exact .env file.

Copy link
Contributor Author

@coledykstra coledykstra Jun 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I just assume the other project is there? How do I change index.js to get that to work. Do I need something like

// Specify the path to the .env file for the local directory
const envFilePath = path.resolve(__dirname, '.env');

// Load the .env file
const result = dotenv.config({ path: envFilePath });

Which I already don't like doing. Can I just merge both .env into the top level one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just put all the files into the existing canvas-seederdirectory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you were saying to just move the single line
API_TOKEN=my_api_token
I can put the whole node.js project into the same folder as the Go project?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah definitely, just put it all in the 'canvas-seeder' directory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the other directory can be removed now that it's merged into canvas-seeder

CANVAS_URL=https://my.canvas.instance.edu
TAB_FILE_PATH=./credentials.tab
3 changes: 3 additions & 0 deletions canvas-discussion-bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
credentials.tab
node_modules
133 changes: 133 additions & 0 deletions canvas-discussion-bot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const axios = require('axios');
const dotenv = require('dotenv');
const { faker } = require('@faker-js/faker');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');

async function retrieveCourses(apiToken, canvasURL) {
try {
const endPoint = "/api/v1/accounts/self/courses";
const url = canvasURL + endPoint;

const response = await axios.get(url, {
headers: {
'Authorization': `Bearer ${apiToken}`
}
});

return response.data;
} catch (error) {
throw new Error(`Error retrieving courses: ${error.message}`);
}
}

// Specify the path to the .env file for the local directory
const envFilePath = path.resolve(__dirname, '.env');

// Load the .env file
const result = dotenv.config({ path: envFilePath });

if (result.error) {
console.error('Error loading .env file:', result.error);
} else {
console.log('Successfully loaded .env file');
}

async function postDiscussion(username, password, courseId, index) {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
console.log('postDiscussion Course Id:', courseId);
try {
// Navigate to the Canvas login page
await page.goto(process.env.CANVAS_URL);

// Perform login
// Wait for the first textbox to become visible and type username
await page.waitForSelector('#pseudonym_session_unique_id_forgot');
await page.type('#pseudonym_session_unique_id_forgot', username);

// Wait for the second textbox to become visible and type password
await page.waitForSelector('#pseudonym_session_password');
await page.type('#pseudonym_session_password', password);

// Wait for the first button to become visible and click
await page.waitForSelector('.Button');
await page.click('.Button');
// Wait for navigation to complete
await page.waitForNavigation();


// Navigate to course and post discussion
const courseURL = `${process.env.CANVAS_URL}/courses/${courseId}/discussion_topics`;
await page.goto(courseURL);
await page.waitForSelector('#add_discussion');
await page.click('#add_discussion');

await page.waitForSelector('#discussion-title');
await page.type('#discussion-title', faker.lorem.sentence());

await page.waitForSelector('#discussion-topic-message2_ifr');
const iframeElement = await page.$('#discussion-topic-message2_ifr');
const iframe = await iframeElement.contentFrame();
await iframe.waitForSelector('body');
await iframe.type('body', faker.lorem.sentences(8));

// Click the "Save" button
await page.waitForSelector('button.btn.btn-primary.submit_button');
await page.click('button.btn.btn-primary.submit_button');
console.log('Hit Save button')

} catch (error) {
console.error(`Failed to post discussion for user ${username}:`, error);
} finally {
// Close the browser
await browser.close();
}
}

function randomChoice(array) {
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
}

let courseIds = [];

retrieveCourses(process.env.API_TOKEN, process.env.CANVAS_URL)
.then(courses => {
for (let i = 0; i < courses.length; i++) {
const courseId = courses[i].id;
courseIds.push(courseId);
}
randomCourseId = randomChoice(courseIds);
console.log('Random Course Id:', randomCourseId);
// Read and parse the tab-delimited credentials file
tab_file = path.join(__dirname, process.env.TAB_FILE_PATH)
console.log(`Reading the file: ${tab_file}`);
fs.readFile(tab_file, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}

// Split the file content by lines
const lines = data.trim().split('\n');

// Process each line (skipping blank lines and comment lines)
lines.forEach((line, index) => {
line = line.trim();
if (line === '' || line.startsWith('#')) {
return;
}

// Split each line by tab to get username and password
const [username, password] = line.split('\t');
postDiscussion(username, password, randomCourseId, index + 1);
});

console.log('Tab-delimited text file successfully processed');
});
})
.catch(error => {
console.error('Error:', error.message);
});
Loading
Loading