aboutsummaryrefslogtreecommitdiff
path: root/c/data.cpp
blob: 32e059f2567fe5ff53eed30b5aed13d140fb30b9 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <thread>
#include <windows.h>
#include <wininet.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/xpath.h>

#include "data.h"
#include "episodelistview.h"
#include "err.h"
#include "res.h"
#include "util.h"
#include "win32.h"
#include "window.h"

using namespace std::string_literals;

UniqueOk<htmlParserCtxtPtr, xmlFreeParserCtxt> RemoteParserCtxt(const wchar_t* wszUrl, const char* szUrl)
{
	static Unique<HINTERNET, InternetCloseHandle> hi =
		InternetOpenW(L"Episode Browser", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, 0);
	if (hi.Bad(0))
		throw Err(WINDOWS, L"Internet handle could not be opened: %s");

	Unique<HINTERNET, InternetCloseHandle> hiUrl = InternetOpenUrlW(
	    hi.v, wszUrl, nullptr, 0, INTERNET_FLAG_NO_UI, 0);
	if (hiUrl.Bad(0))
		throw Err(WININET, L"Could not open "s + wszUrl + L": %s");

	char bufX[1024];
	Unique<htmlParserCtxtPtr, xmlFreeParserCtxt> ctx = htmlCreatePushParserCtxt(
	    nullptr, nullptr, bufX, sizeof(bufX), szUrl, XML_CHAR_ENCODING_UTF8);
	if (ctx.Bad(0))
		throw Err(LIBXML2, L"HTML parser context could not be created: %s");

	htmlCtxtUseOptions(ctx.v, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING);

	BOOL r;
	DWORD cbRead;
	char bufI[1024];
	while (r = InternetReadFile(hiUrl.v, bufI, sizeof(bufI), &cbRead), cbRead) {
		if (!r)
			throw Err(WININET, L"HTML could not be retrieved: %s");
		if (!htmlParseChunk(ctx.v, bufI, cbRead, 0))
			throw Err(LIBXML2, L"HTML could not be parsed: %s");
	}
	htmlParseChunk(ctx.v, bufI, 0, 1); /* Stop parsing. */

	return ctx;
}

static inline void XmlFree(void* p) { xmlFree(p); }

template <size_t N>
bool WcharsFromXmlchars(wchar_t (&dst)[N], Unique<xmlChar*, XmlFree> utf8)
{
	if (utf8.Bad(0))
		throw Err(LIBXML2, L"Node content could not be retrieved: %s");

	/* Truncate if source is larger than destination. */
	int lenUtf8 = xmlStrlen(utf8.v);
	utf8.v[Min(N, static_cast<size_t>(lenUtf8))] = 0;

	/* Convert internal representation from UTF-8 to Latin-1,
	 * which seems to actually convert the string to proper UTF-8
	 * (???). */
	unsigned char lat1[N];
	int lenLat1 = N-1;
	if (UTF8Toisolat1(lat1, &lenLat1, utf8.v, &lenUtf8) <= 0)
		return false;
	lat1[lenLat1] = 0;

	/* Write wide string to destination, if it fits. */
	char* const src = reinterpret_cast<char*>(lat1);
	const int cchNarrow = lenLat1+1;
	const int cchWide = MultiByteToWideChar(CP_UTF8, 0, src, cchNarrow, nullptr, 0);
	if (static_cast<size_t>(cchWide) > N)
		return false;
	return MultiByteToWideChar(CP_UTF8, 0, src, cchNarrow, dst, cchWide);
}

/* The Fetch* functions are run in a separate thread via WaitFor. The
 * main thread and the fetch thread communicate by setting flags on a
 * shared byte. At any given time, only a single fetch thread may be
 * performing work. */

enum Signal : unsigned char
{
	READY = 1<<0,	/* Main -> fetch: start working! */
	DONE = 1<<1,	/* Fetch -> main: work is done. */
	ABORT = 1<<2	/* Main -> fetch: exit prematurely! */
};

static Window* s_window;

