forked from sergiotarxz/mangareader
Refactoring a little to make the code easier to change.
This commit is contained in:
parent
dab00a460f
commit
6dd5f63428
92
mangafox.c
92
mangafox.c
@ -11,8 +11,10 @@
|
||||
|
||||
#include <manga.h>
|
||||
|
||||
#define XML_COPY_NODE_RECURSIVE 2 | 1
|
||||
|
||||
const char *mangafox_url =
|
||||
"https://mangafox.fun";
|
||||
"https://mangafox.fun";
|
||||
struct String {
|
||||
char *content;
|
||||
size_t size;
|
||||
@ -41,7 +43,7 @@ print_classes (const char *class_attribute,
|
||||
size_t class_attribute_size);
|
||||
int
|
||||
has_class (const char *class_attribute,
|
||||
char *class_to_check);
|
||||
const char *class_to_check);
|
||||
void
|
||||
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);
|
||||
char *
|
||||
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
|
||||
retrieve_mangafox_title () {
|
||||
xmlDocPtr html_response;
|
||||
@ -110,37 +120,77 @@ get_request (const char *url, gsize *size_response_text) {
|
||||
struct Manga *
|
||||
parse_main_mangafox_page (const xmlDocPtr html_document,
|
||||
const size_t *size) {
|
||||
xmlIndentTreeOutput = 1;
|
||||
xmlXPathObjectPtr xpath_result = get_nodes_xpath_expression (html_document,
|
||||
xmlNodePtr *nodes;
|
||||
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]");
|
||||
|
||||
if (!xpath_result) {
|
||||
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) {
|
||||
fprintf(stderr, "No match\n");
|
||||
return NULL;
|
||||
goto cleanup_find_all_manga_slide;
|
||||
}
|
||||
for (int i = 0; i < node_set->nodeNr; i++) {
|
||||
xmlNodePtr node = node_set->nodeTab[i];
|
||||
for (xmlAttr *attrs = node->properties; attrs; attrs=attrs->next) {
|
||||
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);
|
||||
nodes = loop_search_class (node, nodes, "manga-slide", len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cleanup_find_all_manga_slide:
|
||||
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
|
||||
has_class (const char *class_attribute,
|
||||
char *class_to_check) {
|
||||
const char *class_to_check) {
|
||||
char *re = "\\s+";
|
||||
struct SplittedString *classes;
|
||||
int return_value = 0;
|
||||
@ -275,10 +325,6 @@ get_nodes_xpath_expression (const xmlDocPtr document, char *xpath) {
|
||||
xmlXPathObjectPtr result;
|
||||
|
||||
context = xmlXPathNewContext (document);
|
||||
if (!context) {
|
||||
fprintf(stderr, "Error in xmlXpathNewContext\n");
|
||||
return NULL;
|
||||
}
|
||||
result = xmlXPathEvalExpression ((const xmlChar *)xpath, context);
|
||||
|
||||
xmlXPathFreeContext (context);
|
||||
|
Loading…
Reference in New Issue
Block a user