getClassData method
- dynamic cID
Récupère et lit la page d'un cours sur celene, retourne la liste des ressources disponibles sur cette page
Implementation
Future<List<Course>> getClassData(cID) async{
List<FileEntry> downloadedCourse = files.containsKey(cID) ? files[cID]! : [];
logger(files);
logger("Loaded downloaded courses");
List<Course> courses = [];
if (!loggedIn){
logger("Not logged in, need to log in to Celene");
bool result = await loginToCelene();
if (!result){
throw Exception("ERROR WHILE CONNECTING TO CELENE");
}
logger("Successfully logged in to Celene");
}
logger(_casAuth?.session.cookieStore.cookies);
if (_casAuth != null){
Uri classUrl = getClassUrl(cID);
logger("Now retrieving class data : class url is ${classUrl}");
//casAuth!.prepareRequest();
logger("CAS AUTH HEADERS");
Response classData;
try{
classData = await _casAuth!.session.get(classUrl, headers: _casAuth!.headers);
}
on ClientException{
classData = await _casAuth!.session.get(classUrl, headers: _casAuth!.headers);
}
logger("Get response finished");
if (classData.statusCode == 200){
logger("GET RESPONSE 200 -> Now parsing the page");
//casAuth!.session.updateCookies(classData);
//casAuth!.prepareRequest();
BeautifulSoup soup = BeautifulSoup(classData.body);
//logger(soup.prettify());
Bs4Element? topic = soup.find('ul', class_: 'topics');
List<Bs4Element> sections = soup.findAll("li", class_: "section course-section main");
for (Bs4Element i in sections){
logger("SectionName");
String? topic = i.find("h3", class_: "sectionname")?.text.trim();
List<Bs4Element> li_elements = i.findAll("li", class_:"activity activity-wrapper");
logger("Found ${li_elements.length} li_elements");
for (Bs4Element i in li_elements){
Course? newCourse = Course.constructFromCeleneInfo(i,parent: topic);
if (newCourse != null){
print("DOWNLOADED COURSES ARE ${downloadedCourse}");
FileEntry? associatedFile = downloadedCourse.where((e) => e.entryName == newCourse.name).toList().firstOrNull;
newCourse.downloaded = associatedFile != null;
newCourse.associatedFile = associatedFile;
if (newCourse.type == "Dossier" && newCourse.downloaded){
logger("The folder is downloaded so we have to add all files in this folder");
for (FileEntry j in (newCourse.associatedFile!.children)!){
logger("Adding subCourse");
Course subCourse = Course.constructFromFileInfo(j,topic: topic,parentFolder: newCourse.name);
subCourse.setFile(j);
courses.add(subCourse);
}
}
else{
courses.add(newCourse);
}
}
}
}
}
return courses;
}
return courses;
}