void WaitFor(Window& window, void (*f)(unsigned char*))
{
	static unsigned char sig = READY;
	static UINT_PTR iTimer = 0;

	static auto procTimer = [](HWND, UINT, UINT_PTR, DWORD) -> void
	{
		static int i = 0;

		if (sig & DONE) {
			KillTimer(nullptr, iTimer);
			i = 0;
			sig = READY; /* Reset signals. */
			s_window->elv.Update(); /* Reset status bar. */
			EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED);
		} else {
			/* Animate ellipsis in status bar. */
			static const wchar_t* text[] = {L".", L"..", L"...", L""};
			i = (i+1)%(sizeof(text)/sizeof(*text));
			s_window->Status(text[i], 1);
		}
	};

	static auto procThread = [](void (*f)(unsigned char*)) noexcept -> void
	{
		std::set_terminate(OnTerminate);
		while (!(sig & READY))
			Sleep(100);
		sig = 0;
		EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_ENABLED);
		try {
			f(&sig);
			sig |= DONE;
		} catch (...) {
			sig |= DONE;
			EBMessageBox(What(), L"Remote Data Retrieval Error");
		}
	};

	/* Null indicates that any active task should be cancelled. */
	if (!f) {
		sig |= ABORT;
		EnableMenuItem(GetMenu(s_window->hWnd), IDM_FILE_FETCH_CANCEL, MF_GRAYED);
		return;
	}

	/* Ensure that only a single thread is waited on. */
	if (!(sig & READY)) {
		if (EBMessageBox(L"Another task is active. "
		    L"Do you want to cancel the existing task and start a new one?",
		    L"Error", MB_YESNO|MB_ICONWARNING) == IDYES)
			sig |= ABORT;
		else
			return;
	}

	s_window = &window;
	std::thread(procThread, f).detach();
	s_window->Status(L".", 1);
	if (!(iTimer = SetTimer(nullptr, iTimer, 500, procTimer)))
		throw Err(WINDOWS, L"Timer could not be started: %s");
}

void FetchData(unsigned char* sig)
{
	/* The remote data is retrieved using WinINet from the
	 * Detective Conan World wiki. Using libxml2's "push parser",
	 * the HTML is parsed piece by piece as it is retrieved. The
	 * episode data are contained in table rows matching a (very!)
	 * specific XPath query. This is fragile theoretically, but
	 * unlikely to break practically. */

	wchar_t url[_countof(s_window->cfg.prefixUrl)+46+1];
	Swprintf(url, L"%shttps://www.detectiveconanworld.com/wiki/Anime", s_window->cfg.prefixUrl);

	UniqueOk<htmlParserCtxtPtr, xmlFreeParserCtxt> ctx =
		RemoteParserCtxt(url, "https://www.detectiveconanworld.com/wiki/Anime");

	Unique<xmlXPathContextPtr, xmlXPathFreeContext> xpathCtx = xmlXPathNewContext(ctx.v->myDoc);
	if (xpathCtx.Bad(0))
		throw Err(LIBXML2, L"XPath context could not be created: %s");

	Unique<xmlXPathObjectPtr, xmlXPathFreeObject> xpathObj = xmlXPathEvalExpression(
	    reinterpret_cast<const xmlChar*>("//tr[./td[1] != '' and ./td[3][@style='background:#f2fde9;']]"),
	    xpathCtx.v);
	if (xpathObj.Bad(0))
		throw Err(LIBXML2, L"XPath object could not be created: %s");

	xmlNodeSetPtr nodes = xpathObj.v->nodesetval;

	if (!nodes || !nodes->nodeNr)
		throw Err(GENERIC, L"Data retrieval failed: No matching HTML nodes found.");

	for (int i = 0; i < nodes->nodeNr; i++) {
		if (*sig & ABORT)
			return;

		const xmlNodePtr node = nodes->nodeTab[i];
		if (xmlChildElementCount(node) != 8)
			throw Err(GENERIC, L"Data retrieval failed: Unexcepted number of columns in table.");

		ElvDataA& e = s_window->fvElv.At(i);
		DlvDataA& d = s_window->fvDlv.At(i);

		/* Each datum is contained within a specific cell in
		 * the row. The child element count above ensures that
		 * none of the following nodes are null. */
		const xmlNodePtr nodeEp = xmlFirstElementChild(node);
		const xmlNodePtr nodeTitle = xmlNextElementSibling(xmlNextElementSibling(nodeEp));
		const xmlNodePtr nodeDate = xmlNextElementSibling(nodeTitle);
		const xmlNodePtr nodeSource = xmlNextElementSibling(
		    xmlNextElementSibling(xmlNextElementSibling(nodeDate)));
		const xmlNodePtr nodeHint = xmlNextElementSibling(nodeSource);

		WcharsFromXmlchars(d.date, xmlNodeGetContent(nodeDate));
		WcharsFromXmlchars(d.source, xmlNodeGetContent(nodeSource));
		WcharsFromXmlchars(d.hint, xmlNodeGetContent(nodeHint));
		e.bTVOriginal = wcsncmp(d.source, L"TV", 2) == 0? 1: 0;
		WcharsFromXmlchars(e.siEp, xmlNodeGetContent(nodeEp));
		e.siEp[wcscspn(e.siEp, L"W")] = 0; /* Remove potential "WPS" suffix. */
		WcharsFromXmlchars(e.title, xmlNodeGetContent(nodeTitle));

		/* Retrieve the link to the episode's wiki entry,
		 * which should be the first (and only) child element
		 * of the title node. */
		const xmlNodePtr nodeLink = xmlFirstElementChild(nodeTitle);
		if (nodeLink)
			WcharsFromXmlchars(d.wiki,
			    xmlGetProp(nodeLink, reinterpret_cast<const xmlChar*>("href")));
	}
}

