summaryrefslogtreecommitdiffstats
path: root/fbreader/src/network/litres/LitResBooksFeedParser.cpp
blob: 970a8eb323ce729e4db89f20658a6b1ffead387e (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright (C) 2009-2012 Geometer Plus <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <cstdlib>

#include <ZLStringUtil.h>
#include <ZLUnicodeUtil.h>

#include "LitResBooksFeedParser.h"
#include "LitResBookItem.h"
#include "LitResGenre.h"
#include "LitResUtil.h"
#include "../NetworkLink.h"

static const std::string TAG_CATALOG = "catalit-fb2-books";
static const std::string TAG_BOOK = "fb2-book";
static const std::string TAG_TEXT_DESCRIPTION = "text_description";
static const std::string TAG_HIDDEN = "hidden";
static const std::string TAG_TITLE_INFO = "title-info";
static const std::string TAG_GENRE = "genre";
static const std::string TAG_AUTHOR = "author";
static const std::string TAG_FIRST_NAME = "first-name";
static const std::string TAG_MIDDLE_NAME = "middle-name";
static const std::string TAG_LAST_NAME = "last-name";
static const std::string TAG_AUTHOR_ID = "id";
static const std::string TAG_BOOK_TITLE = "book-title";
static const std::string TAG_ANNOTATION = "annotation";
static const std::string TAG_DATE = "date";
static const std::string TAG_SEQUENCE = "sequence";
static const std::string TAG_LANGUAGE = "lang";

std::string LitResBooksFeedParser::stringAttributeValue(const char **attributes, const char *name) {
	if (attributes == 0) {
		return std::string();
	}
	const char *value = attributeValue(attributes, name);
	return value != 0 ? value : std::string();
}

LitResBooksFeedParser::LitResBooksFeedParser(const NetworkLink &link, NetworkItem::List &books, LitResBooksFeedItem::LoadingState *loadingState) :
	myLink(link),
	myBooks(books),
	myIndex(0),
	myLoadingState(loadingState) {
	myState = START;
}


void LitResBooksFeedParser::startElementHandler(const char *tag, const char **attributes) {
	processState(tag, false, attributes);
	myState = getNextState(tag, false);
	myBuffer.clear();
}

void LitResBooksFeedParser::endElementHandler(const char *tag) {
	processState(tag, true, 0);
	myState = getNextState(tag, true);
	myBuffer.clear();
}

void LitResBooksFeedParser::characterDataHandler(const char *data, std::size_t len) {
	myBuffer.append(data, len);
}

void LitResBooksFeedParser::processState(const std::string &tag, bool closed, const char **attributes) {
	switch(myState) {
	case START:
		if (!closed && TAG_CATALOG == tag) {
			if (myLoadingState) {
				myLoadingState->AllPagesCount = ZLStringUtil::stringToInteger(stringAttributeValue(attributes, "pages"), 1);
			}
		}
		break;
	case CATALOG:
		if (!closed && TAG_BOOK == tag) {
			myBookId = stringAttributeValue(attributes, "hub_id");
			myURLByType[NetworkItem::URL_COVER] =
				stringAttributeValue(attributes, "cover_preview");
			myURLByType[NetworkItem::URL_FULL_COVER] =
				stringAttributeValue(attributes, "cover");

			std::string url = stringAttributeValue(attributes, "url");
			if (!url.empty()) {
				myLink.rewriteUrl(url, true); // This code duplicates code in FBReader::openInBrowser and is not required
				myURLByType[NetworkItem::URL_HTML_PAGE] = url;
			}

			//TODO check if buying book works right
			std::string price = BuyBookReference::price(stringAttributeValue(attributes, "price"), "RUB");
			myReferences.push_back(new BuyBookReference(
				LitResUtil::generatePurchaseUrl(myLink, myBookId),
				BookReference::FB2_ZIP,
				BookReference::BUY,
				price
			));

			std::string hasTrial = stringAttributeValue(attributes, "has_trial");
			if (!hasTrial.empty() && hasTrial != "0") {
				myReferences.push_back(new BookReference(
					LitResUtil::generateTrialUrl(myBookId),
					BookReference::FB2_ZIP,
					BookReference::DOWNLOAD_DEMO
				));
			}

			myReferences.push_back(new BookReference(
				LitResUtil::generateDownloadUrl(myBookId),
				BookReference::FB2_ZIP,
				BookReference::DOWNLOAD_FULL_CONDITIONAL
			));
		}
		break;
	case BOOK:
		if (closed && TAG_BOOK == tag) {
			myBooks.push_back(new LitResBookItem(
				myLink,
				myBookId,
				myIndex++,
				myTitle,
				mySummary,
				myLanguage,
				myDate,
				myAuthors,
				myTags,
				mySeriesTitle,
				myIndexInSeries,
				myURLByType,
				myReferences,
				myAuthorsIds
			));

			myTitle.erase();
			mySummary.erase();
			myLanguage.erase();
			myDate.erase();
			mySeriesTitle.erase();
			myIndexInSeries = 0;
			myAuthors.clear();
			myAuthorsIds.clear();
			myTags.clear();
			myURLByType.clear();
			myReferences.clear();
		}
		break;
	case BOOK_DESCRIPTION:
		break;
	case HIDDEN:
		break;
	case TITLE_INFO:
		if (!closed) {
			if (TAG_AUTHOR == tag) {
				myAuthorFirstName.clear();
				myAuthorMiddleName.clear();
				myAuthorLastName.clear();
			} else if (TAG_SEQUENCE == tag) {
				mySeriesTitle = stringAttributeValue(attributes, "name");
				if (!mySeriesTitle.empty()) {
					const char *indexInSeries = attributeValue(attributes, "number");
					myIndexInSeries = indexInSeries != 0 ? std::atoi(indexInSeries) : 0;
				}
			}
		}
		break;
	case AUTHOR:
		if (closed && TAG_AUTHOR == tag) {
			NetworkBookItem::AuthorData data;
			if (!myAuthorFirstName.empty()) {
				data.DisplayName.append(myAuthorFirstName);
			}
			if (!myAuthorMiddleName.empty()) {
				if (!data.DisplayName.empty()) {
					data.DisplayName.append(" ");
				}
				data.DisplayName.append(myAuthorMiddleName);
			}
			if (!myAuthorLastName.empty()) {
				if (!data.DisplayName.empty()) {
					data.DisplayName.append(" ");
				}
				data.DisplayName.append(myAuthorLastName);
			}
			data.SortKey = myAuthorLastName;
			myAuthors.push_back(data);
			myAuthorsIds.push_back(myAuthorId);
		}
		break;
	case FIRST_NAME:
		if (closed && TAG_FIRST_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorFirstName = myBuffer;
		}
		break;
	case MIDDLE_NAME:
		if (closed && TAG_MIDDLE_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorMiddleName = myBuffer;
		}
		break;
	case LAST_NAME:
		if (closed && TAG_LAST_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorLastName = myBuffer;
		}
		break;
	case AUTHOR_ID:
		if (closed && TAG_AUTHOR_ID == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorId = myBuffer;
		}
		break;
	case GENRE:
		if (closed && TAG_GENRE == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);

			const std::map<std::string,shared_ptr<LitResGenre> > &genresMap =
				LitResGenreMap::Instance().genresMap();
			const std::map<shared_ptr<LitResGenre>,std::string> &genresTitles =
				LitResGenreMap::Instance().genresTitles();

			std::map<std::string, shared_ptr<LitResGenre> >::const_iterator it = genresMap.find(myBuffer);
			if (it != genresMap.end()) {
				std::map<shared_ptr<LitResGenre>, std::string>::const_iterator jt = genresTitles.find(it->second);
				if (jt != genresTitles.end()) {
					myTags.push_back(jt->second);
				}
			}
		}
		break;
	case BOOK_TITLE:
		if (closed && TAG_BOOK_TITLE == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myTitle = myBuffer;
		}
		break;
	case ANNOTATION:
		if (!closed) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			if (!myBuffer.empty()) {
				mySummary.append(myBuffer);
				mySummary.append(" ");
			}
		} else {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			mySummary.append(myBuffer);
			int size = mySummary.size();
			if (size > 0) {
				if (TAG_ANNOTATION == tag) {
					if (mySummary[size - 1] == '\n') {
						mySummary.erase(size - 1);
					}
				} else if ("p" == tag) {
					if (mySummary[size - 1] != '\n') {
						mySummary.append("\n");
					}
				} else {
					if (!myBuffer.empty() && mySummary[size - 1] != '\n') {
						mySummary.append(" ");
					}
				}
			}
		}
		break;
	case DATE:
		if (closed && TAG_DATE == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myDate = myBuffer;
		}
		break;
	case LANGUAGE:
		if (closed && TAG_LANGUAGE == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myLanguage = myBuffer;
		}
		break;
	}
}

