VLCMDFBrowsingArtworkProvider.m 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCMDFBrowsingArtworkProvider.h"
#import "MetaDataFetcherKit.h"

15
@interface VLCMDFBrowsingArtworkProvider () <MDFMovieDBFetcherDataRecipient>
16
{
17
    MDFMovieDBFetcher *_tmdbFetcher;
18 19 20 21 22 23 24 25
}

@end

@implementation VLCMDFBrowsingArtworkProvider

- (void)reset
{
26 27
    if (_tmdbFetcher) {
        [_tmdbFetcher cancelAllRequests];
28
    } else {
29 30 31 32 33 34 35 36
        _tmdbFetcher = [[MDFMovieDBFetcher alloc] init];
        _tmdbFetcher.dataRecipient = self;
        _tmdbFetcher.shouldDecrapifyInputStrings = YES;
    }
}

- (void)setSearchForAudioMetadata:(BOOL)searchForAudioMetadata
{
37
    NSLog(@"there is currently no audio metadata fetcher :-(");
38 39 40 41
}

- (void)searchForArtworkForVideoRelatedString:(NSString *)string
{
42
    [_tmdbFetcher searchForMovie:string];
43 44 45 46 47 48
}

#pragma mark - MDFMovieDB

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFindMovie:(MDFMovie *)details forSearchRequest:(NSString *)searchRequest
{
49
    if (details == nil) {
50
        return;
51
    }
52 53
    [aFetcher cancelAllRequests];
    MDFMovieDBSessionManager *sessionManager = [MDFMovieDBSessionManager sharedInstance];
54
    if (!sessionManager.hasFetchedProperties) {
55
        return;
56
    }
57 58 59

    if (details.movieDBID == 0) {
        /* we found nothing, let's see if it's a TV show */
60
        [_tmdbFetcher searchForTVShow:searchRequest];
61 62 63 64 65 66 67 68
        return;
    }

    NSString *imagePath = details.posterPath;
    NSArray *sizes = sessionManager.posterSizes;
    NSString *imageSize;

    if (sizes != nil) {
69 70
        NSUInteger count = sizes.count;
        if (count > 1) {
71
            imageSize = sizes[1];
72
        } else if (count > 0) {
73 74 75 76 77 78 79 80 81 82 83
            imageSize = sizes.firstObject;
        }
    }

    if (!imagePath) {
        imagePath = details.backdropPath;
        sizes = sessionManager.backdropSizes;
        if (sizes != nil && sizes.count > 0) {
            imageSize = sizes.firstObject;
        }
    }
84
    if (!imagePath) {
85
        return;
86
    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    NSString *thumbnailURLString = [NSString stringWithFormat:@"%@%@%@",
                                    sessionManager.imageBaseURL,
                                    imageSize,
                                    imagePath];
    self.artworkReceiver.thumbnailURL = [NSURL URLWithString:thumbnailURLString];
}

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFailToFindMovieForSearchRequest:(NSString *)searchRequest
{
    APLog(@"Failed to find a movie for '%@'", searchRequest);
}

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFindTVShow:(MDFTVShow *)details forSearchRequest:(NSString *)searchRequest
{
102
    if (details == nil) {
103
        return;
104 105
    }

106 107 108 109 110 111 112 113 114 115
    [aFetcher cancelAllRequests];
    MDFMovieDBSessionManager *sessionManager = [MDFMovieDBSessionManager sharedInstance];
    if (!sessionManager.hasFetchedProperties)
        return;

    NSString *imagePath = details.posterPath;
    NSArray *sizes = sessionManager.posterSizes;
    NSString *imageSize;

    if (sizes != nil) {
116 117
        NSUInteger count = sizes.count;
        if (count > 1) {
118
            imageSize = sizes[1];
119
        } else if (count > 0) {
120 121 122 123 124 125 126 127 128 129 130
            imageSize = sizes.firstObject;
        }
    }

    if (!imagePath) {
        imagePath = details.backdropPath;
        sizes = sessionManager.backdropSizes;
        if (sizes != nil && sizes.count > 0) {
            imageSize = sizes.firstObject;
        }
    }
131
    if (!imagePath) {
132
        return;
133
    }
134 135 136 137 138 139 140 141 142 143 144 145 146 147

    NSString *thumbnailURLString = [NSString stringWithFormat:@"%@%@%@",
                                    sessionManager.imageBaseURL,
                                    imageSize,
                                    imagePath];
    self.artworkReceiver.thumbnailURL = [NSURL URLWithString:thumbnailURLString];
}

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFailToFindTVShowForSearchRequest:(NSString *)searchRequest
{
    APLog(@"failed to find TV show");
}

@end