Skip to content

Commit

Permalink
Documentation & CI
Browse files Browse the repository at this point in the history
Added comments to the test file and added self modifies to blackboard
scraper
  • Loading branch information
Freeman committed Jan 8, 2024
1 parent 492f618 commit 7ad9f01
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 51 deletions.
112 changes: 73 additions & 39 deletions backend/blackboard_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,46 @@ def shutdown(self):
logging.info("Session closed and deleted.")
else:
logging.warning("No active session to delete.")

def scrape(self):

if self.is_logged_in == False:
self.response = "Not logged in."
return

self.enable_instructors()
if self.get_InstructorsFound() == False:
self.response = "No instructors found."

self.get_courses()
if self.courseFound == False or len(self.courses) == 0:
self.response = "No courses found."
return

self.get_download_tasks()
if self.downloadTasksFound == False or len(self.download_tasks) == 0:
self.response = "Failed to get download tasks."
return

file_key = self.download_and_save_file()
if self.zipFound == False or file_key == None:
self.response = "Failed to download and save file."
return

self.response = None
self.last_activity_time = time.time()
return file_key

def login(self):
"""
Logs into blackboard using the username and password provided using
the requests library and saves the session cookies.
self modifies:
is_logged_in -- A boolean value indicating if the user is logged in.
last_activity_time -- The time of the last activity.
response -- The response of the login attempt.
"""
try:
Expand Down Expand Up @@ -158,36 +192,18 @@ def login(self):
except Exception as e:
logging.error(f"An error occurred during login: {e}")

def scrape(self):

if self.is_logged_in == False:
self.response = "Not logged in."
return

self.enable_instructors()
if self.get_InstructorsFound() == False:
self.response = "No instructors found."

self.get_courses()
if self.courseFound == False or len(self.courses) == 0:
self.response = "No courses found."
return

self.get_download_tasks()
if self.downloadTasksFound == False or len(self.download_tasks) == 0:
self.response = "Failed to get download tasks."
return

file_key = self.download_and_save_file()
if self.zipFound == False or file_key == None:
self.response = "Failed to download and save file."
return

self.response = None
self.last_activity_time = time.time()
return file_key

def enable_instructors(self):

"""
Enables instructors to be shown
self modifies:
instructorsFound -- A boolean value indicating if instructors were found.
last_activity_time -- The time of the last activity.
response -- The response of the enable instructors attempt.
"""

if self.is_logged_in == False:
self.response = "Not logged in."
Expand Down Expand Up @@ -270,19 +286,25 @@ def enable_instructors(self):
logging.error(f"An error occurred enabling instructors: {e}")

def get_courses(self):

if self.is_logged_in == False:
self.response = "Not logged in."
return


"""
Gets the courses the user is taking and stores in a dictionary
contained in the courses attribute. The key is the course name and
the value is the link to the course.
self modifies:
courses -- A dictionary of courses the user is taking.
courseFound -- A boolean value indicating if courses were found.
last_activity_time -- The time of the last activity.
response -- The response of the get courses attempt.
"""

if self.is_logged_in == False:
self.response = "Not logged in."
return

try:
form_data = {
'action': 'refreshAjaxModule',
Expand Down Expand Up @@ -361,6 +383,11 @@ def download_and_save_file(self):
"""
Downloads and saves the taks passed from the get dwonload tasks function.
self modifies:
zipFound -- A boolean value indicating if the zip file was found.
last_activity_time -- The time of the last activity.
response -- The response of the download and save file attempt.
"""

Expand Down Expand Up @@ -428,17 +455,24 @@ def download_task(task):
return os.path.relpath(zip_file_path, os.getcwd())

def get_download_tasks(self):

if self.is_logged_in == False:
self.response = "Not logged in."
return


"""
Gets a list of download tasks to be executed by collection all of the
"downlaodable" coneent from each course.
self modifies:
download_tasks -- A list of download tasks to be executed.
downloadTasksFound -- A boolean value indicating if download tasks were found.
last_activity_time -- The time of the last activity.
response -- The response of the get download tasks attempt.
"""

if self.is_logged_in == False:
self.response = "Not logged in."
return

download_tasks = []

hrefs = self.courses
Expand Down
75 changes: 63 additions & 12 deletions backend/test_blackboard_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,57 @@
from unittest.mock import patch
from usernames import usernames

""""
Test Case Senarios:
#* Login *#
- Valid credentials
- Invalid username
- Invalid password
- Invalid username and password
# TODO:
- Failed GET request
- Failed POST request
- HTML parsing failed
#* Enable Instructors *#
- Logged in
- Not logged in
- GET request failed
- POST request failed
# TODO:
- HTML parsing failed
- No instructors found
- Instructors found
#* Get Courses *#
- Logged in
- Not logged in
- No courses
- Error finding course list
# TODO:
- HTML parsing failed
#* Get Download Tasks *#
- Logged in
- Not logged in
# TODO:
- HTML parsing failed
"""


class TestBlackboardSession(unittest.TestCase):

Expand Down Expand Up @@ -364,20 +415,20 @@ def test_get_download_tasks_logged_in(self):
self.assertAlmostEqual(
session.last_activity_time, time.time(), delta=1)

def test_get_download_tasks_not_logged_in(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session.is_logged_in = False
def test_get_download_tasks_not_logged_in(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session.is_logged_in = False

# Execute get_download_tasks
session.get_download_tasks()
# Execute get_download_tasks
session.get_download_tasks()

# Check the response
self.assertEqual(session.response, "Not logged in.")
self.assertFalse(session.downloadTasksFound)
self.assertIsNone(session.last_activity_time)
# Check the response
self.assertEqual(session.response, "Not logged in.")
self.assertFalse(session.downloadTasksFound)
self.assertIsNone(session.last_activity_time)


if __name__ == '__main__':
Expand Down

0 comments on commit 7ad9f01

Please sign in to comment.