void FetchScreenwriters(unsigned char* sig)
{
	/* Screenwriters are expensive to fetch, so we try to avoid
	 * fetching screenwriters for episodes that already have a
	 * screenwriter. Additionally, in the same session, we don't
	 * try to fetch screenwriters for episodes for which we have
	 * already tried to fetch screenwriters. We keep track of
	 * these states using the iLast variable. */
	static int iLast = -1;
	int iMax = s_window->cfg.cEp-1;

	/* Find the last episode that has a screenwriter. */
	if (iLast == -1)
		for (size_t i = 0; i < s_window->fvDlv.c; i++)
			if (const DlvDataA& d = s_window->fvDlv[i]; !d.date[0]) {
				iMax = i-1;
				break;
			} else if (d.screenwriter[0])
				iLast = i;

	FINALLY { s_window->Status(L""); };

	/* Fetch screenwriters for the rest of the episodes. */
	const wchar_t prefix[] = L"https://www.detectiveconanworld.com";
	wchar_t url[256];
	Wcscpy(url, prefix);

	Unique<xmlXPathContextPtr, xmlXPathFreeContext> xpathCtx;
	Unique<xmlXPathObjectPtr, xmlXPathFreeObject> xpathObj;
	for (iLast++; iLast < iMax; iLast++) {
		if (*sig & ABORT)
			return;

		wchar_t msg[48];
		Swprintf(msg, L"Fetching screenwriter for episode %d...", iLast+1);
		s_window->Status(msg);

		/* Retrieve URL for episode's wiki page. */
		DlvDataA& d = s_window->fvDlv[iLast];
		Wcscpy(Buf(url)+Len(prefix), d.wiki);

		/* Retrieve screenwriter from HTML. */
		UniqueOk<htmlParserCtxtPtr, xmlFreeParserCtxt> ctx = RemoteParserCtxt(url, nullptr);
		xpathCtx = xmlXPathNewContext(ctx.v->myDoc);
		if (xpathCtx.Bad(0))
			throw Err(LIBXML2, L"XPath context could not be created: %s");

		xpathObj = xmlXPathEvalExpression(reinterpret_cast<const xmlChar*>(
			    "//th[contains(text(), 'Screenplay:')]/following-sibling::td"),
			    xpathCtx.v);
		if (xpathObj.Bad(0))
			throw Err(LIBXML2, L"XPath object could not be created: %s");

		xmlNodeSetPtr nodes = xpathObj.v->nodesetval;
		if (nodes && nodes->nodeNr)
			WcharsFromXmlchars(d.screenwriter, xmlNodeGetContent(nodes->nodeTab[0]));
	}
}