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
#include "malclient.h"
#include <QTcpSocket>
#include <QDebug>
#include <iostream>
#include <QFile>
#include "nwutils.h"
#include "utils.h"
namespace Mal {
Client::Client(OnlineCredentials& credentials, OnlineCredentials::TimeLock &lock, QObject *parent) :
OnlineTvShowDatabase::Client(credentials, lock, parent)
{
}
OnlineTvShowDatabase::SearchResult* Client::search(QString anime) {
QString name = anime;
if (name.isEmpty() || name.isNull()) {
return NULL;
}
QString url = "http://myanimelist.net/api/anime/search.xml?q=";
url.append(name.replace(' ', '+').remove('~'));
CurlResult userData(this);
CURL* handle = credentials.curlClient(lock, url.toLocal8Bit().data(), userData);
CURLcode error = curl_easy_perform(handle);
curl_easy_cleanup(handle);
if (error || userData.data.str().size() < 2) {
qDebug() << "received error" << error << "for query '" << url << "'' with this message:\n";
userData.print();
} else {
return new SearchResult(userData, name);
}
return NULL;
}
const QString Client::IDENTIFIER_KEY = "mal";
const QString Client::identifierKey() const {
return IDENTIFIER_KEY;
}
///////////////////////////////////////////////////////////////////
//
// MAL ENTRY
//
//////////////////////////////////////////////////////////////////
void Entry::calculateQuerySimiliarity(const QString query) {
int titleResult = Utils::querySimiliarity(query, title);
int englishTitleResult = Utils::querySimiliarity(query, englishTitle);
int bestResult = titleResult > englishTitleResult ? titleResult : englishTitleResult;
for (int i=0; i < synonyms.length(); ++i) {
const QString& synonym = synonyms.at(i);
int result = Utils::querySimiliarity(query, synonym);
if (result > bestResult) {
bestResult = result;
}
}
querySimiliarityScore = bestResult;
}
int Entry::getRemoteId() const {
return id.toInt();
}
void Entry::updateSynopsis(TvShow& show) const {
show.setSynopsis(synopsis);
}
void Entry::updateTitle(TvShow&) const {
//show.setName();
// show.setLongTitle(title);
}
void Entry::updateRemoteId(TvShow& show) const {
show.setRemoteId(Client::IDENTIFIER_KEY, id.toInt());
}
void Entry::updateRelations(TvShow& ) const {
}
void Entry::updateAiringDates(TvShow& show) const {
show.setTotalEpisodes(episodes);
show.setShowType(type);
show.setAiringStatus(status);
show.setStartDate(QDate::fromString(startDate, Entry::dateFormat));
show.setEndDate(QDate::fromString(endDate, Entry::dateFormat));
}
QStringList Entry::getSynonyms() const {
return synonyms;
}
void Entry::updateImage(TvShow& show, QDir libraryDir) const {
show.downloadImage(image, libraryDir);
}
Entry::Entry(nw::XmlReader& reader) {
parse(reader);
querySimiliarityScore = 0;
}
void Entry::parse(nw::XmlReader &xr) {
NwUtils::describe(xr, "id", id);
NwUtils::describe(xr, "title", title);
NwUtils::describe(xr, "englishTitle", englishTitle);
this->parseSynonyms(xr);
NwUtils::describe(xr, "episodes", episodes);
NwUtils::describe(xr, "type", type);
NwUtils::describe(xr, "status", status);
NwUtils::describe(xr, "start_date", startDate);
NwUtils::describe(xr, "end_date", endDate);
NwUtils::describe(xr, "synopsis", synopsis);
NwUtils::describe(xr, "image", image);
title = QUrl::fromPercentEncoding(title.toLatin1());
image = QUrl::fromPercentEncoding(image.toLatin1());
synopsis = QUrl::fromPercentEncoding(synopsis.toLatin1());
}
void Entry::parseSynonyms(nw::XmlReader &reader) {
QString synonyms;
NwUtils::describe(reader, "synonyms", synonyms);
synonyms = QUrl::fromPercentEncoding(synonyms.toLatin1());
this->synonyms = synonyms.split(QRegExp("; "));
}
QString Entry::dateFormat = "yyyy-MM-dd";
///////////////////////////////////////////////////////////////////
//
// MAL Search result
//
//////////////////////////////////////////////////////////////////
SearchResult::SearchResult(CurlResult &result, QString query) :
OnlineTvShowDatabase::SearchResult(query)
{
parse(result);
}
void SearchResult::parse(CurlResult &result) {
std::cout.flush();
nw::XmlReader xr(result.data);
xr.push("anime");
xr.describeArray("", "entry", 0);
bool hasEntries = false;
for (int i=0; xr.enterNextElement(i); ++i) {
entries.append(Entry(xr));
entries.back().calculateQuerySimiliarity(searchedQuery);
hasEntries = true;
}
if (!hasEntries) {
qDebug() << "no results for mal search >" << searchedQuery;
}
xr.close();
}
OnlineTvShowDatabase::Entry* SearchResult::bestEntry() {
std::pair<int, Entry*> best(-1, NULL);
for (int i=0; i < entries.length(); ++i) {
Entry* entry = &entries[i];
int score = Utils::querySimiliarity(this->searchedQuery, entry->title);
/*
foreach (const QString& name, entry->englishTitles) {
int s = Utils::querySimiliarity(this->query, name);
score = score >= s ? score : s;
}
*/
foreach (const QString& name, entry->synonyms) {
int s = Utils::querySimiliarity(this->searchedQuery, name);
score = score >= s ? score : s;
}
/*
foreach (const QString& name, entry->japaneseTitles) {
int s = Utils::querySimiliarity(this->query, name);
score = score >= s ? score : s;
}
*/
if (score > best.first) {
best.first = score;
best.second = entry;
}
}
return best.second;
}
} // namespace