-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf97980
feat: add Puppeteer student discussion posts to Canvas
coledykstra 0a6a639
fix: remove debugging delay and non-headless mode
coledykstra ad41e66
chore: merge folder canvas-discussion-bot into canvas-seeder
coledykstra ad1c1e5
chore: remove canvas-discussion-bot
coledykstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
API_TOKEN=my_api_token | ||
CANVAS_URL=https://my.canvas.instance.edu | ||
TAB_FILE_PATH=./credentials.tab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.env | ||
credentials.tab | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 likeWhich I already don't like doing. Can I just merge both
.env
into the top level one?There was a problem hiding this comment.
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-seeder
directoryThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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