forked from kevintuhumury/itvdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TVDbShow.m
164 lines (132 loc) · 5.61 KB
/
TVDbShow.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
//
// TVDbShow.m
// iTVDb
//
// Created by Kevin Tuhumury on 7/10/12.
// Copyright (c) 2012 Thmry. All rights reserved.
//
#import "TVDbShow.h"
#import "TVDbClient.h"
#import "TVDbEpisode.h"
#import "TVDbImage.h"
#import "XMLReader.h"
#import "NSString+Helper.h"
@interface TVDbShow()
+ (void)buildShowWithDictionary:(NSDictionary *)dictionary reference:(NSMutableArray **)reference;
+ (NSString *)searchTermUrl:(NSString *)term;
+ (NSString *)searchTerm:(NSString *)term;
+ (NSString *)showUrl:(NSNumber *)showId;
- (void)buildEpisodesWithDictionary:(NSDictionary *)dictionary;
- (void)buildEpisodeWithDictionary:(NSDictionary *)dictionary reference:(NSMutableArray **)reference;
@end
@implementation TVDbShow
@synthesize showId, title, description, banner, bannerThumbnail, imdbId, premiereDate;
@synthesize status, genre, actors, poster, posterThumbnail, airDay, airTime, runtime, network, contentRating, rating, episodes;
#pragma mark - initializers
- (TVDbShow *)initWithDictionary:(NSDictionary *)dictionary
{
if (self = [super init])
{
NSDictionary *showDictionary = dictionary;
if ([dictionary retrieveForPath:@"Series"])
{
showDictionary = [dictionary retrieveForPath:@"Series"];
}
// properties retrieved by a basic search request
self.showId = [NSNumber numberWithInt:[[showDictionary retrieveForPath:@"id"] intValue]];
self.title = [showDictionary retrieveForPath:@"SeriesName"];
self.description = [showDictionary retrieveForPath:@"Overview"];
self.imdbId = [showDictionary retrieveForPath:@"IMDB_ID"];
self.premiereDate = [NSString stringToDate:[showDictionary retrieveForPath:@"FirstAired"]];
if ([showDictionary retrieveForPath:@"banner"])
{
TVDbImage *bannerImage = [[TVDbImage alloc] initWithUrl:[showDictionary retrieveForPath:@"banner"]];
self.banner = [bannerImage url];
self.bannerThumbnail = [bannerImage thumbnailUrl];
}
// properties retrieved by a detailed series search request
self.status = [showDictionary retrieveForPath:@"Status"];
self.genre = [NSString pipedStringToArray:[showDictionary retrieveForPath:@"Genre"]];
self.actors = [NSString pipedStringToArray:[showDictionary retrieveForPath:@"Actors"]];
self.airDay = [showDictionary retrieveForPath:@"Airs_DayOfWeek"];
self.airTime = [showDictionary retrieveForPath:@"Airs_Time"];
self.runtime = [showDictionary retrieveForPath:@"Runtime"];
self.network = [showDictionary retrieveForPath:@"Network"];
self.contentRating = [showDictionary retrieveForPath:@"ContentRating"];
self.rating = [showDictionary retrieveForPath:@"Rating"];
if ([showDictionary retrieveForPath:@"poster"])
{
TVDbImage *posterImage = [[TVDbImage alloc] initWithUrl:[showDictionary retrieveForPath:@"poster"]];
self.poster = [posterImage url];
self.posterThumbnail = [posterImage thumbnailUrl];
}
if ([dictionary retrieveForPath:@"Episode"])
{
[self buildEpisodesWithDictionary:[dictionary retrieveForPath:@"Episode"]];
}
}
return self;
}
# pragma mark - class methods
+ (NSMutableArray *)findByName:(NSString *)name
{
id response = [[[TVDbClient sharedInstance] requestURL:[self searchTermUrl:name]] retrieveForPath:@"Data.Series"];
NSMutableArray *shows = [NSMutableArray array];
if ([response isKindOfClass:[NSDictionary class]])
{
[self buildShowWithDictionary:response reference:&shows];
}
if ([response isKindOfClass:[NSArray class]])
{
for (id showDictionary in response)
{
[self buildShowWithDictionary:showDictionary reference:&shows];
}
}
return shows;
}
+ (TVDbShow *)findById:(NSNumber *)showId
{
NSDictionary *responseDictionary = [[TVDbClient sharedInstance] requestURL:[self showUrl:showId]];
return [[TVDbShow alloc] initWithDictionary:[responseDictionary retrieveForPath:@"Data"]];
}
# pragma mark - internal methods
+ (void)buildShowWithDictionary:(NSDictionary *)dictionary reference:(NSMutableArray **)reference
{
TVDbShow *show = [[TVDbShow alloc] initWithDictionary:dictionary];
[*reference addObject:show];
}
+ (NSString *)searchTermUrl:(NSString *)term
{
return [NSString stringWithFormat: @"GetSeries.php?seriesname=%@&language=%@", [self searchTerm:term], [[TVDbClient sharedInstance] language]];
}
+ (NSString *)searchTerm:(NSString *)term
{
return [term stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
}
+ (NSString *)showUrl:(NSNumber *)showId
{
return [[[TVDbClient sharedInstance] apiKey] stringByAppendingString:[NSString stringWithFormat:@"/series/%@/all/", showId]];
}
- (void)buildEpisodesWithDictionary:(NSDictionary *)dictionary
{
NSMutableArray *showEpisodes = [NSMutableArray array];
if ([dictionary isKindOfClass:[NSDictionary class]])
{
[self buildEpisodeWithDictionary:dictionary reference:&showEpisodes];
}
if ([dictionary isKindOfClass:[NSArray class]])
{
for (id episodeDictionary in dictionary)
{
[self buildEpisodeWithDictionary:episodeDictionary reference:&showEpisodes];
}
}
self.episodes = showEpisodes;
}
- (void)buildEpisodeWithDictionary:(NSDictionary *)dictionary reference:(NSMutableArray **)reference
{
TVDbEpisode *episode = [[TVDbEpisode alloc] initWithDictionary:dictionary];
[*reference addObject:episode];
}
@end