MLTVShowEpisodesInfoGrabber.m 5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*****************************************************************************
 * MLTVShowEpisodesInfoGrabber.m
 * Lunettes
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
 * Copyright (C) 2010-2013 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Pierre's avatar
Pierre committed
26 27 28

#import "MLTVShowEpisodesInfoGrabber.h"
#import "TheTVDBGrabber.h"
Pierre's avatar
Pierre committed
29
#import "MLURLConnection.h"
Pierre's avatar
Pierre committed
30 31

@interface MLTVShowEpisodesInfoGrabber ()
Pierre's avatar
Pierre committed
32 33 34 35
#if !HAVE_BLOCK
    <MLURLConnectionDelegate>
#endif

36 37
@property (readwrite, strong) NSDictionary *results;
@property (readwrite, strong) NSArray *episodesResults;
Pierre's avatar
Pierre committed
38 39 40 41 42 43 44 45

- (void)didReceiveData:(NSData *)data;
@end

@implementation MLTVShowEpisodesInfoGrabber
@synthesize delegate=_delegate;
@synthesize episodesResults=_episodesResults;
@synthesize results=_results;
Pierre's avatar
Pierre committed
46 47 48
#if !HAVE_BLOCK
@synthesize userData=_userData;
#endif
Pierre's avatar
Pierre committed
49

Pierre's avatar
Pierre committed
50 51 52 53 54 55 56 57 58 59 60
#if !HAVE_BLOCK
- (void)urlConnection:(MLURLConnection *)connection didFinishWithError:(NSError *)error
{
    if (error) {
        _connection = nil;
        return;
    }
    [self didReceiveData:connection.data];
}
#endif

Pierre's avatar
Pierre committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
- (void)lookUpForShowID:(NSString *)showId
{
    [_connection cancel];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TVDB_QUERY_EPISODE_INFO, TVDB_HOSTNAME, TVDB_API_KEY, showId, TVDB_DEFAULT_LANGUAGE]];

    // Balanced below

#if HAVE_BLOCK
    _connection = [[MLURLConnection runConnectionWithURL:url andBlock:^(MLURLConnection *connection, NSError * error) {
        if (error) {
            [_connection release];
            _connection = nil;
            [self autorelease];
            return;
        }
        [self didReceiveData:connection.data];
        [self autorelease];
    }] retain];
#else
81
    _connection = [MLURLConnection runConnectionWithURL:url delegate:self userObject:nil];
Pierre's avatar
Pierre committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#endif
}

#if HAVE_BLOCK
- (void)lookUpForShowID:(NSString *)id andExecuteBlock:(void (^)())block
{
    Block_release(_block);
    _block = Block_copy(block);
    [self lookUpForShowID:id];
}
#endif

- (void)didReceiveData:(NSData *)data
{
    NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:data options:0 error:nil];

    NSError *error = nil;
    NSArray *nodesSerie = [xmlDoc nodesForXPath:@"./Data/Series" error:&error];
    NSArray *nodesEpisode = [xmlDoc nodesForXPath:@"./Data/Episode" error:&error];


    NSString *serieArtworkURL = nil;
    if ([nodesSerie count] == 1) {
105
        NSXMLNode *node = nodesSerie[0];
Pierre's avatar
Pierre committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        serieArtworkURL = [node stringValueForXPath:@"./poster"];
    }

    if ([nodesEpisode count] > 0 ) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:[nodesEpisode count]];

        for (NSXMLNode *node in nodesEpisode) {
            NSString *episodeId = [node stringValueForXPath:@"./id"];
            if (!episodeId)
                continue;
            NSString *title = [node stringValueForXPath:@"./EpisodeName"];
            NSNumber *seasonNumber = [node numberValueForXPath:@"./SeasonNumber"];
            NSNumber *episodeNumber = [node numberValueForXPath:@"./EpisodeNumber"];
            NSString *artworkURL = [node stringValueForXPath:@"./filename"];
            NSString *shortSummary = [node stringValueForXPath:@"./Overview"];
121 122 123 124 125 126
            [array addObject:@{@"id": episodeId,
                              @"title": title ?: @"",
                              @"shortSummary": shortSummary ?: @"",
                              @"episodeNumber": episodeNumber,
                              @"seasonNumber": seasonNumber,
                              @"artworkURL": [NSString stringWithFormat:TVDB_COVERS_URL, TVDB_IMAGES_HOSTNAME, artworkURL]}];
Pierre's avatar
Pierre committed
127 128
        }
        self.episodesResults = array;
129
        self.results = @{@"serieArtworkURL": [NSString stringWithFormat:TVDB_COVERS_URL, TVDB_IMAGES_HOSTNAME, serieArtworkURL]};
Pierre's avatar
Pierre committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }
    else {
        self.episodesResults = nil;
        self.results = nil;

    }


#if HAVE_BLOCK
    if (_block) {
        _block();
        Block_release(_block);
        _block = NULL;
    }
#endif

    if ([_delegate respondsToSelector:@selector(movieInfoGrabberDidFinishGrabbing:)])
        [_delegate tvShowEpisodesInfoGrabberDidFinishGrabbing:self];
}

@end