Refactoring a little to make the code easier to change.

This commit is contained in:
sergiotarxz 2021-10-22 22:03:05 +02:00
parent dab00a460f
commit 6dd5f63428

View File

@ -11,8 +11,10 @@
#include <manga.h> #include <manga.h>
#define XML_COPY_NODE_RECURSIVE 2 | 1
const char *mangafox_url = const char *mangafox_url =
"https://mangafox.fun"; "https://mangafox.fun";
struct String { struct String {
char *content; char *content;
size_t size; size_t size;
@ -41,7 +43,7 @@ print_classes (const char *class_attribute,
size_t class_attribute_size); size_t class_attribute_size);
int int
has_class (const char *class_attribute, has_class (const char *class_attribute,
char *class_to_check); const char *class_to_check);
void void
splitted_string_free (struct SplittedString *splitted_string); splitted_string_free (struct SplittedString *splitted_string);
@ -53,7 +55,15 @@ iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re,
size_t subject_size, size_t *start_pos, size_t *offset); size_t subject_size, size_t *start_pos, size_t *offset);
char * char *
get_request (const char *url, gsize *size_response_text); get_request (const char *url, gsize *size_response_text);
xmlNodePtr *
loop_search_class(const xmlNodePtr node, xmlNodePtr *nodes,
const char * class, size_t *len);
void
print_debug_nodes (const xmlDocPtr html_document,
xmlNodePtr *nodes, size_t nodes_len);
xmlNodePtr *
find_all_manga_slide(const xmlDocPtr html_document,
size_t *len);
void void
retrieve_mangafox_title () { retrieve_mangafox_title () {
xmlDocPtr html_response; xmlDocPtr html_response;
@ -110,37 +120,77 @@ get_request (const char *url, gsize *size_response_text) {
struct Manga * struct Manga *
parse_main_mangafox_page (const xmlDocPtr html_document, parse_main_mangafox_page (const xmlDocPtr html_document,
const size_t *size) { const size_t *size) {
xmlIndentTreeOutput = 1; xmlNodePtr *nodes;
xmlXPathObjectPtr xpath_result = get_nodes_xpath_expression (html_document, size_t nodes_len = 0;
nodes = find_all_manga_slide (html_document, &nodes_len);
print_debug_nodes (html_document, nodes, nodes_len);
}
void
print_debug_nodes (const xmlDocPtr html_document,
xmlNodePtr *nodes, size_t nodes_len) {
xmlBufferPtr buffer = xmlBufferCreate ();
for (int i = 0; i < nodes_len; i++) {
xmlNodeDump (buffer, html_document, nodes[i],
0, 1);
}
xmlBufferDump (stdout, buffer);
xmlBufferFree (buffer);
}
xmlNodePtr *
find_all_manga_slide(const xmlDocPtr html_document,
size_t *len) {
xmlNodeSetPtr node_set;
xmlNodePtr *nodes;
xmlXPathObjectPtr xpath_result;
node_set = NULL;
nodes = NULL;
xpath_result = get_nodes_xpath_expression (html_document,
"//div[@class]"); "//div[@class]");
if (!xpath_result) { if (!xpath_result) {
fprintf(stderr, "Empty xpath result\n"); fprintf(stderr, "Empty xpath result\n");
return NULL; goto cleanup_find_all_manga_slide;
} }
xmlNodeSetPtr node_set = xpath_result->nodesetval; node_set = xpath_result->nodesetval;
if (!node_set) { if (!node_set) {
fprintf(stderr, "No match\n"); fprintf(stderr, "No match\n");
return NULL; goto cleanup_find_all_manga_slide;
} }
for (int i = 0; i < node_set->nodeNr; i++) { for (int i = 0; i < node_set->nodeNr; i++) {
xmlNodePtr node = node_set->nodeTab[i]; xmlNodePtr node = node_set->nodeTab[i];
for (xmlAttr *attrs = node->properties; attrs; attrs=attrs->next) { nodes = loop_search_class (node, nodes, "manga-slide", len);
if (!xmlStrcmp(attrs->name, (const xmlChar *)"class")
&& attrs->children && attrs->children->content) {
const char *content = (char *) attrs->children->content;
if (has_class (content, "manga-slide")) {
printf("%s\n", content);
} }
} cleanup_find_all_manga_slide:
}
}
xmlXPathFreeObject (xpath_result); xmlXPathFreeObject (xpath_result);
return nodes;
}
xmlNodePtr *
loop_search_class(const xmlNodePtr node, xmlNodePtr *nodes,
const char * class, size_t *len) {
for (xmlAttr *attr = node->properties; attr; attr=attr->next) {
if (!xmlStrcmp(attr->name, (const xmlChar *)"class")
&& attr->children && attr->children->content) {
const char *content = (char *) attr->children->content;
if (has_class (content, class)) {
(*len)++;
nodes = g_realloc (nodes, (sizeof *nodes) * *len);
nodes[(*len)-1] = xmlCopyNode(node, XML_COPY_NODE_RECURSIVE);
}
}
}
return nodes;
} }
int int
has_class (const char *class_attribute, has_class (const char *class_attribute,
char *class_to_check) { const char *class_to_check) {
char *re = "\\s+"; char *re = "\\s+";
struct SplittedString *classes; struct SplittedString *classes;
int return_value = 0; int return_value = 0;
@ -275,10 +325,6 @@ get_nodes_xpath_expression (const xmlDocPtr document, char *xpath) {
xmlXPathObjectPtr result; xmlXPathObjectPtr result;
context = xmlXPathNewContext (document); context = xmlXPathNewContext (document);
if (!context) {
fprintf(stderr, "Error in xmlXpathNewContext\n");
return NULL;
}
result = xmlXPathEvalExpression ((const xmlChar *)xpath, context); result = xmlXPathEvalExpression ((const xmlChar *)xpath, context);
xmlXPathFreeContext (context); xmlXPathFreeContext (context);