1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 09:27:55 +01:00
Telegram/Telegraph/TGInstagramMediaIdSignal.m
2015-10-01 19:19:52 +03:00

46 lines
1.6 KiB
Objective-C

#import "TGInstagramMediaIdSignal.h"
#import "TGRemoteHttpLocationSignal.h"
@implementation TGInstagramMediaIdSignal
+ (id)objectAtDictionaryPath:(NSString *)path dictionary:(NSDictionary *)dictionary
{
if ([dictionary respondsToSelector:@selector(objectForKey:)])
{
NSRange range = [path rangeOfString:@"."];
if (range.location == NSNotFound)
return dictionary[path];
else
return [self objectAtDictionaryPath:[path substringFromIndex:range.location + 1] dictionary:dictionary[[path substringToIndex:range.location]]];
}
return nil;
}
+ (SSignal *)instagramMediaIdForShortcode:(NSString *)shortcode
{
NSString *url = [[NSString alloc] initWithFormat:@"http://api.instagram.com/oembed?url=http://instagram.com/p/%@", shortcode];
return [[TGRemoteHttpLocationSignal dataForHttpLocation:url] mapToSignal:^SSignal *(NSData *response)
{
NSString *mediaId = nil;
@try {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:response options:0 error:nil];
if ([dict respondsToSelector:@selector(objectForKey:)])
{
NSString *dictMediaId = [self objectAtDictionaryPath:@"media_id" dictionary:dict];
if ([dictMediaId respondsToSelector:@selector(characterAtIndex:)])
mediaId = dictMediaId;
}
} @catch(__unused id exception) {
}
if (mediaId == nil)
return [SSignal fail:nil];
else
return [SSignal single:mediaId];
}];
}
@end