-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaObject.m
401 lines (331 loc) · 14.1 KB
/
MediaObject.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// MediaObject.m
// KyckClient
//
// Created by Kevin Musselman on 10/30/12.
//
//
#import "MediaObject.h"
typedef void(^OemebdCB)(NSDictionary*);
@interface MediaObject()
@property (nonatomic, readwrite) BOOL isRetrieving;
@end
@implementation MediaObject
@synthesize isRetrieving;
@synthesize delegate, originalURL, mediaID, mediaURL, photoRatio, photoURL, mtype, vtype, memoryCache, numTries, tryAgain;
+ (MemoryCache *)globalMemoryCache
{
static MemoryCache *sharedCache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedCache = [[MemoryCache alloc] init];
});
return sharedCache;
}
-(id)initWithDelegate:(id<MediaObjectDelegate>)del
{
if((self=[self init]))
{
self.delegate = del;
}
return self;
}
-(id)init
{
if((self=[super init]))
{
self.memoryCache = [MediaObject globalMemoryCache];
originalURL = @"";
photoRatio = [NSNumber numberWithInt:1];
mediaURL = @"";
mediaID = @"";
numTries = 0;
tryAgain = YES;
isRetrieving = NO;
}
return self;
}
-(BOOL)parseYoutube
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:youtube\\.com\\/(?:[^\\/]+\\/.+\\/|(?:v|e(?:mbed)?)\\/|.*[?&]v=)|youtu\\.be\\/)([^\"&?\\/ \"]{11})" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:originalURL options:0 range:NSMakeRange(0, [originalURL length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>1)
{
mediaID = [originalURL substringWithRange:[match rangeAtIndex:1]];
mediaURL = [NSString stringWithFormat:@"http://www.youtube.com/embed/%@", mediaID];
photoURL = [NSString stringWithFormat:@"http://img.youtube.com/vi/%@/0.jpg", mediaID];
photoRatio = [NSNumber numberWithFloat:1.3333];
mtype = video;
vtype = youtube;
[self storeInMemory];
return YES;
}
return NO;
}
-(BOOL)parseDailyMotion
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:dailymotion\\.com\\/(?:embed\\/)?video\\/(?:([^_]+).*))|(?:dai\\.ly\\/(.*))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:originalURL options:0 range:NSMakeRange(0, [originalURL length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>1)
{
mtype = video;
vtype = dailymotion;
if ([match rangeAtIndex:1].location != NSNotFound)
{
mediaID = [originalURL substringWithRange:[match rangeAtIndex:1]];
mediaURL = [NSString stringWithFormat:@"http://www.dailymotion.com/embed/video/%@", mediaID];
photoURL = [NSString stringWithFormat:@"http://www.dailymotion.com/thumbnail/video/%@", mediaID];
photoRatio = [NSNumber numberWithFloat:1.3333];
}
[self storeInMemory];
return YES;
}
return NO;
}
-(BOOL)parseFlicker
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:flickr\\.com\\/|flic\\.kr\\/)" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:originalURL options:0 range:NSMakeRange(0, [originalURL length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>0)
{
mediaURL = originalURL;
mtype = photo;
vtype = othervideo;
[self storeInMemory];
return YES;
}
return NO;
}
-(BOOL)parseInstagram
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:instagram\\.com\\/|instagr\\.am\\/)" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:originalURL options:0 range:NSMakeRange(0, [originalURL length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>0)
{
mediaURL = originalURL;
mtype = photo;
vtype = othervideo;
[self storeInMemory];
return YES;
}
return NO;
}
-(void)makeOembedCall:(NSString *)baseURL withCallback:(OemebdCB)cb
{
isRetrieving = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/services/oembed?format=json&url=%@", baseURL, self.originalURL]]];
NSError* networkError = nil;
NSURLResponse* response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&networkError];
// If we get a 404 error then the request will not fail with an error, so only let successful
// responses pass.
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 300) {
networkError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
isRetrieving = NO;
if (nil != networkError) {
tryAgain = NO;
[self storeInMemory];
return;
}
else
{
NSError *error = nil;
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if ([obj isKindOfClass:[NSDictionary class]]) {
cb(obj);
}
}
});
});
}
-(void)setParseUrl:(NSString *)url andRetrieve:(BOOL)ret
{
originalURL = url;
NSDictionary *obj = [self.memoryCache objectWithName:originalURL];
if(obj)
{
if(obj[@"mediaURL"]) mediaURL = obj[@"mediaURL"];
if(obj[@"mediaID"]) mediaID = obj[@"mediaID"];
if(obj[@"photoURL"] && ![obj[@"photoURL"] isEqualToString:@""]) photoURL = obj[@"photoURL"];
if(obj[@"photoRatio"]) photoRatio = obj[@"photoRatio"];
if(obj[@"mtype"]) mtype = [obj[@"mtype"] intValue];
if(obj[@"vtype"]) vtype = [obj[@"vtype"] intValue];
if (obj[@"numTries"]) numTries = [obj[@"numTries"] intValue];
if (obj[@"tryAgain"]) tryAgain = [obj[@"tryAgain"] boolValue];
if (ret)
{
if (!(!photoURL || [photoURL isEqualToString:@""]) || !tryAgain || numTries>3 || isRetrieving)
{
return;
}
}
else
{
return;
}
}
numTries = 0;
tryAgain = YES;
mtype = othermedia;
vtype = othervideo;
if (ret) {
photoRatio = [NSNumber numberWithInt:1];
photoURL = nil;
photoRatio = nil;
[self parseURLandRetrieve];
}
else
{
[self parseURL];
}
}
-(void)parseURL
{
if ([self parseYoutube]) {
}
else if([self parseDailyMotion])
{
}
else if([self parseFlicker])
{
}
else if([self parseInstagram])
{
}
}
-(void)parseURLandRetrieve
{
if ([self parseYoutube]) {
}
else if([self parseDailyMotion])
{
if (!self.mediaURL || [self.mediaURL isEqualToString:@""])
{
[self makeOembedCall:@"http://www.dailymotion.com" withCallback:^(NSDictionary *obj) {
NSString *html = [obj objectForKey:@"html"];
if(html)
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:src=(?:\"|\')([^\"\']+))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:html options:0 range:NSMakeRange(0, [html length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>1)
{
self.mediaURL = [html substringWithRange:[match rangeAtIndex:1]];
}
}
NSString *thumb = [obj objectForKey:@"thumbnail_url"];
if(thumb)
{
self.photoURL = thumb;
CGFloat width = [[obj objectForKey:@"thumbnail_width"] floatValue];
CGFloat height = [[obj objectForKey:@"thumbnail_height"] floatValue];
if (height>0) {
CGFloat ratio = width/height;
self.photoRatio = [NSNumber numberWithFloat:ratio];
if ([self.delegate respondsToSelector:@selector(didSetPhotoURL)]) {
[self.delegate didSetPhotoURL];
}
}
}
[self storeInMemory];
}];
}
}
else if([self parseFlicker])
{
self.numTries++;
[self storeInMemory];
[self makeOembedCall:@"http://www.flickr.com" withCallback:^(NSDictionary *obj) {
NSString *thumb = [obj objectForKey:@"thumbnail_url"];
if(thumb)
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([^_]+_?[A-Z0-9]+)(_.)?(\\..*)" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *matches = [regex matchesInString:thumb options:0 range:NSMakeRange(0, [thumb length])];
NSTextCheckingResult *match = (matches.count>0 ? [matches objectAtIndex:0] : nil);
if (match && match.numberOfRanges>2)
{
NSString * p1 = [thumb substringWithRange:[match rangeAtIndex:1]];
NSString * p2 = [thumb substringWithRange:[match rangeAtIndex:3]];
if(![match rangeAtIndex:1].location == NSNotFound)
{
}
NSString *final = [NSString stringWithFormat:@"%@%@", p1, p2];
self.photoURL = final;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:self.photoURL]];
NSError* networkError = nil;
NSURLResponse* response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&networkError];
// If we get a 404 error then the request will not fail with an error, so only let successful
// responses pass.
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 300) {
networkError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil];
}
}
if (nil != networkError) {
dispatch_async(dispatch_get_main_queue(), ^{
tryAgain = NO;
[self storeInMemory];
return;
});
}
UIImage *img = [[UIImage alloc] initWithData:data];
CGFloat ratio = (img ? img.size.width/img.size.height : 0);
dispatch_async(dispatch_get_main_queue(), ^{
self.photoRatio = [NSNumber numberWithFloat:ratio];
if ([self.delegate respondsToSelector:@selector(didSetPhotoURL)]) {
[self.delegate didSetPhotoURL];
}
[self storeInMemory];
});
});
}
}
else
{
[self storeInMemory];
}
}];
}
else if([self parseInstagram])
{
self.numTries++;
[self storeInMemory];
[self makeOembedCall:@"http://api.instagram.com" withCallback:^(NSDictionary *obj) {
NSString *thumb = [obj objectForKey:@"url"];
if(thumb)
{
self.photoURL = thumb;
CGFloat width = [[obj objectForKey:@"width"] floatValue];
CGFloat height = [[obj objectForKey:@"height"] floatValue];
if (height>0) {
CGFloat ratio = width/height;
self.photoRatio = [NSNumber numberWithFloat:ratio];
if ([self.delegate respondsToSelector:@selector(didSetPhotoURL)]) {
[self.delegate didSetPhotoURL];
}
}
}
[self storeInMemory];
}];
}
}
-(void)storeInMemory
{
NSDictionary *dict = @{@"mediaURL":mediaURL, @"mediaID": (mediaID ? mediaID : @""), @"photoURL": (photoURL ? photoURL : @""), @"photoRatio": (photoRatio ? photoRatio : @""), @"mtype":[NSNumber numberWithInt:mtype], @"vtype":[NSNumber numberWithInt:vtype], @"numTries":[NSNumber numberWithInt:numTries], @"tryAgain":[NSNumber numberWithBool:tryAgain]};
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:600];
[self.memoryCache storeObject:dict withName:originalURL expiresAfter:dt];
}
@end