LitResBooksFeedParser::State LitResBooksFeedParser::getNextState(const std::string &tag, bool closed) {
	switch(myState) {
	case START:
		if (!closed && TAG_CATALOG == tag) {
			return CATALOG;
		}
		break;
	case CATALOG:
		if (!closed) {
			if (TAG_BOOK == tag) {
				return BOOK;
			}
		} else {
			if (TAG_CATALOG == tag) {
				return START;
			}
		}
		break;
	case BOOK:
		if (!closed) {
			if (TAG_TEXT_DESCRIPTION == tag) {
				return BOOK_DESCRIPTION;
			}
		} else {
			if (TAG_BOOK == tag) {
				return CATALOG;
			}
		}
		break;
	case BOOK_DESCRIPTION:
		if (!closed) {
			if (TAG_HIDDEN == tag) {
				return HIDDEN;
			}
		} else {
			if (TAG_TEXT_DESCRIPTION == tag) {
				return BOOK;
			}
		}
		break;
	case HIDDEN:
		if (!closed) {
			if (TAG_TITLE_INFO == tag) {
				return TITLE_INFO;
			}
		} else {
			if (TAG_HIDDEN == tag) {
				return BOOK_DESCRIPTION;
			}
		}
		break;
	case TITLE_INFO:
		if (!closed) {
			if (TAG_GENRE == tag) {
				return GENRE;
			} else if (TAG_AUTHOR == tag) {
				return AUTHOR;
			} else if (TAG_BOOK_TITLE == tag) {
				return BOOK_TITLE;
			} else if (TAG_ANNOTATION == tag) {
				return ANNOTATION;
			} else if (TAG_DATE == tag) {
				return DATE;
			} else if (TAG_LANGUAGE == tag) {
				return LANGUAGE;
			} /*else if (TAG_SEQUENCE == tag) {
				return SEQUENCE; // handled without state through attributes
			}*/
		} else {
			if (TAG_TITLE_INFO == tag) {
				return HIDDEN;
			}
		}
		break;
	case AUTHOR:
		if (!closed) {
			if (TAG_FIRST_NAME == tag) {
				return FIRST_NAME;
			} else if (TAG_MIDDLE_NAME == tag) {
				return MIDDLE_NAME;
			} else if (TAG_LAST_NAME == tag) {
				return LAST_NAME;
			} else if (TAG_AUTHOR_ID == tag) {
				return AUTHOR_ID;
			}
		} else {
			if (TAG_AUTHOR == tag) {
				return TITLE_INFO;
			}
		}
		break;
	case FIRST_NAME:
		if (closed && TAG_FIRST_NAME == tag) {
			return AUTHOR;
		}
		break;
	case MIDDLE_NAME:
		if (closed && TAG_MIDDLE_NAME == tag) {
			return AUTHOR;
		}
		break;
	case LAST_NAME:
		if (closed && TAG_LAST_NAME == tag) {
			return AUTHOR;
		}
		break;
	case AUTHOR_ID:
		if (closed && TAG_AUTHOR_ID == tag) {
			return AUTHOR;
		}
		break;
	case GENRE:
		if (closed && TAG_GENRE == tag) {
			return TITLE_INFO;
		}
		break;
	case BOOK_TITLE:
		if (closed && TAG_BOOK_TITLE == tag) {
			return TITLE_INFO;
		}
		break;
	case ANNOTATION:
		if (closed && TAG_ANNOTATION == tag) {
			return TITLE_INFO;
		}
		break;
	case DATE:
		if (closed && TAG_DATE == tag) {
			return TITLE_INFO;
		}
		break;
	case LANGUAGE:
		if (closed && TAG_LANGUAGE == tag) {
			return TITLE_INFO;
		}
		break;
	}
	return myState;
}