-
Notifications
You must be signed in to change notification settings - Fork 6
/
TVDbEpisode.m
109 lines (88 loc) · 4.33 KB
/
TVDbEpisode.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
//
// TVDbEpisode.m
// iTVDb
//
// Created by Kevin Tuhumury on 7/10/12.
// Copyright (c) 2012 Thmry. All rights reserved.
//
#import "TVDbEpisode.h"
#import "TVDbClient.h"
#import "TVDbImage.h"
#import "XMLReader.h"
#import "NSString+Helper.h"
@interface TVDbEpisode()
+ (NSString *)episodeUrl:(NSNumber *)episodeId;
+ (NSString *)episodeUrlByShowId:(NSNumber *)showId seasonNumber:(NSNumber *)seasonNumber episodeNumber:(NSNumber *)episodeNumber;
@end
@implementation TVDbEpisode
@synthesize episodeId, title, description, seasonNumber, episodeNumber, banner, bannerThumbnail, writer, director, gueststars, imdbId, premiereDate, rating, showId;
#pragma mark - initializers
- (TVDbEpisode *)initWithDictionary:(NSDictionary *)dictionary
{
if (self = [super init])
{
self.episodeId = [NSNumber numberWithInt:[[dictionary retrieveForPath:@"id"] intValue]];
self.title = [dictionary retrieveForPath:@"EpisodeName"];
self.description = [dictionary retrieveForPath:@"Overview"];
self.seasonNumber = [NSNumber numberWithInt:[[dictionary retrieveForPath:@"SeasonNumber"] intValue]];
self.episodeNumber = [NSNumber numberWithInt:[[dictionary retrieveForPath:@"EpisodeNumber"] intValue]];
if ([dictionary retrieveForPath:@"filename"])
{
TVDbImage *bannerImage = [[TVDbImage alloc] initWithUrl:[dictionary retrieveForPath:@"filename"]];
self.banner = [bannerImage url];
self.bannerThumbnail = [bannerImage thumbnailUrl];
if ([dictionary retrieveForPath:@"EpImgFlag"])
{
int imgFlag = [[dictionary retrieveForPath:@"EpImgFlag"] intValue];
switch (imgFlag) {
case 1:
self.bannerAspectRatio = FOUR_THIRD;
break;
case 2:
self.bannerAspectRatio = SIXTEEN_NINTH;
break;
case 3:
self.bannerAspectRatio = INVALID;
break;
case 4:
self.bannerAspectRatio = TOO_SMALL;
break;
case 5:
self.bannerAspectRatio = BLACK_BARS;
break;
default:
self.bannerAspectRatio = IMPROPER;
}
}
}
self.writer = [NSString pipedStringToArray:[dictionary retrieveForPath:@"Writer"]];
self.director = [NSString pipedStringToArray:[dictionary retrieveForPath:@"Director"]];
self.gueststars = [NSString pipedStringToArray:[dictionary retrieveForPath:@"GuestStars"]];
self.imdbId = [dictionary retrieveForPath:@"IMDB_ID"];
self.premiereDate = [NSString stringToDate:[dictionary retrieveForPath:@"FirstAired"]];
self.rating = [dictionary retrieveForPath:@"Rating"];
self.showId = [NSNumber numberWithInt:[[dictionary retrieveForPath:@"seriesid"] intValue]];
}
return self;
}
#pragma mark - class methods
+ (TVDbEpisode *)findById:(NSNumber *)episodeId
{
NSDictionary *episodeDictionary = [[TVDbClient sharedInstance] requestURL:[self episodeUrl:episodeId]];
return [[TVDbEpisode alloc] initWithDictionary:[episodeDictionary retrieveForPath:@"Data.Episode"]];
}
+ (TVDbEpisode *)findByShowId:(NSNumber *)showId seasonNumber:(NSNumber *)seasonNumber episodeNumber:(NSNumber *)episodeNumber
{
NSDictionary *episodeDictionary = [[TVDbClient sharedInstance] requestURL:[self episodeUrlByShowId:showId seasonNumber:seasonNumber episodeNumber:episodeNumber]];
return [[TVDbEpisode alloc] initWithDictionary:[episodeDictionary retrieveForPath:@"Data.Episode"]];
}
#pragma mark - internal methods
+ (NSString *)episodeUrl:(NSNumber *)episodeId
{
return [[[TVDbClient sharedInstance] apiKey] stringByAppendingString:[NSString stringWithFormat:@"/episodes/%@", episodeId]];
}
+ (NSString *)episodeUrlByShowId:(NSNumber *)showId seasonNumber:(NSNumber *)seasonNumber episodeNumber:(NSNumber *)episodeNumber
{
return [[[TVDbClient sharedInstance] apiKey] stringByAppendingString:[NSString stringWithFormat:@"/series/%@/default/%@/%@", showId, seasonNumber, episodeNumber]];
}
@end