Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created February 27, 2012 14:03
Show Gist options
  • Save Dimillian/1923991 to your computer and use it in GitHub Desktop.
Save Dimillian/1923991 to your computer and use it in GitHub Desktop.
MSmovie
-(id)initWithMovieDic:(NSDictionary *)dic fromSearchResult:(BOOL)search
{
self = [super init];
if (self) {
_udid = [dic objectForKey:@"id"];
_title = [dic objectForKey:@"name"];
if ([self isNotNull:[dic objectForKey:@"alternative_name"]]) {
_alternateTitle = [dic objectForKey:@"alternative_name"];
}
else{
_alternateTitle = NSLocalizedString(@"No alternative title", @"No alternatie title movie object");
}
if ([self isNotNull:[dic objectForKey:@"overview"]]) {
_subtitle = [dic objectForKey:@"overview"];
}
else{
_subtitle = @"No sumary";
}
NSArray *posters = [dic objectForKey:@"posters"];
if (posters.count != 0 ) {
NSDictionary *tinyImage = [posters objectAtIndex:1];
NSDictionary *tinyImageDetail = [tinyImage objectForKey:@"image"];
_tinyImageLink = [tinyImageDetail objectForKey:@"url"];
NSDictionary *normalImage = [posters objectAtIndex:4];
NSDictionary *normalImageDetal = [normalImage objectForKey:@"image"];
_normalImageLink = [normalImageDetal objectForKey:@"url"];
}
else{
_tinyImageLink = @"";
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setDateFormat:@"yyyy-MM-dd"];
if ([self isNotNull:[dic objectForKey:@"released"]]) {
_releaseDate = [df dateFromString:[dic objectForKey:@"released"]];
_releaseYear = [self extractYearFromDate:_releaseDate];
_readbleDate = [self formatToReableDate:_releaseDate];
}
else{
_releaseDate = [NSDate date];
_releaseYear = NSLocalizedString(@"N/A", @"Unknown date");
_readbleDate = NSLocalizedString(@"N/A", @"Unknown date");
}
//If no search then parse the rest
if (!search) {
if ([dic objectForKey:@"cast"]) {
NSArray *casts = [dic objectForKey:@"cast"];
_castArray = [[NSMutableArray alloc]initWithCapacity:casts.count];
for (NSDictionary *castDic in casts) {
MSCast *cast = [[MSCast alloc]initWithDic:castDic];
[self.castArray addObject:cast];
}
}
if ([dic objectForKey:@"genres"]) {
NSArray *kinds = [dic objectForKey:@"genres"];
_kindArray = [[NSMutableArray alloc]initWithCapacity:kinds.count];
for (NSDictionary *kindDic in kinds) {
[_kindArray addObject:[kindDic objectForKey:@"name"]];
}
}
_summaryComplete = [self constructFullSumary];
_kindListString = [self constructKindList];
_length = [NSString stringWithFormat:@"%@ min", [dic objectForKey:@"runtime"]];
if ([_length isEqualToString:@"0"]) {
_length = NSLocalizedString(@"N/A", "N/A");
}
}
}
return self;
}
-(BOOL)isNotNull:(NSString *)string
{
return ([string class] != [NSNull class]);
}
-(NSString *)formatToReableDate:(NSDate *)date
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [NSString stringWithFormat:@"%@",
[df stringFromDate:date]];
return dateString;
}
-(NSString *)extractYearFromDate:(NSDate *)date
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setDateFormat:@"yyyy"];
NSString *dateString = [NSString stringWithFormat:@"%@",
[df stringFromDate:date]];
return dateString;
}
-(NSString *)constructFullSumary
{
MSCast *producer;
NSString *localizedProducer = NSLocalizedString(@"Producer", @"Producer/realisateur");
if (self.castArray.count > 0) {
for (MSCast *cast in self.castArray) {
if ([cast.job isEqualToString:@"Producer"]) {
producer = cast;
break;
}
}
_realisator = [NSString stringWithFormat:@"%@: %@", localizedProducer, producer.name];
}
else{
_realisator = [NSString stringWithFormat:@"%@: Unknown", localizedProducer];
}
NSString *castListTPS = [[NSString alloc]init];
NSInteger castIndex = 0;
if (self.castArray.count > 0) {
for (MSCast *cast in self.castArray) {
if (castIndex > 4 || castIndex > self.castArray.count) {
break;
}
if ([cast.job isEqualToString:@"Actor"]) {
castListTPS = [castListTPS stringByAppendingFormat:[NSString stringWithFormat:@"%@, ", cast.name]];
castIndex++;
}
}
}
else {
castListTPS = NSLocalizedString(@"N/A", "N/A");
}
NSString *localiszedActor = NSLocalizedString(@"Actors", @"Actors/acteur");
_castListString = [NSString stringWithFormat:@"%@: %@", localiszedActor, castListTPS];
NSString *localiazedSummary = NSLocalizedString(@"Summary", @"Sumary/Synopsis");
return [NSString stringWithFormat:@"%@\n%@\n%@: %@", self.realisator, self.castListString, localiazedSummary, self.subtitle];
}
-(NSString *)constructKindList
{
NSString *tpsKind = [[NSString alloc]init];
NSInteger z = 0;
if (self.kindArray.count > 0) {
for (NSString *kind in self.kindArray) {
if (z > 2 || z > self.castArray.count) {
break;
}
tpsKind = [tpsKind stringByAppendingFormat:@"%@, ", kind];
}
}
else {
tpsKind = NSLocalizedString(@"N/A", "N/A");
}
return tpsKind;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment