forked from berkley/AnimatedPhotoViewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlickr.m
218 lines (188 loc) · 7.46 KB
/
Flickr.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
//
// Flickr.m
// AnimatedPhotoViewer
//
// Created by Chad Berkley on 11/16/10.
// Copyright 2010 OffHeGoes. All rights reserved.
//
#import "Flickr.h"
#import "Session.h"
#import "PhotoCache.h"
#import "FlickrPhoto.h"
@implementation Flickr
@synthesize apikey, secret, context;
static Flickr *sharedInstance = nil;
NSString *queryType;
//url protocol: geoflickr://auth.success
+ (Flickr*)sharedInstance
{
@synchronized(self)
{
if (sharedInstance == nil)
{
sharedInstance = [[Flickr alloc] init];
}
}
return sharedInstance;
}
- (id)init
{
if(self = [super init])
{
secret = @"2095762ff1a7b481";
apikey = @"06878e5364a281e7ccdc5b5f8f85e57e";
context = [[OFFlickrAPIContext alloc] initWithAPIKey:apikey sharedSecret:secret];
}
return self;
}
- (void)openAuthUrl
{
NSURL *url = [self.context loginURLFromFrobDictionary:nil requestedPermission:@"read"];
if (![[UIApplication sharedApplication] openURL:url])
{
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
}
- (void)makeFlickrAPICallWithName:(NSString*)name params:(NSDictionary*)params queryType:(NSString*)qt
{
queryType = qt;
OFFlickrAPIRequest *request = [[OFFlickrAPIRequest alloc] initWithAPIContext:self.context];
[request setDelegate:self];
[request callAPIMethodWithGET:name arguments:params];
}
//search flickr using the current values in Session
- (void)searchFlickr
{
if([Session sharedInstance].currentLocation == nil)
{ //don't run the query if we don't have a current location yet.
NSLog(@"Not running flickr query because we don't have a location");
return;
}
NSLog(@"Running Flickr query");
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableArray *objects = [[NSMutableArray alloc] init];
//add the api key
[keys addObject:@"api_key"];
[objects addObject:self.apikey];
//add the user id if we only want to search this users photos, don't add
//anything to search public photos
if([Session sharedInstance].searchMyPhotosOnly)
{
[keys addObject:@"user_id"];
[objects addObject:@"me"];
}
//add the text based query if the query string isn't nil
NSString *query = [Session trimString:[Session sharedInstance].query];
if(![query isEqualToString:@""])
{
[keys addObject:@"text"];
[objects addObject:query];
NSLog(@"Query Param: %@", query);
}
//add geo radius params
[keys addObject:@"radius"];
[objects addObject:[[NSNumber numberWithInt:[Session sharedInstance].distanceThreshold] stringValue]];
NSLog(@"radius: %@", [[NSNumber numberWithInt:[Session sharedInstance].distanceThreshold] stringValue]);
//add current location
[keys addObject:@"lat"];
[objects addObject:[[NSNumber numberWithDouble:[Session sharedInstance].currentLocation.coordinate.latitude] stringValue]];
[keys addObject:@"lon"];
[objects addObject:[[NSNumber numberWithDouble:[Session sharedInstance].currentLocation.coordinate.longitude] stringValue]];
NSLog(@"Current location: lat:%@ lon:%@",
[[NSNumber numberWithDouble:[Session sharedInstance].currentLocation.coordinate.latitude] stringValue],
[[NSNumber numberWithDouble:[Session sharedInstance].currentLocation.coordinate.longitude] stringValue]);
//add units
[keys addObject:@"radius_units"];
[objects addObject:@"mi"];
//add geo information and square icon url to the search results
[keys addObject:@"extras"];
[objects addObject:@"geo,url_sq,url_m"];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[Session inspectDictionary:dict];
[self makeFlickrAPICallWithName:@"flickr.photos.search" params:dict queryType:@"query"];
}
//sets Session.flickrAuthKey if successful
- (void)getAuthToken:(NSString*)frob
{
NSArray *objects = [NSArray arrayWithObjects:self.apikey, frob, nil];
NSArray *keys = [NSArray arrayWithObjects:@"api_key", @"frob", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[self makeFlickrAPICallWithName:@"flickr.auth.GetToken" params:dict queryType:@"getAuthToken"];
}
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
if([queryType isEqualToString:@"getAuthToken"])
{
NSDictionary *auth = [inResponseDictionary objectForKey:@"auth"];
NSDictionary *token = [auth objectForKey:@"token"];
[Session sharedInstance].flickrAuthKey = [token objectForKey:@"_text"];
NSDictionary *user = [auth objectForKey:@"user"];
[Session sharedInstance].flickrUsername = [user objectForKey:@"username"];
[Session sharedInstance].flickrFullname = [user objectForKey:@"fullname"];
[Session sharedInstance].flickrNsid = [user objectForKey:@"nsid"];
NSLog(@"set flickrAuthKey to %@", [Session sharedInstance].flickrAuthKey);
NSLog(@"set flickrUsername to %@", [Session sharedInstance].flickrUsername);
NSLog(@"set flickrFullname to %@", [Session sharedInstance].flickrFullname);
NSLog(@"set flickrNsid to %@", [Session sharedInstance].flickrNsid);
}
else if([queryType isEqualToString:@"query"])
{
/*
<photos page="2" pages="89" perpage="10" total="881">
<photo id="2636" owner="47058503995@N01"
secret="a123456" server="2" title="test_04"
ispublic="1" isfriend="0" isfamily="0" />
<photo id="2635" owner="47058503995@N01"
secret="b123456" server="2" title="test_03"
ispublic="0" isfriend="1" isfamily="1" />
<photo id="2633" owner="47058503995@N01"
secret="c123456" server="2" title="test_01"
ispublic="1" isfriend="0" isfamily="0" />
<photo id="2610" owner="12037949754@N01"
secret="d123456" server="2" title="00_tall"
ispublic="1" isfriend="0" isfamily="0" />
</photos>
//get back a dict that looks like this
//kick off a thread to cache returned photos
*/
//[Session inspectDictionary:inResponseDictionary];
NSDictionary *photosDict = [inResponseDictionary objectForKey:@"photos"];
NSArray *photos = [photosDict objectForKey:@"photo"];
for(int i=0; i<[photos count]; i++)
{
NSDictionary *photoDict = [photos objectAtIndex:i];
//NSLog(@"photo %i has id %@ and url %@", i, [photoDict objectForKey:@"id"], [photoDict objectForKey:@"url_sq"]);
//[Session inspectDictionary:photoDict];
//[NSThread detachNewThreadSelector:@selector(cachePhoto:) toTarget:self withObject:photoDict];
NSLog(@"caching photo: %@", [photoDict objectForKey:@"id"]);
FlickrPhoto *photo = [[FlickrPhoto alloc] initWithDict:photoDict];
[[PhotoCache sharedInstance] cachePhoto:photo];
}
}
}
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didFailWithError:(NSError *)inError
{
NSLog(@"flickr request of type %@ failed with error %@", queryType, [inError localizedDescription]);
if([queryType isEqualToString:@"getAuthToken"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Authenticating"
message:[NSString stringWithFormat:@"Could not authenticate with Flickr: %@", [inError localizedDescription]]
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
else if([queryType isEqualToString:@"query"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Query Error"
message:[NSString stringWithFormat:@"There was an error querying Flickr: %@", [inError localizedDescription]]
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest imageUploadSentBytes:(NSUInteger)inSentBytes totalBytes:(NSUInteger)inTotalBytes
{
NSLog(@"sending bytes to flickr");
}
@end