Dirty initial commit with leaks everywhere.

This commit is contained in:
Sergio Iglesias 2021-10-17 10:37:48 +02:00
parent 754448f205
commit 9591489cd9
5 changed files with 181 additions and 0 deletions

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
CC := gcc
LIBS := libadwaita-1 gtk4 libsoup-2.4 libxml-2.0
INCDIR := -I ./include
CFLAGS := $(shell pkg-config --cflags ${LIBS}) -Wall
LDFLAGS := $(shell pkg-config --libs ${LIBS})
CC_COMMAND := ${CC} ${INCDIR} ${CFLAGS}
all: build
build:
${CC_COMMAND} mangafox.c main.c -o main ${LDFLAGS}

4
include/manga.h Normal file
View File

@ -0,0 +1,4 @@
struct Manga {
char *title;
char *image_url;
};

2
include/mangafox.h Normal file
View File

@ -0,0 +1,2 @@
void
retrieve_mangafox_title();

63
main.c Normal file
View File

@ -0,0 +1,63 @@
#include <gtk/gtk.h>
#include <adwaita.h>
#include <mangafox.h>
AdwHeaderBar *
create_headerbar (GtkBox *box);
GtkBox *
create_main_box (AdwApplicationWindow *window);
static void
activate (AdwApplication *app,
gpointer user_data)
{
GtkWidget *window =
adw_application_window_new (GTK_APPLICATION (app));
GtkBox *box = create_main_box(
ADW_APPLICATION_WINDOW
(window));
create_headerbar (box);
gtk_widget_show (window);
}
GtkBox *
create_main_box (AdwApplicationWindow *window) {
GtkWidget *box = gtk_box_new(
GTK_ORIENTATION_VERTICAL,
10);
adw_application_window_set_content(
window,
box);
return GTK_BOX (box);
}
AdwHeaderBar *
create_headerbar (GtkBox *box) {
GtkWidget *title =
adw_window_title_new ("Window", NULL);
GtkWidget *header =
adw_header_bar_new();
adw_header_bar_set_title_widget(
ADW_HEADER_BAR (header),
GTK_WIDGET (title));
gtk_box_append (GTK_BOX (box), header);
return ADW_HEADER_BAR (header);
}
int
main (int argc,
char **argv)
{
AdwApplication *app;
retrieve_mangafox_title();
int status;
app = adw_application_new ("org.mangareader", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}

103
mangafox.c Normal file
View File

@ -0,0 +1,103 @@
#include <stdio.h>
#include <libsoup/soup.h>
#include <manga.h>
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
const char *mangafox_url =
"https://mangafox.fun";
struct Manga *
parse_main_mangafox_page (
const xmlDocPtr html_document,
const size_t *size);
xmlXPathObjectPtr
get_nodes_xpath_expression (
const xmlDocPtr document,
char *xpath);
void
retrieve_mangafox_title() {
SoupSession
*soup_session;
SoupMessage *msg;
GValue response = G_VALUE_INIT;
guint status;
gsize size_response_text;
xmlDocPtr html_response;
g_value_init (&response, G_TYPE_BYTES);
soup_session =
soup_session_new();
msg =
soup_message_new(
"GET",
mangafox_url
);
status =
soup_session_send_message (soup_session, msg);
g_object_get_property(
G_OBJECT (msg),
"response-body-data",
&response);
const char *response_text =
g_bytes_get_data (
(GBytes *)
g_value_peek_pointer
(&response),
&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);
printf("%u\n", status);
}
struct Manga *
parse_main_mangafox_page (
const xmlDocPtr html_document,
const size_t *size) {
xmlIndentTreeOutput = 1;
// xmlDocDump (stderr, html_document);
xmlXPathObjectPtr xpath_result = get_nodes_xpath_expression (html_document,
"//a");
if (!xpath_result) {
fprintf(stderr, "Empty xpath result\n");
return NULL;
}
xmlNodeSetPtr node_set = xpath_result->nodesetval;
printf("%d\n", node_set->nodeNr);
for (int i = 0; i < node_set->nodeNr; i++) {
xmlNodePtr node = node_set->nodeTab[i];
for (xmlAttr *attrs = node->properties; attrs->next; attrs=attrs->next) {
if (!xmlStrcmp(attrs->name, (const xmlChar *)"href")) {
printf("%s\n", (const char *)attrs->children->content);
}
}
}
}
xmlXPathObjectPtr
get_nodes_xpath_expression (
const xmlDocPtr document,
char *xpath) {
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext (document);
if (!context) {
fprintf(stderr, "Error in xmlXpathNewContext\n");
return NULL;
}
result = xmlXPathEvalExpression ((const xmlChar *)xpath, context);
return result;
}