-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraperFunctions.py
95 lines (76 loc) · 3.23 KB
/
scraperFunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from Models import Course, Document
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as E
import time
from BeautifulSoup import BeautifulSoup
import re
def login(driver, user, passw):
driver.get("https://mymcgill.mcgill.ca/portal/page/portal/Login")
driver.find_element_by_id('username').send_keys(user)
driver.find_element_by_id('password').send_keys(passw, Keys.RETURN)
driver.find_element_by_link_text('Access myCourses').click()
driver.switch_to_window(driver.window_handles[1])
del driver.window_handles[0]
##assert(LogedIn!)
def getCourses(driver,semester):
###goes to the main page of my courses and return all classes that have the semester passed in their link as a course obkect
time.sleep(3)
courses = []
links = driver.find_elements_by_partial_link_text(semester)
for link in links:
title = link.get_attribute("text")
x = Course(semester, title, link.get_attribute("href"), link)
courses.append(x)
return courses
def getAllDocuments(driver, course):
driver.get(course.link)
toRetFolders = []
driver.find_element_by_link_text('Content').click()
driver.find_element_by_id('TreeItemTOC').click()
## load button does not exit always and does not always apear when it should....
## jankey code i know
try:
loadMoreButtons = driver.find_element_by_partial_link_text('Load More').click()
for button in loadMoreButtons:
button.click()
except:
pass
return scrapeDocuments(driver.page_source,course.title)
def match_class(target):
target = target.split()
def do_match(tag):
try:
classes = dict(tag.attrs)["class"]
except KeyError:
classes = ""
classes = classes.split()
return all(c in classes for c in target)
return do_match
def scrapeDocuments(html,courseTitle):
soup = BeautifulSoup(html)
list = soup.findAll(match_class("d2l-datalist"))
h2s = list[0].findAll("h2")
folders = list[0].findAll(match_class("d2l-collapsepane-content"))
documents = []
for folder, title in zip( folders, h2s):
links = folder.findAll('a')
for link in links:
if link.get('href') != "javascript:void(0);" and link.get('href') != None :
try:
idNumbers = [int(s) for s in link.get('href').split("/") if s.isdigit()]
documentLink = "https://mycourses2.mcgill.ca/d2l/le/content/{0}/topics/files/download/{1}/DirectFileTopicDownload".format(idNumbers[0],idNumbers[1])
documentType = docTypes[link.get('title').split(' - ')[-1]]
documents.append(Document(link.string, documentLink,documentType, title.string,courseTitle))
except:
print "Unknown Document Type Found"
return documents
### a Helper global dictonary for parsing the doc title to the appropriate extenssion
docTypes = {
'Adobe Acrobat Document': ".pdf",
'PowerPoint Presentation': ".ppt",
'Word Document': ".doc",
'Link Topic': "Link",
}