Skip to content
Snippets Groups Projects
Commit 742d062a authored by Antoine Fontaine's avatar Antoine Fontaine :8ball:
Browse files

Cleanups

parent c24a8eff
Branches
Tags
No related merge requests found
......@@ -97,7 +97,7 @@ moodle_parse_folder_content (const gchar *html, gsize html_size, const gchar *mo
"main", strlen("main"),
NULL);
if(collection && collection->list && collection->length==1)
if(collection->length==1)
root_node = collection->list[0];
else
g_error ("weird result getting the folder container\n");
......@@ -108,27 +108,25 @@ moodle_parse_folder_content (const gchar *html, gsize html_size, const gchar *mo
MyHTML_TAG_A,
NULL);
if (!hrefs || !hrefs->list || !hrefs->length)
if (hrefs->length == 0)
g_error ("weird result getting folder's links\n");
resources = malloc (hrefs->length*sizeof(struct Resource)); // at most this size will be used.
resources = malloc (hrefs->length * sizeof (struct Resource)); // at most this size will be used.
size_t count=0;
enum resource_type type;
gchar *name=NULL;
gchar *href=NULL;
gchar *filename=NULL;
gchar *name=NULL, *href=NULL, *filename=NULL;
guint id;
for (size_t i=0; i<hrefs->length; i++) {
myhtml_tree_node_t *node = hrefs->list[i];
myhtml_tree_attr_t *attr = myhtml_node_attribute_first (node);
while (attr) {
// find the href attribute
for (myhtml_tree_attr_t *attr = myhtml_node_attribute_first (node); attr; attr = attr->next) {
if (strcmp (myhtml_attribute_key (attr, NULL), "href") == 0) {
href = g_strdup (myhtml_attribute_value (attr, NULL)); // found!
break;
}
attr = myhtml_attribute_next (attr);
}
if (href) {
type = read_link (href, &id, &filename);
if (node->child && node->child->next && node->child->next->child) {
......@@ -138,6 +136,7 @@ moodle_parse_folder_content (const gchar *html, gsize html_size, const gchar *mo
count++;
}
}
resources = realloc (resources, count * sizeof (struct Resource));
myhtml_collection_destroy (hrefs);
myhtml_tree_destroy (tree);
......@@ -168,120 +167,58 @@ moodle_parse_course_list (const gchar *html, gsize html_size, const gchar *filen
"coc-courselist", strlen("coc-courselist"),
NULL);
if(collection && collection->list && collection->length==1)
if (collection->length==1)
courses_node = collection->list[0];
else
g_error ("weird result getting the courses container\n");
myhtml_collection_destroy (collection);
/*hrefs = myhtml_get_nodes_by_attribute_value_begin (tree, NULL, courses_node, FALSE,
"href", sizeof("href"),
EPFL_COURSE_LINK, sizeof(EPFL_COURSE_LINK),
NULL);*/
hrefs = myhtml_get_nodes_by_tag_id_in_scope (tree, NULL,
courses_node,
MyHTML_TAG_A,
NULL);
if (!hrefs || !hrefs->list || !hrefs->length)
if (hrefs->length == 0)
g_error ("weird result getting courses links\n");
courses = malloc (hrefs->length*sizeof(struct Course)); // at most this size will be used.
courses = malloc (hrefs->length * sizeof (struct Course)); // at most this size will be used.
size_t count=0;
size_t size;
const char *string;
for (size_t i=0; i<hrefs->length; i++) {
myhtml_tree_attr_t* attr = myhtml_node_attribute_first (hrefs->list[i]);
string = NULL;
while (attr) {
// <a> must have an href
if (strcmp (myhtml_attribute_key (attr, NULL), "href") != 0) {
attr = myhtml_attribute_next (attr);
continue; // not the right attribute
for (myhtml_tree_attr_t *attr = myhtml_node_attribute_first (hrefs->list[i]); attr; attr = attr->next) {
if (strcmp (myhtml_attribute_key (attr, NULL), "href") == 0) {
// we found the href attribute
if (g_str_has_prefix (myhtml_attribute_value (attr, NULL), EPFL_COURSE_LINK))
string = myhtml_attribute_value (attr, &size); // yay!
break;
}
// and href musts start with …/course/view.php?id=
if (strncmp (myhtml_attribute_value (attr, NULL), EPFL_COURSE_LINK, strlen (EPFL_COURSE_LINK)) != 0)
break; // not an interesting link
string = myhtml_attribute_value (attr, &size); // yay!
attr = myhtml_attribute_next (attr);
}
if (string && size) {
// nice link, we add it!
//g_print ("url: %s\n", string);
courses[count].url = malloc (size+1);
strcpy (courses[count].url, string);
//courses[count].url[size] = '\0';
courses[count].url = g_strndup (string, size);
sscanf(string, EPFL_COURSE_LINK"%u", &(courses[count].id));
string = myhtml_node_text (myhtml_node_child (hrefs->list[i]), &size);
//g_print (", %s\n", string);
if (G_UNLIKELY (!string || !size)) {
g_print ("failed to get course name\n");
free (courses[count].url);
continue;
}
courses[count].name = malloc (size+1);
strcpy (courses[count].name, string);
courses[count].name = g_strndup (string, size);
count++;
}
}
courses = realloc (courses, sizeof(struct Course)*count);
courses = realloc (courses, sizeof (struct Course) * count);
myhtml_collection_destroy (hrefs);
myhtml_tree_destroy (tree);
/*
struct Course *cs = malloc(10*(sizeof(struct Course)));
cs[0].name = malloc (50*sizeof (char));
cs[1].name = malloc (50*sizeof (char));
cs[2].name = malloc (50*sizeof (char));
cs[3].name = malloc (50*sizeof (char));
cs[4].name = malloc (50*sizeof (char));
cs[5].name = malloc (50*sizeof (char));
cs[6].name = malloc (50*sizeof (char));
cs[7].name = malloc (50*sizeof (char));
cs[8].name = malloc (50*sizeof (char));
cs[9].name = malloc (50*sizeof (char));
cs[0].url = malloc (50*sizeof (char));
cs[1].url = malloc (50*sizeof (char));
cs[2].url = malloc (50*sizeof (char));
cs[3].url = malloc (50*sizeof (char));
cs[4].url = malloc (50*sizeof (char));
cs[5].url = malloc (50*sizeof (char));
cs[6].url = malloc (50*sizeof (char));
cs[7].url = malloc (50*sizeof (char));
cs[8].url = malloc (50*sizeof (char));
cs[9].url = malloc (50*sizeof (char));
strcpy(cs[0].name, "Électricité" );
strcpy(cs[1].name, "Analyse" );
strcpy(cs[2].name, "Construction Mécanique");
strcpy(cs[3].name, "Design of Experiment" );
strcpy(cs[4].name, "Électrotechnique" );
strcpy(cs[5].name, "Éléments de Machine" );
strcpy(cs[6].name, "Mobilité" );
strcpy(cs[7].name, "Matériaux" );
strcpy(cs[8].name, "Physique" );
strcpy(cs[9].name, "Programmation" );
strcpy(cs[0].url, "https://moodle.epfl.ch/course/view.php?id=14019");
strcpy(cs[1].url, "https://moodle.epfl.ch/course/view.php?id=14837");
strcpy(cs[2].url, "https://moodle.epfl.ch/course/view.php?id=15000");
strcpy(cs[3].url, "https://moodle.epfl.ch/course/view.php?id=15829");
strcpy(cs[4].url, "https://moodle.epfl.ch/course/view.php?id=15544");
strcpy(cs[5].url, "https://moodle.epfl.ch/course/view.php?id=13923");
strcpy(cs[6].url, "https://moodle.epfl.ch/course/view.php?id=14412");
strcpy(cs[7].url, "https://moodle.epfl.ch/course/view.php?id=15843");
strcpy(cs[8].url, "https://moodle.epfl.ch/course/view.php?id=13917");
strcpy(cs[9].url, "https://moodle.epfl.ch/course/view.php?id=15698");
cs[0].id = 14019;
cs[1].id = 14837;
*/
struct Courses courses_struct = {courses, count};
return courses_struct;
}
......@@ -290,16 +227,15 @@ moodle_parse_course_list (const gchar *html, gsize html_size, const gchar *filen
static struct Week
moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t *div)
{
myhtml_tree_node_t *element = div->child;
myhtml_tree_node_t *li;
guint resource_count = 0;
struct Resource *resources = NULL;
while (element) {
for (myhtml_tree_node_t *element = div->child; element; element = element->next) {
if (element->tag_id == MyHTML_TAG_UL) {
// files
resources = malloc (sizeof (struct Resource)*1000);
li = element->child;
while (li) {
resources = malloc (sizeof (struct Resource) * 1000);
for (li = element->child; li; li = li->next) {
gchar *name;
gchar *textname;
enum resource_type type;
......@@ -311,36 +247,22 @@ moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t *
if (!hrefs || !hrefs->length) {
// no <a> in this week
myhtml_collection_destroy (hrefs);
li = li->next;
continue;
}
myhtml_tree_node_t *node = hrefs->list[0];
myhtml_tree_attr_t *attr = myhtml_node_attribute_first (node);
const char *string, *href=NULL;
size_t size;
name = textname = NULL;
id = 0;
while (attr) {
// <a> must have an href
for (myhtml_tree_attr_t *attr = myhtml_node_attribute_first (node); attr; attr = attr->next) {
if (strcmp (myhtml_attribute_key (attr, NULL), "href") == 0) {
href = myhtml_attribute_value (attr, &size);
const char *href = myhtml_attribute_value (attr, NULL);
type = read_link (href, &id, NULL);
string = /*g_path_get_basename (*/href/*)*/;
size = strlen (string);
name = malloc (size+1);
strcpy (name, string);
name = g_strdup (/*g_path_get_basename (*/href/*)*/);
if (node->child && node->child->next && node->child->next->child) {
string = myhtml_node_text(node->child->next->child, &size);
textname = malloc (size+1);
strcpy(textname, string);
textname = g_strdup (myhtml_node_text(node->child->next->child, NULL));
}
break;
}
attr = myhtml_attribute_next (attr);
}
if (id && textname && name) {
......@@ -352,11 +274,9 @@ moodle_extract_week (gchar *week_name, myhtml_tree_t *tree, myhtml_tree_node_t *
}
myhtml_collection_destroy (hrefs);
li = li->next;
}
resources = realloc (resources, sizeof(struct Resource)*resource_count);
resources = realloc (resources, sizeof (struct Resource)*resource_count);
}
element = element->next;
}
struct Week week = {week_name, resource_count, resources};
......@@ -385,7 +305,7 @@ moodle_parse_course_content (const gchar *html, gsize html_size, const gchar *fi
"role", strlen("role"),
"main", strlen("main"),
NULL);
if(root_col && root_col->list && root_col->length==1)
if(root_col->length==1)
root_node = root_col->list[0];
else
g_error ("weird result getting the courses' root element\n");
......@@ -397,9 +317,7 @@ moodle_parse_course_content (const gchar *html, gsize html_size, const gchar *fi
"section-", strlen("section-"),
NULL);
if (!weeks_col || !weeks_col->list)
g_error ("weird result getting the course's weeks\n");
if (!weeks_col->length) {
if (weeks_col->length == 0) {
myhtml_collection_destroy (weeks_col);
g_warning ("%s seems to have no course section\n", filename);
struct CourseContent empty = {0, NULL};
......@@ -408,87 +326,31 @@ moodle_parse_course_content (const gchar *html, gsize html_size, const gchar *fi
week_count = weeks_col->length;
weeks = malloc (sizeof (struct Week)*(week_count+1));
weeks = malloc (sizeof (struct Week) * (week_count+1));
for (size_t i=0; i<week_count; i++) {
// each element contain several div. We need to find the right one
char* week_name = NULL;
{
myhtml_tree_attr_t* attr = myhtml_node_attribute_first (weeks_col->list[i]);
while (attr) {
if (strcmp(myhtml_attribute_key (attr, NULL), "aria-label") == 0) {
size_t size; const char* string;
string = myhtml_attribute_value (attr, &size);
week_name = malloc (size+1);
strcpy (week_name, string);
}
attr = attr->next;
} // while (attr)}
for (myhtml_tree_attr_t* attr = myhtml_node_attribute_first (weeks_col->list[i]); attr; attr = attr->next) {
if (strcmp(myhtml_attribute_key (attr, NULL), "aria-label") == 0) {
week_name = g_strdup (myhtml_attribute_value (attr, NULL));
break;
}
}
myhtml_tree_node_t *div = weeks_col->list[i]->child;
while (div) {
myhtml_tree_attr_t* attr = myhtml_node_attribute_first (div);
while (attr) {
for (myhtml_tree_node_t *div = weeks_col->list[i]->child; div; div = div->next) {
for (myhtml_tree_attr_t* attr = myhtml_node_attribute_first (div); attr; attr = attr->next) {
if (strcmp(myhtml_attribute_key (attr, NULL), "class") == 0
&& strcmp(myhtml_attribute_value (attr, NULL), "content") == 0) {
// we reached the actual week content !
weeks[i] = moodle_extract_week (week_name, tree, div);
}
attr = attr->next;
} // while (attr)
div = div->next;
}
}
} // foreach attr
} // foreach div
} // foreach week
myhtml_collection_destroy (weeks_col);
myhtml_tree_destroy (tree);
/*
struct Resource *fs = malloc(6*sizeof(struct DownloadableFile));
fs[0].name = malloc (50*sizeof (char));
fs[1].name = malloc (50*sizeof (char));
fs[2].name = malloc (50*sizeof (char));
fs[3].name = malloc (50*sizeof (char));
fs[4].name = malloc (50*sizeof (char));
fs[5].name = malloc (50*sizeof (char));
fs[0].url = malloc (50*sizeof (char));
fs[1].url = malloc (50*sizeof (char));
fs[2].url = malloc (50*sizeof (char));
fs[3].url = malloc (50*sizeof (char));
fs[4].url = malloc (50*sizeof (char));
fs[5].url = malloc (50*sizeof (char));
strcpy(fs[0].name, "Analyse court complet II");
strcpy(fs[1].name, "Série 1" );
strcpy(fs[2].name, "Série 1 — corrigé" );
strcpy(fs[3].name, "Série 2" );
strcpy(fs[4].name, "Série 2 — corrigé" );
strcpy(fs[5].name, "Maple file" );
strcpy(fs[0].url, "https://moodle.epfl.ch/course/view.php?id=14019");
strcpy(fs[1].url, "https://moodle.epfl.ch/course/view.php?id=14837");
strcpy(fs[2].url, "https://moodle.epfl.ch/course/view.php?id=15000");
strcpy(fs[3].url, "https://moodle.epfl.ch/course/view.php?id=15829");
strcpy(fs[4].url, "https://moodle.epfl.ch/course/view.php?id=15544");
strcpy(fs[5].url, "https://moodle.epfl.ch/course/view.php?id=13923");
struct Week ws[] =
{{0, 5, &fs[0]},
{1, 2, &fs[3]},
{2, 1, &fs[5]},
{3, 1, &fs[1]},
{4, 1, &fs[3]},
{5, 1, &fs[5]}};
weeks = malloc(6*sizeof(struct Week));
weeks[0] = ws[0];
weeks[1] = ws[1];
weeks[2] = ws[2];
weeks[3] = ws[3];
weeks[4] = ws[4];
weeks[5] = ws[5];
week_count = 6;
*/
struct CourseContent content = {week_count, weeks};
return content;
}
......
......@@ -228,9 +228,8 @@ moodle_provider_connect (GInputStream **response)
}
free (html);*/
if (strncmp (soup_uri_to_string (soup_message_get_uri (msg), FALSE),
EPFL_TEQUILA_LOGIN_ACTION,
strlen(EPFL_TEQUILA_LOGIN_ACTION)) == 0) {
if (g_str_has_prefix (soup_uri_to_string (soup_message_get_uri (msg), FALSE),
EPFL_TEQUILA_LOGIN_ACTION)) {
g_warning ("Did not redirect. This is due to an incorrect password, or to a bug.\n");
return MOODLE_ERROR_AUTH;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment