2021-10-17 10:37:48 +02:00
|
|
|
#include <stdio.h>
|
2021-10-18 23:05:15 +02:00
|
|
|
|
2021-10-17 10:37:48 +02:00
|
|
|
#include <libsoup/soup.h>
|
|
|
|
#include <libxml/HTMLparser.h>
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
|
2021-10-18 23:05:15 +02:00
|
|
|
#ifndef PCRE2_CODE_UNIT_WIDTH
|
|
|
|
#define PCRE2_CODE_UNIT_WIDTH 8
|
|
|
|
#include <pcre2.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <manga.h>
|
|
|
|
|
2021-10-22 22:03:05 +02:00
|
|
|
#define XML_COPY_NODE_RECURSIVE 2 | 1
|
|
|
|
|
2021-10-17 10:37:48 +02:00
|
|
|
const char *mangafox_url =
|
2021-10-22 22:03:05 +02:00
|
|
|
"https://mangafox.fun";
|
2021-10-21 22:18:44 +02:00
|
|
|
struct String {
|
2021-10-22 22:03:05 +02:00
|
|
|
char *content;
|
|
|
|
size_t size;
|
2021-10-21 22:18:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SplittedString {
|
2021-10-22 22:03:05 +02:00
|
|
|
struct String *substrings;
|
|
|
|
size_t n_strings;
|
2021-10-21 22:18:44 +02:00
|
|
|
};
|
2021-10-17 10:37:48 +02:00
|
|
|
|
|
|
|
struct Manga *
|
|
|
|
parse_main_mangafox_page (
|
2021-10-22 22:03:05 +02:00
|
|
|
const xmlDocPtr html_document,
|
|
|
|
const size_t *size);
|
2021-10-17 10:37:48 +02:00
|
|
|
xmlXPathObjectPtr
|
|
|
|
get_nodes_xpath_expression (
|
2021-10-22 22:03:05 +02:00
|
|
|
const xmlDocPtr document,
|
|
|
|
char *xpath);
|
2021-10-18 23:05:15 +02:00
|
|
|
char *
|
|
|
|
alloc_string(size_t len);
|
|
|
|
void
|
|
|
|
copy_substring(const char *origin, char *dest,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t dest_len, size_t start, size_t len);
|
2021-10-18 23:05:15 +02:00
|
|
|
void
|
|
|
|
print_classes (const char *class_attribute,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t class_attribute_size);
|
2021-10-18 23:05:15 +02:00
|
|
|
int
|
|
|
|
has_class (const char *class_attribute,
|
2021-10-22 22:03:05 +02:00
|
|
|
const char *class_to_check);
|
2021-10-21 22:18:44 +02:00
|
|
|
void
|
|
|
|
splitted_string_free (struct SplittedString *splitted_string);
|
2021-10-18 23:05:15 +02:00
|
|
|
|
2021-10-21 22:18:44 +02:00
|
|
|
struct SplittedString *
|
|
|
|
split(char *re_str, size_t re_str_size, const char *subject,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t subject_size);
|
2021-10-21 22:18:44 +02:00
|
|
|
void
|
|
|
|
iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re, int *will_break, const char *subject,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t subject_size, size_t *start_pos, size_t *offset);
|
2021-10-17 19:41:52 +02:00
|
|
|
char *
|
|
|
|
get_request (const char *url, gsize *size_response_text);
|
2021-10-22 22:03:05 +02:00
|
|
|
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);
|
2021-10-23 04:47:21 +02:00
|
|
|
char *
|
|
|
|
get_attr (xmlNodePtr const node, const char *attr_name);
|
|
|
|
char *
|
|
|
|
get_manga_slide_cover(xmlNodePtr node);
|
|
|
|
char *
|
|
|
|
match_1 (char *re_str, char *subject);
|
|
|
|
|
2021-10-17 10:37:48 +02:00
|
|
|
void
|
2021-10-17 19:41:52 +02:00
|
|
|
retrieve_mangafox_title () {
|
2021-10-22 22:03:05 +02:00
|
|
|
xmlDocPtr html_response;
|
|
|
|
gsize size_response_text;
|
|
|
|
char *response_text = get_request (mangafox_url,
|
|
|
|
&size_response_text);
|
|
|
|
html_response = htmlReadMemory (response_text,
|
|
|
|
size_response_text,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
HTML_PARSE_RECOVER | HTML_PARSE_NODEFDTD
|
|
|
|
| HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING
|
|
|
|
);
|
|
|
|
size_t manga_size;
|
|
|
|
parse_main_mangafox_page (html_response, &manga_size);
|
|
|
|
xmlFreeDoc (html_response);
|
|
|
|
free (response_text);
|
2021-10-17 19:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
get_request (const char *url, gsize *size_response_text) {
|
2021-10-22 22:03:05 +02:00
|
|
|
SoupSession *soup_session;
|
|
|
|
SoupMessage *msg;
|
|
|
|
GValue response = G_VALUE_INIT;
|
|
|
|
guint status;
|
|
|
|
|
|
|
|
*size_response_text = 0;
|
|
|
|
|
|
|
|
g_value_init (&response, G_TYPE_BYTES);
|
|
|
|
|
|
|
|
soup_session = soup_session_new ();
|
|
|
|
msg = soup_message_new ("GET", url);
|
|
|
|
status = soup_session_send_message (soup_session, msg);
|
|
|
|
g_object_get_property(
|
|
|
|
G_OBJECT (msg),
|
|
|
|
"response-body-data",
|
|
|
|
&response);
|
|
|
|
|
|
|
|
printf("%u\n", status);
|
|
|
|
const char *html_response = g_bytes_get_data ((GBytes *)
|
|
|
|
g_value_peek_pointer (&response),
|
|
|
|
size_response_text);
|
|
|
|
|
|
|
|
char *return_value = g_strndup (html_response, *size_response_text);
|
|
|
|
|
|
|
|
g_value_unset (&response);
|
|
|
|
g_object_unref (soup_session);
|
|
|
|
g_object_unref (msg);
|
|
|
|
|
|
|
|
return return_value;
|
2021-10-17 10:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Manga *
|
2021-10-17 19:41:52 +02:00
|
|
|
parse_main_mangafox_page (const xmlDocPtr html_document,
|
2021-10-22 22:03:05 +02:00
|
|
|
const size_t *size) {
|
|
|
|
xmlNodePtr *nodes;
|
2021-10-23 04:47:21 +02:00
|
|
|
xmlNodePtr node;
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t nodes_len = 0;
|
|
|
|
|
|
|
|
nodes = find_all_manga_slide (html_document, &nodes_len);
|
2021-10-23 04:47:21 +02:00
|
|
|
//print_debug_nodes (html_document, nodes, nodes_len);
|
|
|
|
for (int i = 0; i < nodes_len; i++) {
|
|
|
|
node = nodes[i];
|
|
|
|
char *cover = get_manga_slide_cover(node);
|
|
|
|
if (cover) {
|
|
|
|
printf("%s\n", cover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
get_manga_slide_cover(xmlNodePtr node) {
|
|
|
|
for (xmlNodePtr child = node->children; child; child=child->next) {
|
|
|
|
char *attr = get_attr (child, "class");
|
|
|
|
if (attr && has_class (attr, "m-slide-background")) {
|
|
|
|
char *style = get_attr (child, "style");
|
|
|
|
char *match = match_1 ("background-image:url\\((.*?)\\)", style);
|
|
|
|
if (match) {
|
|
|
|
printf("%s\n", match);
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2021-10-22 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
goto cleanup_find_all_manga_slide;
|
|
|
|
}
|
|
|
|
node_set = xpath_result->nodesetval;
|
|
|
|
if (!node_set) {
|
|
|
|
fprintf(stderr, "No match\n");
|
|
|
|
goto cleanup_find_all_manga_slide;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < node_set->nodeNr; i++) {
|
|
|
|
xmlNodePtr node = node_set->nodeTab[i];
|
|
|
|
nodes = loop_search_class (node, nodes, "manga-slide", len);
|
|
|
|
}
|
|
|
|
cleanup_find_all_manga_slide:
|
|
|
|
xmlXPathFreeObject (xpath_result);
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:47:21 +02:00
|
|
|
char *
|
|
|
|
get_attr (xmlNodePtr const node, const char *attr_name) {
|
|
|
|
char *return_value = NULL;
|
2021-10-22 22:03:05 +02:00
|
|
|
for (xmlAttr *attr = node->properties; attr; attr=attr->next) {
|
2021-10-23 04:47:21 +02:00
|
|
|
if (!xmlStrcmp(attr->name, (const xmlChar *) attr_name)
|
2021-10-22 22:03:05 +02:00
|
|
|
&& attr->children && attr->children->content) {
|
2021-10-23 04:47:21 +02:00
|
|
|
if (!attr->children->content) continue;
|
|
|
|
size_t content_len = strlen((char *)
|
|
|
|
attr->children->content);
|
|
|
|
return_value = alloc_string(content_len);
|
|
|
|
copy_substring ((char *) attr->children->content, return_value,
|
|
|
|
content_len,
|
|
|
|
0,
|
|
|
|
content_len);
|
|
|
|
break;
|
2021-10-22 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-23 04:47:21 +02:00
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr *
|
|
|
|
loop_search_class (const xmlNodePtr node, xmlNodePtr *nodes,
|
|
|
|
const char * class, size_t *len) {
|
|
|
|
char *content = get_attr (node, "class");
|
|
|
|
if (!content) {
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
if (has_class (content, class)) {
|
|
|
|
(*len)++;
|
|
|
|
nodes = g_realloc (nodes, (sizeof *nodes) * *len);
|
|
|
|
nodes[(*len)-1] = xmlCopyNode(node, XML_COPY_NODE_RECURSIVE);
|
|
|
|
}
|
|
|
|
g_free (content);
|
2021-10-22 22:03:05 +02:00
|
|
|
return nodes;
|
2021-10-17 10:37:48 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 23:05:15 +02:00
|
|
|
int
|
|
|
|
has_class (const char *class_attribute,
|
2021-10-22 22:03:05 +02:00
|
|
|
const char *class_to_check) {
|
|
|
|
char *re = "\\s+";
|
|
|
|
struct SplittedString *classes;
|
|
|
|
int return_value = 0;
|
|
|
|
classes = split(re, strlen(re), class_attribute,
|
|
|
|
strlen(class_attribute));
|
|
|
|
for (int i = 0; i<classes->n_strings; i++) {
|
|
|
|
if (strcmp(classes->substrings[i].content, class_to_check) == 0) {
|
|
|
|
return_value = 1;
|
|
|
|
goto cleanup_has_class;
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 22:18:44 +02:00
|
|
|
|
|
|
|
cleanup_has_class:
|
2021-10-22 22:03:05 +02:00
|
|
|
splitted_string_free (classes);
|
|
|
|
return return_value;
|
2021-10-21 22:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
splitted_string_free (struct SplittedString *splitted_string) {
|
2021-10-22 22:03:05 +02:00
|
|
|
for (int i = 0; i<splitted_string->n_strings; i++) {
|
|
|
|
g_free (splitted_string->substrings[i].content);
|
|
|
|
}
|
2021-10-21 22:18:44 +02:00
|
|
|
|
2021-10-22 22:03:05 +02:00
|
|
|
g_free (splitted_string->substrings);
|
|
|
|
g_free (splitted_string);
|
2021-10-18 23:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SplittedString *
|
|
|
|
split(char *re_str, size_t re_str_size, const char *subject, size_t subject_size) {
|
2021-10-22 22:03:05 +02:00
|
|
|
pcre2_code_8 *re;
|
|
|
|
size_t start_pos = 0;
|
|
|
|
size_t offset = 0;
|
|
|
|
int regex_compile_error;
|
|
|
|
PCRE2_SIZE error_offset;
|
|
|
|
struct SplittedString *splitted_string;
|
|
|
|
|
|
|
|
splitted_string = g_malloc (sizeof *splitted_string);
|
|
|
|
|
|
|
|
splitted_string->n_strings = 0;
|
|
|
|
splitted_string->substrings = NULL;
|
|
|
|
re = pcre2_compile ((PCRE2_SPTR8) re_str,
|
|
|
|
re_str_size, 0, ®ex_compile_error, &error_offset, NULL);
|
|
|
|
while (start_pos < subject_size) {
|
|
|
|
int will_break = 0;
|
|
|
|
iterate_string_to_split(splitted_string, re, &will_break,
|
|
|
|
subject, subject_size, &start_pos, &offset);
|
|
|
|
if (will_break) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pcre2_code_free (re);
|
|
|
|
re = NULL;
|
|
|
|
|
|
|
|
return splitted_string;
|
2021-10-21 22:18:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:47:21 +02:00
|
|
|
char *
|
|
|
|
match_1 (char *re_str, char *subject) {
|
|
|
|
pcre2_code *re;
|
|
|
|
pcre2_match_data *match_data;
|
|
|
|
|
|
|
|
char *return_value;
|
|
|
|
int regex_compile_error;
|
|
|
|
int rc;
|
|
|
|
size_t len_match = 0;
|
|
|
|
|
|
|
|
return_value = NULL;
|
|
|
|
PCRE2_SIZE error_offset;
|
|
|
|
|
|
|
|
re = pcre2_compile ((PCRE2_SPTR8) re_str, strlen (re_str), 0,
|
|
|
|
®ex_compile_error, &error_offset, NULL);
|
|
|
|
match_data = pcre2_match_data_create_from_pattern (re, NULL);
|
|
|
|
rc = pcre2_match (re, (PCRE2_SPTR8) subject, strlen (subject),
|
|
|
|
0, 0, match_data, NULL);
|
|
|
|
if (rc < 0 ) {
|
|
|
|
goto cleanup_match;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcre2_substring_get_bynumber (match_data, 1, (PCRE2_UCHAR8**)
|
|
|
|
&return_value, &len_match);
|
|
|
|
cleanup_match:
|
|
|
|
pcre2_match_data_free (match_data);
|
|
|
|
pcre2_code_free (re);
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2021-10-21 22:18:44 +02:00
|
|
|
void
|
|
|
|
iterate_string_to_split(struct SplittedString *splitted_string, pcre2_code *re, int *will_break, const char *subject,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t subject_size, size_t *start_pos, size_t *offset) {
|
|
|
|
pcre2_match_data_8 *match_data;
|
|
|
|
PCRE2_SIZE *ovector;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
splitted_string->n_strings++;
|
|
|
|
match_data = pcre2_match_data_create_from_pattern_8 (re, NULL);
|
2021-10-23 04:47:21 +02:00
|
|
|
rc = pcre2_match ( re, (PCRE2_SPTR8) subject, subject_size, *start_pos, 0, match_data,
|
2021-10-22 22:03:05 +02:00
|
|
|
NULL);
|
|
|
|
if (splitted_string->substrings) {
|
|
|
|
splitted_string->substrings = g_realloc (splitted_string->substrings,
|
|
|
|
(sizeof *splitted_string->substrings) * (*offset + 1));
|
|
|
|
} else {
|
|
|
|
splitted_string->substrings = g_malloc (sizeof *splitted_string->substrings);
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
|
|
|
struct String *current_substring =
|
|
|
|
&splitted_string->substrings [*offset];
|
|
|
|
current_substring->content = alloc_string (subject_size
|
|
|
|
- *start_pos);
|
|
|
|
copy_substring (subject, current_substring->content,
|
|
|
|
subject_size,
|
|
|
|
*start_pos,
|
|
|
|
subject_size - *start_pos);
|
|
|
|
current_substring->size = subject_size - *start_pos;
|
|
|
|
|
|
|
|
*will_break = 1;
|
|
|
|
goto cleanup_iterate_string_to_split;
|
|
|
|
}
|
|
|
|
ovector = pcre2_get_ovector_pointer_8(match_data);
|
|
|
|
splitted_string->substrings[*offset].content = alloc_string (
|
|
|
|
ovector[0] - *start_pos);
|
|
|
|
copy_substring (subject, splitted_string->substrings[*offset]
|
|
|
|
.content,
|
|
|
|
subject_size,
|
|
|
|
*start_pos,
|
|
|
|
ovector[0] - *start_pos - 1);
|
|
|
|
splitted_string->substrings[*offset].size =
|
|
|
|
ovector[0] - *start_pos - 1;
|
|
|
|
|
|
|
|
*start_pos = ovector[1];
|
|
|
|
|
|
|
|
*offset += 1;
|
2021-10-21 22:18:44 +02:00
|
|
|
|
|
|
|
cleanup_iterate_string_to_split:
|
2021-10-22 22:03:05 +02:00
|
|
|
pcre2_match_data_free (match_data);
|
2021-10-18 23:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
alloc_string(size_t len) {
|
2021-10-23 04:47:21 +02:00
|
|
|
char * return_value = NULL;
|
2021-10-22 22:03:05 +02:00
|
|
|
return g_malloc (len + 1 * sizeof *return_value);
|
2021-10-18 23:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
copy_substring(const char *origin, char *dest, size_t dest_len, size_t start,
|
2021-10-22 22:03:05 +02:00
|
|
|
size_t len) {
|
|
|
|
size_t copying_offset = 0;
|
|
|
|
while (copying_offset < len) {
|
|
|
|
if (!(start+copying_offset <=dest_len)) {
|
|
|
|
fprintf(stderr, "Read attempt out of bounds.%ld %ld %ld\n", dest_len, start, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dest[copying_offset] = origin[start+copying_offset];
|
|
|
|
copying_offset++;
|
|
|
|
}
|
|
|
|
dest[len] = '\0';
|
2021-10-18 23:05:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:37:48 +02:00
|
|
|
xmlXPathObjectPtr
|
2021-10-17 19:41:52 +02:00
|
|
|
get_nodes_xpath_expression (const xmlDocPtr document, char *xpath) {
|
2021-10-22 22:03:05 +02:00
|
|
|
xmlXPathContextPtr context;
|
|
|
|
xmlXPathObjectPtr result;
|
2021-10-17 10:37:48 +02:00
|
|
|
|
2021-10-22 22:03:05 +02:00
|
|
|
context = xmlXPathNewContext (document);
|
|
|
|
result = xmlXPathEvalExpression ((const xmlChar *)xpath, context);
|
2021-10-21 22:18:44 +02:00
|
|
|
|
2021-10-22 22:03:05 +02:00
|
|
|
xmlXPathFreeContext (context);
|
2021-10-21 22:18:44 +02:00
|
|
|
|
2021-10-22 22:03:05 +02:00
|
|
|
return result;
|
2021-10-17 10:37:48 +02:00
|
|
|
}
|
|
|
|
|