VLCMDFBrowsingArtworkProvider.m 6.37 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, MDFHatchetFetcherDataRecipient>
16
{
17 18
    MDFMovieDBFetcher *_tmdbFetcher;
    MDFHatchetFetcher *_hatchetFetcher;
19 20 21 22 23 24 25 26
}

@end

@implementation VLCMDFBrowsingArtworkProvider

- (void)reset
{
27 28 29 30
    if (_tmdbFetcher) {
        [_tmdbFetcher cancelAllRequests];
        if (_hatchetFetcher)
            [_hatchetFetcher cancelAllRequests];
31
    } else {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        _tmdbFetcher = [[MDFMovieDBFetcher alloc] init];
        _tmdbFetcher.dataRecipient = self;
        _tmdbFetcher.shouldDecrapifyInputStrings = YES;

        if (_searchForAudioMetadata) {
            _hatchetFetcher = [[MDFHatchetFetcher alloc] init];
            _hatchetFetcher.dataRecipient = self;
        }
    }
}

- (void)setSearchForAudioMetadata:(BOOL)searchForAudioMetadata
{
    if (searchForAudioMetadata) {
        _hatchetFetcher = [[MDFHatchetFetcher alloc] init];
        _hatchetFetcher.dataRecipient = self;
48
    }
49
    _searchForAudioMetadata = searchForAudioMetadata;
50 51 52 53
}

- (void)searchForArtworkForVideoRelatedString:(NSString *)string
{
54
    [_tmdbFetcher searchForMovie:string];
55 56 57 58 59 60
}

#pragma mark - MDFMovieDB

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFindMovie:(MDFMovie *)details forSearchRequest:(NSString *)searchRequest
{
61
    if (details == nil) {
62
        return;
63
    }
64 65
    [aFetcher cancelAllRequests];
    MDFMovieDBSessionManager *sessionManager = [MDFMovieDBSessionManager sharedInstance];
66
    if (!sessionManager.hasFetchedProperties) {
67
        return;
68
    }
69 70 71

    if (details.movieDBID == 0) {
        /* we found nothing, let's see if it's a TV show */
72
        [_tmdbFetcher searchForTVShow:searchRequest];
73 74 75 76 77 78 79 80
        return;
    }

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

    if (sizes != nil) {
81 82
        NSUInteger count = sizes.count;
        if (count > 1) {
83
            imageSize = sizes[1];
84
        } else if (count > 0) {
85 86 87 88 89 90 91 92 93 94 95
            imageSize = sizes.firstObject;
        }
    }

    if (!imagePath) {
        imagePath = details.backdropPath;
        sizes = sessionManager.backdropSizes;
        if (sizes != nil && sizes.count > 0) {
            imageSize = sizes.firstObject;
        }
    }
96
    if (!imagePath) {
97
        return;
98
    }
99 100 101 102 103 104 105 106 107 108 109

    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);
110 111 112 113

    if (_searchForAudioMetadata) {
        [_hatchetFetcher searchForArtist:searchRequest];
    }
114 115 116 117
}

- (void)MDFMovieDBFetcher:(MDFMovieDBFetcher *)aFetcher didFindTVShow:(MDFTVShow *)details forSearchRequest:(NSString *)searchRequest
{
118 119 120 121
    if (details == nil) {
        if (_searchForAudioMetadata) {
            [_hatchetFetcher searchForArtist:searchRequest];
        }
122
        return;
123 124
    }

125 126 127 128 129 130 131 132 133 134
    [aFetcher cancelAllRequests];
    MDFMovieDBSessionManager *sessionManager = [MDFMovieDBSessionManager sharedInstance];
    if (!sessionManager.hasFetchedProperties)
        return;

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

    if (sizes != nil) {
135 136
        NSUInteger count = sizes.count;
        if (count > 1) {
137
            imageSize = sizes[1];
138
        } else if (count > 0) {
139 140 141 142 143 144 145 146 147 148 149
            imageSize = sizes.firstObject;
        }
    }

    if (!imagePath) {
        imagePath = details.backdropPath;
        sizes = sessionManager.backdropSizes;
        if (sizes != nil && sizes.count > 0) {
            imageSize = sizes.firstObject;
        }
    }
150 151 152 153
    if (!imagePath) {
        if (_searchForAudioMetadata) {
            [_hatchetFetcher searchForArtist:searchRequest];
        }
154
        return;
155
    }
156 157 158 159 160 161 162 163 164 165 166

    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");
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

    if (_searchForAudioMetadata) {
        [_hatchetFetcher searchForArtist:searchRequest];
    }
}

- (void)MDFHatchetFetcher:(MDFHatchetFetcher *)aFetcher didFindArtist:(MDFArtist *)artist forSearchRequest:(NSString *)searchRequest
{
    /* we have no match */
    if (!artist) {
        [self _simplifyMetaDataSearchString:searchRequest];
        return;
    }

    NSArray *imageURLStrings = artist.largeSizedImages;
    NSString *imageURLString;

    if (imageURLStrings.count > 0) {
        imageURLString = imageURLStrings.firstObject;
    } else {
        imageURLStrings = artist.mediumSizedImages;
        if (imageURLStrings.count > 0) {
            imageURLString = imageURLStrings.firstObject;
        }
    }

    if (imageURLString) {
        self.artworkReceiver.thumbnailURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?height=300&width=250",imageURLString]];
    } else {
        [self _simplifyMetaDataSearchString:searchRequest];
    }
}

- (void)MDFHatchetFetcher:(MDFHatchetFetcher *)aFetcher didFailToFindArtistForSearchRequest:(NSString *)searchRequest
{
    [self _simplifyMetaDataSearchString:searchRequest];
}

- (void)_simplifyMetaDataSearchString:(NSString *)searchString
{
    NSRange lastRange = [searchString rangeOfString:@" " options:NSBackwardsSearch];
    if (lastRange.location != NSNotFound)
        [_hatchetFetcher searchForArtist:[searchString substringToIndex:lastRange.location]];
210 211 212
}

@end