-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVFlickrPhotoService.m
107 lines (93 loc) · 3.47 KB
/
MVFlickrPhotoService.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
#import "MVFlickrPhotoService.h"
#import "MVJSONGetRequest.h"
#define kMVFlickrAPIKey @"4e41bc0965b8a101fabf5b63c72db069"
@interface MVFlickrPhotoService ()
@property (strong, readwrite) NSURL *url;
@property (readwrite) long long photoId;
@property (strong, readwrite) NSString *title;
@property (strong, readwrite) NSURL *imageUrl;
@property (readwrite) BOOL informationFetched;
@property (readwrite) BOOL error;
@end
@implementation MVFlickrPhotoService
@synthesize url = url_,
photoId = photoId_,
title = title_,
imageUrl = imageUrl_,
informationFetched = informationFetched_,
error = error_;
- (id)initWithURL:(NSURL*)url
photoId:(long long)photoId
{
self = [super init];
if(self)
{
url_ = url;
photoId_ = photoId;
title_ = nil;
imageUrl_ = nil;
informationFetched_ = NO;
error_ = NO;
}
return self;
}
#pragma mark -
#pragma mark MVService Methods
- (void)fetchInformation
{
NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=%@&photo_id=%lld&format=json&nojsoncallback=1",
kMVFlickrAPIKey, self.photoId];
NSURL *url = [NSURL URLWithString:urlString];
MVJSONGetRequest *getRequest = [[MVJSONGetRequest alloc] initWithURL:url];
[getRequest get:^(NSObject *object) {
@try {
NSDictionary *dictionary = (NSDictionary*)object;
if(dictionary && [dictionary valueForKey:@"photo"])
{
NSDictionary *photo = [dictionary valueForKey:@"photo"];
self.title = [[photo valueForKey:@"title"] valueForKey:@"_content"];
NSString *farm = [photo valueForKey:@"farm"];
NSString *server = [photo valueForKey:@"server"];
NSString *secret = [photo valueForKey:@"secret"];
NSString *imageUrlString = [NSString stringWithFormat:
@"http://farm%@.staticflickr.com/%@/%lld_%@.jpg",
farm, server, self.photoId, secret];
self.imageUrl = [NSURL URLWithString:imageUrlString];
}
else
{
self.error = YES;
}
}
@catch (NSException *exception) {
self.error = YES;
}
self.informationFetched = YES;
}];
}
+ (MVFlickrPhotoService*)serviceForURL:(NSURL*)url;
{
if([url.host isEqualToString:@"flickr.com"] || [url.host isEqualToString:@"www.flickr.com"])
{
NSString *pattern = @"\\/photos\\/.[^\\/]*\\/([0-9]*)\\/?";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *matches = [regex matchesInString:url.path
options:0
range:NSMakeRange(0, url.path.length)];
if(matches.count == 1)
{
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSRange photoIdRange = [match rangeAtIndex:1];
long long photoId = [[url.path substringWithRange:photoIdRange] longLongValue];
if(photoId == 0)
return nil;
MVFlickrPhotoService *service = [[MVFlickrPhotoService alloc] initWithURL:url
photoId:photoId];
return service;
}
}
return nil;
}
@end