root/src/anilistdotcodatabase.cpp

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 "anilistdotcodatabase.h"
#include "anilistdotcocredentials.h"
#include "utils.h"
#include <QUrl>
#include <QDebug>

AnilistDotCoDatabase::AnilistDotCoDatabase(OnlineCredentials& credentials, OnlineCredentials::TimeLock& lock, QObject* parent = NULL) :
    OnlineTvShowDatabase::Client(credentials, lock, parent)
{

}

const QString AnilistDotCoDatabase::identifierKey() const {
    return AnilistDotCoCredentials::IDENTIFIER_KEY;
}



// GET: anime/search/{query}
// SmallEntry
//{
//        "id": 1,
//        "title_romaji": "Cowboy Bebop",
//        "type": "TV",
//        "image_url_med": "http://anilist.co/img/dir/anime/med/1.jpg",
//        "image_url_sml": "http://anilist.co/img/dir/anime/sml/1.jpg",
//        "title_japanese": "カウボーイビバップ",
//        "title_english": "Cowboy Bebop",
//        "synonyms": [],
//        "image_url_lge": "http://anilist.co/img/dir/anime/reg/1.jpg",
//        "airing_status": "finished airing",
//        "average_score": "86.8",
//        "total_episodes": 26,
//        "adult": false,
//        "popularity": 7574,
//        "relation_type": null,
//        "role": null
//}
OnlineTvShowDatabase::SearchResult* AnilistDotCoDatabase::search(QString anime) {
    CurlResult userdata;
    QString escapedQuery = anime.replace("!", "\\!");
    QString url = QUrl(QString("https://anilist.co/api/anime/search/%1").arg(escapedQuery)).toString(QUrl::FullyEncoded);
    CURL* handle = credentials.curlClient(lock, url.toStdString().c_str(), userdata);

    CURLcode error = curl_easy_perform(handle);
    curl_easy_cleanup(handle);

    if (error) {
        qDebug() << "received error during search " << error << " with this message:\n";
        userdata.print();
        return NULL;
    }

    return new SearchResult(userdata, credentials, lock);
}

AnilistDotCoDatabase::Entry::Entry() :
    id(-1),
    total_episodes(-1),
    adult(false),
    popularity(-1),
    relation_type(-1),
    role(-1),
    extended(NULL)
{

}

AnilistDotCoDatabase::Entry::~Entry() {
    if (this->extended) {
        delete this->extended;
    }
}

int AnilistDotCoDatabase::Entry::getRemoteId() const {
    return this->id;
}

void AnilistDotCoDatabase::Entry::updateSynopsis(TvShow &show) const {
    if (this->extended) {
        show.setSynopsis(this->extended->description);
    }
}

void AnilistDotCoDatabase::Entry::updateTitle(TvShow &) const {
//    can't modifiy title yet TODO
}

void AnilistDotCoDatabase::Entry::updateRemoteId(TvShow &show) const {
    show.setRemoteId(AnilistDotCoCredentials::IDENTIFIER_KEY, this->id);
}

void AnilistDotCoDatabase::Entry::updateRelations(TvShow &) const {
    // TODO
}

void AnilistDotCoDatabase::Entry::updateAiringDates(TvShow &show) const {
    show.setTotalEpisodes(this->total_episodes);
    show.setShowType(this->type);
    show.setAiringStatus(this->airing_status);
    if (this->extended) {
        show.setStartDate(this->extended->start_date.date());
        show.setEndDate(this->extended->end_date.date());
    }
}

QStringList AnilistDotCoDatabase::Entry::getSynonyms() const {
    QStringList list = this->synonyms;
    list.append(this->title_english);
    list.append(this->title_japanese);
    list.append(this->title_romaji);
    list.removeDuplicates();
    return list;
}

void AnilistDotCoDatabase::Entry::updateImage(TvShow &show, QDir libraryDir) const {
    show.downloadImage(this->image_url_lge, libraryDir);
}

void AnilistDotCoDatabase::Entry::describe(nw::Describer &de) {
    NwUtils::describe(de, "id", id);
    NwUtils::describe(de, "title_romaji", title_romaji);
    NwUtils::describe(de, "type", type);
    NwUtils::describe(de, "image_url_med", image_url_med);
    NwUtils::describe(de, "image_url_sml", image_url_sml);
    NwUtils::describe(de, "title_japanese", title_japanese);
    NwUtils::describe(de, "title_english", title_english);
    NwUtils::describeValueArray(de, "synonyms", synonyms);
    NwUtils::describe(de, "image_url_lge", image_url_lge);
    NwUtils::describe(de, "airing_status", airing_status);
    NwUtils::describe(de, "average_score", average_score);
    NwUtils::describe(de, "total_episodes", total_episodes);
    NwUtils::describe(de, "adult", adult);
    NwUtils::describe(de, "popularity", popularity);
    NwUtils::describe(de, "relation_type", relation_type);
    NwUtils::describe(de, "role", role);
}

