summaryrefslogtreecommitdiff
path: root/cite.c
blob: 6c9852b571689c052668e201208e6f50e95e3780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <curl/curl.h>
#include <libxml/parser.h>
#include <string.h>

typedef struct {
	int id;
	char title[256];
	char og_title[256];
	char directors[1024];
	char actors[1024];
	char prod_year[32];
	char release_date[32];
	char duration[32];
	char main_genres[256];
	char nationality[256];
	char pitch[2048];
	char poster[256];
	char week_times[4096];
	char times[4096];
} Movie;

Movie movies[64] = {0};
CURL *curl = NULL;

static void print_style(void)
{
	puts("<!DOCTYPE html>");
	puts("<html>");
	puts("<head>");
	puts("<meta charset=\"UTF-8\">");
	puts("<link rel='stylesheet' type='text/css' href='https://kdx.re/theme.css'/>");
	puts("</head>");
}

static void print_times(Movie *movie)
{
	printf("<ul>\n");
	for (const char *tok = strtok(movie->times, ";"); tok != NULL; tok = strtok(NULL, ";"))
		printf("<li>%s</li>\n", tok);
	printf("</ul>\n");
}

static void print_movie(Movie *movie, int hide_fr)
{
	if (hide_fr && strcmp(movie->nationality, "France") == 0)
		return;
	if (movie->og_title[0] != '\0')
		printf("<h2>%s</h2>\n", movie->og_title);
	else
		printf("<h2>%s</h2>\n", movie->title);
	printf("<a href=\"%s\"><img src=\"%s\" alt=\"affiche\" /></a>\n",
	       movie->poster, movie->poster);
	printf("<ul>\n");
	printf("<li>réalisateurs : %s</li>\n", movie->directors);
	printf("<li>acteurs : %s</li>\n", movie->actors);
	printf("<li>année de production : %s</li>\n", movie->prod_year);
	printf("<li>date de sortie : %s</li>\n", movie->release_date);
	printf("<li>durée : %s</li>\n", movie->duration);
	printf("<li>genres : %s</li>\n", movie->main_genres);
	printf("<li>nationalité : %s</li>\n", movie->nationality);
	//printf("synopsis : %s\n", movie->pitch);
	printf("<li>horaires :</li>\n");
	print_times(movie);
	printf("</ul>\n");
	printf("\n");
}

static void download_poster(Movie *movie)
{
	char out_path[512];
	char *last_slash = movie->poster;
	while (strchr(last_slash, '/') != NULL)
		last_slash = strchr(last_slash, '/') + 1;
	strcpy(out_path, "cite/");
	strcat(out_path, last_slash);
	FILE *const fp = fopen(out_path, "wb");
	if (fp == NULL)
		return;
	curl_easy_setopt(curl, CURLOPT_URL, movie->poster);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	fprintf(stderr, "getting %s", out_path);
	fprintf(stderr, "\rgot %s    \n", out_path);
	const CURLcode res = curl_easy_perform(curl);
	if (res == CURLE_OK)
		strcpy(movie->poster, last_slash);
}

static void xfree(const void *ptr)
{
	if (ptr != NULL)
		xmlFree((void *)ptr);
}

static int get_id(const xmlNode *node)
{
	const xmlChar *id = xmlGetProp(node, "id");
	if (id == NULL)
		return 0;
	const int v = atoi(id);
	xfree(id);
	return v;
}

static const xmlNode *get_movie_node(const xmlNode *node)
{
	while (node != NULL && strcmp((const char *)node->name, "film") != 0)
		node = node->children;
	return node;
}

static Movie *get_movie(int id)
{
	Movie *movie = movies;
	while (movie->id != id && movie->id != 0)
		movie += 1;
	return movie;
}

