-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilmSearch.m
executable file
·90 lines (68 loc) · 2.92 KB
/
filmSearch.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
//
// filmSearch.m
// filmSearch
//
// Created by Kirbyk on 18.02.2014.
// Copyright (c) 2014 Kirbyk. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <SearchLoader/TLLibrary.h>
#define GET_BOOL(key, default) (prefs[key] ? ((NSNumber *)prefs[key]).boolValue : default)
#define GET_INT(key, default) (prefs[key] ? ((NSNumber *)prefs[key]).intValue : default)
#define GET_STR(key, default) (prefs[key] ? prefs[key] : default)
@interface TLfilmSearchDatastore : NSObject <TLSearchDatastore> {
BOOL $usingInternet;
}
@end
@implementation TLfilmSearchDatastore
- (void)performQuery:(SDSearchQuery *)query withResultsPipe:(SDSearchQuery *)results {
NSString *searchString = [query searchString];
NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/me.kirbyk.filmsearch.plist"];
int limit = GET_INT(@"MovieLimit", 5);
searchString = [searchString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString *format = [NSString stringWithFormat:@"http://www.omdbapi.com/?s=%@", searchString];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:format]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:2];
TLRequireInternet(YES);
$usingInternet = YES;
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error){
if (data != nil) {
NSMutableArray *searchResults = [NSMutableArray array];
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *movies = root[@"Search"];
int count = 0;
for (NSDictionary *movie in movies) {
if (count >= limit) break;
NSMutableString *url = [NSMutableString stringWithString:@"imdb:///title/"];
[url appendString:[NSString stringWithFormat:@"%@", movie[@"imdbID"]]];
SPSearchResult *result = [[[SPSearchResult alloc] init] autorelease];
[result setTitle:movie[@"Title"]];
[result setSubtitle:movie[@"Year"]];
[result setUrl:url];
[searchResults addObject:result];
count++;
}
TLCommitResults(searchResults, TLDomain(@"com.film.film", @"filmSearch"), results);
}
TLRequireInternet(NO);
$usingInternet = NO;
[results storeCompletedSearch:self];
TLFinishQuery(results);
}];
}
- (NSArray *)searchDomains {
return [NSArray arrayWithObject:[NSNumber numberWithInteger:TLDomain(@"com.film.film", @"filmSearch")]];
}
- (NSString *)displayIdentifierForDomain:(NSInteger)domain {
return @"com.film.film";
}
- (BOOL)blockDatastoreComplete {
return $usingInternet;
}
@end