// GET: anime/{id}
// BigEntry
//{
//        "id": 1,
//        "title_romaji": "Cowboy Bebop",
//        "type": "TV",
//        "image_url_med": "http://anilist.co/img/dir/anime/med/1.jpg",
//        "image_url_sml": "http://anilist.co/img/dir/anime/sml/1.jpg",
//        "start_date": "1998-04-03T21:00:00+09:00",
//        "end_date": "1999-04-24T21:00:00+09:00",
//        "classification": "R - 17+ (violence & profanity)",
//        "hashtag": null,
//        "source": null,
//        "title_japanese": "カウボーイビバップ",
//        "title_english": "Cowboy Bebop",
//        "synonyms": [],
//        "description": "Enter a world in the distant future, where Bounty Hunters roam the solar system. Spike and Jet, bounty hunting partners, set out on journeys in an ever struggling effort to win bounty rewards to survive.<br><br>\nWhile traveling, they meet up with other very interesting people. Could Faye, the beautiful and ridiculously poor gambler, Edward, the computer genius, and Ein, the engineered dog be a good addition to the group?",
//        "genres": [
//                "Action",
//                "Adventure",
//                "Comedy",
//                "Drama",
//                "Sci-Fi",
//                "Space"
//        ],
//        "image_url_lge": "http://anilist.co/img/dir/anime/reg/1.jpg",
//        "image_url_banner": "http://anilist.co/img/dir/anime/banner/1.jpg",
//        "duration": 24,
//        "airing_status": "finished airing",
//        "average_score": "86.8",
//        "total_episodes": 26,
//        "youtube_id": null,
//        "adult": false,
//        "popularity": 7574,
//        "relation_type": null,
//        "role": null,
//        "list_stats": {
//                "plan_to_watch": 1673,
//                "watching": 421,
//                "completed": 4855,
//                "on_hold": 511,
//                "dropped": 114
//        },
//        "airing": {
//                "time": "2015-04-12T00:00:00+09:00",
//                "countdown": 497524,
//                "next_episode": 2
//        }
//}
void AnilistDotCoDatabase::Entry::fetchExtended(const OnlineCredentials& credentials, OnlineCredentials::TimeLock& lock) {
    if (this->id == -1) {
        return;
    }

    CurlResult userdata;
    QString url = QUrl(QString("https://anilist.co/api/anime/%1").arg(this->id)).toString(QUrl::FullyEncoded);
    CURL* handle = credentials.curlClient(lock, url.toStdString().c_str(), userdata);

    CURLcode error = curl_easy_perform(handle);
    curl_easy_cleanup(handle);

    if (error) {
        qDebug() << "received error during full-anime-model fetch" << error << " with this message:\n";
        userdata.print();
        return;
    }

    nw::JsonReader jr(userdata.data);
    ExtendedEntry* e = new ExtendedEntry();
    e->describe(jr);
    jr.close();
    this->extended = e;
}

OnlineTvShowDatabase::Entry *AnilistDotCoDatabase::SearchResult::bestEntry() {
    std::pair<int, Entry*> best(-1, NULL);
    for (int i=0; i < entries.length(); ++i) {
        Entry* entry = (Entry*)entries[i];

        // TODO FIXME XXX FUCK ING HEKKELL this is suck horrible redundant cod ewith malclient
        // just put it all into a list and give it to the highest part
        // monads would also come in handy here
        int score = Utils::querySimiliarity(this->searchedQuery, entry->title_romaji);
        const int engScore = Utils::querySimiliarity(this->searchedQuery, entry->title_english);
        const int jpScore = Utils::querySimiliarity(this->searchedQuery, entry->title_japanese);
        score = score >= engScore ? score : engScore;
        score = score >= jpScore ? score : jpScore;
        foreach (const QString& name, entry->synonyms) {
            int s = Utils::querySimiliarity(this->searchedQuery, name);
            score = score >= s ? score : s;
        }
        if (score > best.first) {
            best.first = score;
            best.second = entry;
        }
    }

    if (best.second) {
        best.second->fetchExtended(credentials, lock);
    }

    return best.second;
}


AnilistDotCoDatabase::SearchResult::SearchResult(CurlResult& userdata, const OnlineCredentials &credentials, OnlineCredentials::TimeLock& lock) :
    credentials(credentials),
    lock(lock)
{
    userdata.print();
    nw::JsonReader jr(userdata.data);
    jr.describeArray("", "", -1);
    for (unsigned int i=0; jr.enterNextElement(i); ++i) {
        Entry* e = new Entry();
        e->describe(jr);
        this->entries.push_back(e);
    }
    jr.close();
}


AnilistDotCoDatabase::ExtendedEntry::ExtendedEntry() :
    duration(-1),
    isAiring(false)
{

}

void AnilistDotCoDatabase::ExtendedEntry::describe(nw::Describer &de) {
    NwUtils::describe(de, "start_date", start_date);
    NwUtils::describe(de, "end_date", end_date);
    NwUtils::describe(de, "classification", classification);
    NwUtils::describe(de, "hashtag", hashtag);
    NwUtils::describe(de, "source", source);
    NwUtils::describe(de, "description", description);
    NwUtils::describeValueArray(de, "genres", genres);
    NwUtils::describe(de, "image_url_banner", image_url_banner);
    NwUtils::describe(de, "duration", duration);
    NwUtils::describe(de, "youtube_id", youtube_id);

    //QList<int> list_stats; // {
    //int airing; // {
    if (de.conditionalPush("airing_status", true)) {
        isAiring = true;
        de.pop();
    }
}