static int set_movie_base_fields(Movie *movie, const xmlNode *node)
{
	const xmlNode *times_node = node->children;
	if (times_node == NULL || times_node->next == NULL)
		return 1;
	int err = 0;
	const xmlChar *title = xmlGetProp(node, "titre");
	const xmlChar *og_title = xmlGetProp(node, "titreoriginal");
	const xmlChar *directors = xmlGetProp(node, "realisateurs");
	const xmlChar *actors = xmlGetProp(node, "acteurs");
	const xmlChar *prod_year = xmlGetProp(node, "anneeproduction");
	const xmlChar *release_date = xmlGetProp(node, "datesortie");
	const xmlChar *duration = xmlGetProp(node, "duree");
	const xmlChar *main_genres = xmlGetProp(node, "genreprincipal");
	const xmlChar *nationality = xmlGetProp(node, "nationalite");
	const xmlChar *pitch = xmlGetProp(node, "synopsis");
	const xmlChar *poster = xmlGetProp(node, "affichette");
	const xmlChar *week_times = xmlNodeGetContent(times_node);
	const xmlChar *times = xmlNodeGetContent(times_node->next);
	if (title == NULL || og_title == NULL || directors == NULL ||
	    actors == NULL || prod_year == NULL || release_date == NULL ||
	    duration == NULL || main_genres == NULL || nationality == NULL ||
	    pitch == NULL || poster == NULL || week_times == NULL ||
	    times == NULL) {
		err = 1;
		goto set_fields_panic;
	}
	strncpy(movie->title, title, sizeof(movie->title) - 1);
	strncpy(movie->og_title, og_title, sizeof(movie->title) - 1);
	strncpy(movie->directors, directors, sizeof(movie->directors) - 1);
	strncpy(movie->actors, actors, sizeof(movie->actors) - 1);
	strncpy(movie->prod_year, prod_year, sizeof(movie->prod_year) - 1);
	strncpy(movie->release_date, release_date, sizeof(movie->release_date) - 1);
	strncpy(movie->duration, duration, sizeof(movie->duration) - 1);
	strncpy(movie->main_genres, main_genres, sizeof(movie->main_genres) - 1);
	strncpy(movie->nationality, nationality, sizeof(movie->nationality) - 1);
	strncpy(movie->pitch, pitch, sizeof(movie->pitch) - 1);
	strncpy(movie->poster, poster, sizeof(movie->poster) - 1);
	strncpy(movie->week_times, week_times, sizeof(movie->week_times) - 1);
	strcpy(movie->times, "<b>");
	strncat(movie->times, times, sizeof(movie->times) - 1);
	strncat(movie->times, "</b>", sizeof(movie->times) - 1);
set_fields_panic:
	xfree(title);
	xfree(og_title);
	xfree(directors);
	xfree(actors);
	xfree(prod_year);
	xfree(release_date);
	xfree(duration);
	xfree(main_genres);
	xfree(nationality);
	xfree(pitch);
	xfree(poster);
	xfree(week_times);
	xfree(times);
	return err;
}

static int append_movie_times(Movie *movie, const xmlNode *node)
{
	const xmlNode *times_node = node->children;
	if (times_node == NULL || times_node->next == NULL)
		return 1;
	const xmlChar *week_times = xmlNodeGetContent(times_node);
	const xmlChar *times = xmlNodeGetContent(times_node->next);
	if (week_times == NULL || times == NULL) {
		xfree(week_times);
		xfree(times);
		return 1;
	}
	strncat(movie->week_times, "\n", sizeof(movie->week_times) - 1);
	strncat(movie->week_times, week_times, sizeof(movie->week_times) - 1);
	strncat(movie->times, ";", sizeof(movie->times) - 1);
	strncat(movie->times, times, sizeof(movie->times) - 1);
	xfree(week_times);
	xfree(times);
	return 0;
}

int main(int argc, char **argv)
{
	if (argc != 3)
		return 1;
	xmlDoc *const document = xmlReadFile(argv[1], NULL, 0);
	if (document == NULL) {
		xmlCleanupParser();
		return 1;
	}
	xmlNode *const root = xmlDocGetRootElement(document);
	if (root == NULL) {
		xmlCleanupParser();
		xmlFreeDoc(document);
		return 1;
	}
	curl = curl_easy_init();
	if (curl == NULL) {
		xmlCleanupParser();
		xmlFreeDoc(document);
		return 1;
	}
	for (const xmlNode *week = root->children; week != NULL; week = week->next) {
		for (const xmlNode *mov = get_movie_node(week); mov != NULL; mov = mov->next) {
			const int id = get_id(mov);
			if (id == 0)
				continue;
			Movie *movie = get_movie(id);
			if (movie->id == 0) {
				movie->id = id;
				set_movie_base_fields(movie, mov);
			} else
				append_movie_times(movie, mov);
		}
	}
	print_style();
	puts("<body>");
	puts("<h1><a href=\"http://www.citebd.org/spip.php?film2912\">cinéma de la cité</a></h1>");
	for (Movie *movie = movies; movie->id != 0; movie++) {
		download_poster(movie);
		print_movie(movie, atoi(argv[2]));
	}
	puts("</body>");
	puts("</html>");
	curl_easy_cleanup(curl);
	xmlFreeDoc(document);
	xmlCleanupParser();
	return 0;
}