This repository has been archived by the owner on Jun 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyAnimeList+Search.m
326 lines (317 loc) · 17 KB
/
MyAnimeList+Search.m
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
//
// MyAnimeList+Search.m
// MAL Updater OS X
//
// Created by 高町なのは on 2015/02/11.
// Copyright 2009-2015 MAL Updater OS X Group. All rights reserved. Code licensed under New BSD License
//
#import "MyAnimeList+Search.h"
#import "MyAnimeList+Keychain.h"
#import <EasyNSURLConnection/EasyNSURLConnection.h>
#import "Utility.h"
#import "ExceptionsCache.h"
#import "Recognition.h"
#import <XMLReader/XMLReader.h>
@implementation MyAnimeList (Search)
- (NSString *)searchanime{
// Searches for ID of associated title
NSString *searchtitle = self.DetectedTitle;
if (self.DetectedSeason > 1) {
// Specifically search for season
for (int i = 0; i < 2; i++) {
NSString *tmpid;
switch (i) {
case 0:
tmpid = [self performSearch:[NSString stringWithFormat:@"%@ %i", [Utility desensitizeSeason:searchtitle], self.DetectedSeason]];
break;
case 1:
tmpid = [self performSearch:[NSString stringWithFormat:@"%@ %@ Season", [Utility desensitizeSeason:searchtitle], [Utility numbertoordinal:self.DetectedSeason]]];
default:
break;
}
if (tmpid.length > 0) {
return tmpid;
}
}
}
else {
return [self performSearch:searchtitle]; //Perform Regular Search
}
return [self performSearch:searchtitle];
}
- (NSString *)performSearch:(NSString *)searchtitle{
NSLog(@"Searching For Title");
//Escape Search Term
NSString *searchterm = [Utility urlEncodeString:searchtitle];
//Set Search API
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://myanimelist.net/api/anime/search.xml?q=%@", searchterm]];
EasyNSURLConnection *request = [[EasyNSURLConnection alloc] initWithURL:url];
[Utility setUserAgent:request];
//Set Token
request.headers = (NSMutableDictionary *)@{@"Authorization": [NSString stringWithFormat:@"Basic %@", [self getBase64]]};
//Ignore Cookies
[request setUseCookies:NO];
//Perform Search
[request startRequest];
// Get Status Code
long statusCode = [request getStatusCode];
switch (statusCode) {
case 0:
self.Success = NO;
return @"";
case 200:
return [self findaniid:[request getResponseDataString] searchterm:searchtitle];
default:
self.Success = NO;
return @"";
}
}
- (NSString *)findaniid:(NSString *)ResponseData searchterm:(NSString *)term {
// Parse XML
NSArray *searchdata = [self MALSearchXMLToAtarashiiDataFormat:ResponseData];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSSortDescriptor *descriptor2 = [[NSSortDescriptor alloc] initWithKey:@"english_title" ascending:YES];
searchdata = [searchdata sortedArrayUsingDescriptors:@[descriptor, descriptor2]];
//Initalize NSString to dump the title temporarily
NSString *theshowtitle = @"";
NSString *alttitle = @"";
//Create Regular Expressions
OnigRegexp *regex;
// Check if title is a movie or not.
self.DetectedTitleisMovie = [self.DetectedType isEqualToString:@"Movie"] || [self.DetectedType isEqualToString:@"movie"] ? true : false;
NSLog(@"%@", [self.DetectedType isEqualToString:@"Movie"] || [self.DetectedType isEqualToString:@"movie"] ? @"Title is a movie" : @"Title is not a movie.");
// Create a filtered Arrays
NSArray *sortedArray = [self filterArray:searchdata];
searchdata = nil;
// Used for String Comparison
NSDictionary *titlematch1;
NSDictionary *titlematch2;
int mstatus = 0;
// Remove Colons
term = [term stringByReplacingOccurrencesOfString:@":" withString:@""];
// Search
for (int i = 0; i < 2; i++) {
switch (i) {
case 0:
regex = [OnigRegexp compile:[NSString stringWithFormat:@"(%@)",term] options:OnigOptionIgnorecase];
break;
case 1:
regex = [OnigRegexp compile:[[NSString stringWithFormat:@"(%@)",term] stringByReplacingOccurrencesOfString:@" " withString:@"|"] options:OnigOptionIgnorecase];
break;
default:
break;
}
for (NSDictionary *searchentry in sortedArray) {
theshowtitle = (NSString *)searchentry[@"title"];
NSArray *a = @[];
//Populate Synonyms if any.
if (((NSArray *)searchentry[@"synonyms"]).count > 0) {
a = searchentry[@"synonyms"];
}
else {alttitle = @"";}
// Remove colons as they are invalid characters for filenames and to improve accuracy
theshowtitle = [theshowtitle stringByReplacingOccurrencesOfString:@":" withString:@""];
int matchstatus = 0;
if (((NSArray *)searchentry[@"synonyms"]).count > 0) {
for (NSString *syn in a) {
alttitle = syn;
alttitle = [alttitle stringByReplacingOccurrencesOfString:@":" withString:@""];
matchstatus = [Utility checkMatch:theshowtitle alttitle:alttitle regex:regex option:i];
if (matchstatus == PrimaryTitleMatch || matchstatus == AlternateTitleMatch) {
break;
}
}
}
else {
matchstatus = [Utility checkMatch:theshowtitle alttitle:alttitle regex:regex option:i];
}
if (matchstatus == PrimaryTitleMatch || matchstatus == AlternateTitleMatch) {
if ([[NSString stringWithFormat:@"%@", searchentry[@"type"]] isEqualToString:@"TV"]) { // Check Seasons if the title is a TV show type
// Used for Season Checking
OnigRegexp *regex2 = [OnigRegexp compile:[NSString stringWithFormat:@"((%i(st|nd|rd|th)|%@) season|\\W%i)", self.DetectedSeason, [Utility seasonInWords:self.DetectedSeason],self.DetectedSeason] options:OnigOptionIgnorecase];
OnigResult *smatch = [regex2 search:[NSString stringWithFormat:@"%@ - %@",theshowtitle, alttitle]];
// Description checking
NSString *description = searchentry[@"synopsis"] ? (NSString *)searchentry[@"synopsis"] : @"";
OnigResult *smatch2 = [regex2 search:description];
if (self.DetectedSeason >= 2) { // Season detected, check to see if there is a match. If not, continue.
if (smatch.count == 0 && smatch2.count == 0 && [sortedArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"TV"]].count > 1) { // If there is a second season match, in most cases, it would be the only entry
continue;
}
}
else {
if (smatch.count > 0 && smatch2.count > 0 && self.DetectedSeason >= 2) { // No Season, check to see if there is a season or not. If so, continue.
continue;
}
}
}
if (self.DetectedTitleisMovie) {
self.DetectedEpisode = @"1"; // Usually, there is one episode in a movie.
if ([[NSString stringWithFormat:@"%@", searchentry[@"type"]] isEqualToString:@"Special"]||[[NSString stringWithFormat:@"%@", searchentry[@"type"]] isEqualToString:@"OVA"]) {
self.DetectedTitleisMovie = false;
}
}
//Return titleid if episode is valid
if ( ([NSString stringWithFormat:@"%@", searchentry[@"episodes"]].intValue == 0 || ([NSString stringWithFormat:@"%@",searchentry[@"episodes"]].intValue >= (self.DetectedEpisode).intValue))) {
NSLog(@"Valid Episode Count");
NSLog(@"Term length: %li, Show title Length: %li, alt title length: %li", term.length, theshowtitle.length, alttitle.length);
if (sortedArray.count == 1 || self.DetectedSeason >= 2) {
return [self foundtitle:[NSString stringWithFormat:@"%@",searchentry[@"id"]] info:searchentry];
}
else if ((!titlematch1 && sortedArray.count > 1) && ((term.length < theshowtitle.length+1)||(term.length< alttitle.length+1 && alttitle.length > 0 && matchstatus == AlternateTitleMatch))) {
mstatus = matchstatus;
titlematch1 = searchentry;
continue;
}
else if (titlematch1) {
titlematch2 = searchentry;
return titlematch1 != titlematch2 ? [self comparetitle:term match1:titlematch1 match2:titlematch2 mstatus:mstatus mstatus2:matchstatus] : [self foundtitle:[NSString stringWithFormat:@"%@",searchentry[@"id"]] info:searchentry];
}
}
else {
// Detected episodes exceed total episodes
continue;
}
}
}
}
// If one match is found and not null, then return the id.
if (titlematch1/* || titlematch1 == titlematch2*/) {
// Only Result, return
return [self foundtitle:[NSString stringWithFormat:@"%@",titlematch1[@"id"]] info:titlematch1];
}
// Nothing found, return empty string
return @"";
}
- (NSArray *)filterArray:(NSArray *)searchdata{
NSMutableArray *sortedArray;
if (self.DetectedTitleisMovie) {
sortedArray = [NSMutableArray arrayWithArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)" , @"Movie"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"Special"]]];
}
else if (self.DetectedTitleisEpisodeZero) {
sortedArray = [NSMutableArray arrayWithArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(title CONTAINS %@)" , @"Episode 0"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"Special"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"Movie"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"OVA"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"ONA"]]];
}
else {
// Check if there is any type keywords. If so, only focus on that show type
if (self.DetectedType.length > 0) {
sortedArray = [NSMutableArray arrayWithArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(show_type ==[c] %@)", self.DetectedType]]];
}
else {
sortedArray = [NSMutableArray arrayWithArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"TV"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"ONA"]]];
if (self.DetectedSeason == 1 | self.DetectedSeason == 0) {
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"Special"]]];
[sortedArray addObjectsFromArray:[searchdata filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"OVA"]]];
}
}
}
return sortedArray;
}
- (NSString *)foundtitle:(NSString *)titleid info:(NSDictionary *)found{
//Check to see if Seach Cache is enabled. If so, add it to the cache.
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"useSearchCache"] && titleid.length > 0) {
NSNumber *episodes = found[@"episodes"] == [NSNull null] ? @(0) : (NSNumber *)found[@"episodes"];
//Save AniID
[ExceptionsCache addtoCache:self.DetectedTitle showid:titleid actualtitle:(NSString *)found[@"title"] totalepisodes:episodes.intValue detectedSeason:self.DetectedSeason];
}
//Return the AniID
return titleid;
}
- (NSString *)comparetitle:(NSString *)title match1:(NSDictionary *)match1 match2:(NSDictionary *)match2 mstatus:(int)a mstatus2:(int)b{
// Perform string score between two titles to see if one is the correct match or not
double score1, score2, ascore1, ascore2;
double fuzziness = 0.3;
int season1 = ((NSNumber *)[[Recognition alloc] recognize:match1[@"title"]][@"season"]).intValue;
int season2 = ((NSNumber *)[[Recognition alloc] recognize:match2[@"title"]][@"season"]).intValue;
//Score first title
score1 = string_fuzzy_score(title.UTF8String, [NSString stringWithFormat:@"%@", match1[@"title"]].UTF8String, fuzziness);
ascore1 = [self gethighestsynonymscore:match1[@"synonyms"] withTitle:title];
//Score Second Title
score2 = string_fuzzy_score(title.UTF8String, [NSString stringWithFormat:@"%@", match2[@"title"]].UTF8String, fuzziness);
ascore2 = [self gethighestsynonymscore:match1[@"synonyms"] withTitle:title];
NSLog(@"%@ score - %f", match1[@"title"], score1);
NSLog(@"%@ score - %f", match2[@"title"], score2);
NSLog(@"%@ ascore - %f", match1[@"title"], ascore1);
NSLog(@"%@ ascore - %f", match2[@"title"], ascore2);
//First Season Score Bonus
if (self.DetectedSeason == 0 || self.DetectedSeason == 1) {
if ([(NSString *)match1[@"title"] rangeOfString:@"First"].location != NSNotFound || [(NSString *)match1[@"title"] rangeOfString:@"1st"].location != NSNotFound) {
score1 = score1 + .25;
ascore1 = ascore1 + .25;
}
else if ([(NSString *)match2[@"title"] rangeOfString:@"First"].location != NSNotFound || [(NSString *)match2[@"title"] rangeOfString:@"1st"].location != NSNotFound) {
score2 = score2 + .25;
ascore2 = ascore2 + .25;
}
}
//Season Scoring Calculation
if ( season1 != self.DetectedSeason) {
ascore1 = ascore1 - .5;
score1 = score1 - .5;
}
if ( season2 != self.DetectedSeason) {
ascore2 = ascore2 - .5;
score2 = score2 - .5;
}
// Take the highest of both matches scores
double finalscore1 = score1 > ascore1 ? score1 : ascore1;
double finalscore2 = score2 > ascore2 ? score2 : ascore2;
// Compare Scores
if (finalscore1 == finalscore2 || finalscore1 == INFINITY) {
//Scores can't be reliably compared, just return the first match
return [self foundtitle:[NSString stringWithFormat:@"%@",match1[@"id"]] info:match1];
}
else if(finalscore1 > finalscore2)
{
//Return first title as it has a higher score
return [self foundtitle:[NSString stringWithFormat:@"%@",match1[@"id"]] info:match1];
}
else {
// Return second title since it has a higher score
return [self foundtitle:[NSString stringWithFormat:@"%@",match2[@"id"]] info:match2];
}
}
- (NSArray *)MALSearchXMLToAtarashiiDataFormat:(NSString *)xml {
NSError *error = nil;
NSDictionary *searchxml = [XMLReader dictionaryForXMLString:xml options:XMLReaderOptionsProcessNamespaces error:&error];
NSArray *searchresults;
if (searchxml[@"anime"]) {
searchresults = searchxml[@"anime"][@"entry"];
if (![searchresults isKindOfClass:[NSArray class]]) {
// Import only contains one object, put it in an array.
searchresults = @[searchresults];
}
}
else {
return @[];
}
NSMutableArray *output = [NSMutableArray new];
for (NSDictionary *d in searchresults) {
NSMutableArray *synonyms = [NSMutableArray new];
NSString *englishtitle = @"";
if (d[@"english"][@"text"]) {
[synonyms addObject:d[@"english"][@"text"]];
englishtitle = d[@"english"][@"text"];
}
if (d[@"synonyms"][@"text"]) {
[synonyms addObjectsFromArray:[((NSString *)d[@"synonyms"][@"text"]) componentsSeparatedByString:@";"]];
}
[output addObject:@{@"id":@(((NSString *)d[@"id"][@"text"]).intValue), @"episodes":@(((NSString *)d[@"episodes"][@"text"]).intValue), @"score":@(((NSString *)d[@"score"][@"text"]).floatValue), @"status":d[@"status"][@"text"], @"start_date":[NSString stringWithFormat:@"%@",d[@"start_date"][@"text"]], @"end_date":[NSString stringWithFormat:@"%@",d[@"end_date"][@"text"]], @"synonyms": synonyms, @"synopsis":[NSString stringWithFormat:@"%@",d[@"synopsis"][@"text"]], @"type":d[@"type"][@"text"], @"title":d[@"title"][@"text"], @"english_title":englishtitle}];
}
return output;
}
- (double)gethighestsynonymscore:(NSArray *)synonyms withTitle:(NSString *)title {
double score = 0;
for (NSString *synonym in synonyms ) {
double tmpscore = string_fuzzy_score(title.UTF8String, synonym.UTF8String, 0.3);
if (tmpscore > score) {
score = tmpscore;
}
}
return score;
}
@end