-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTVDBShowSearch.m
More file actions
104 lines (90 loc) · 4.04 KB
/
TVDBShowSearch.m
File metadata and controls
104 lines (90 loc) · 4.04 KB
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
//
// TVDB API.m
// VideoMatic
//
// Created by James Reuss on 18/01/2012.
// Copyright (c) 2012 James Reuss. All rights reserved.
//
#import "TVDBShowSearch.h"
#define GETSERIESURL @"http://www.thetvdb.com/api/GetSeries.php?seriesname="
@implementation TVDBShowSearch
//-------------------------------------------------------------------------------------
// Initialisation Methods
//-------------------------------------------------------------------------------------
- (id)init {
self = [super init];
if (self) {
foundShows = [[NSMutableArray alloc] init];
}
return self;
}
//-------------------------------------------------------------------------------------
// Search Methods
//-------------------------------------------------------------------------------------
- (void)searchForShowWithName:(NSString*)searchTerm {
// Set up the url to search for the show 'showName'.
searchTerm = [searchTerm stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *searchURL = [[NSURL alloc] initWithString:
[NSString stringWithFormat:@"%@%@", GETSERIESURL, searchTerm]];
[foundShows removeAllObjects];
// Create a XML parser to search through the returned results for us.
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:searchURL];
[parser setDelegate:self];
[parser parse];
// Clean up.
[parser release];
[searchURL release];
}
- (NSMutableArray*)getFoundShows {
return foundShows;
}
- (TVDBShow*)getShowElement:(NSUInteger)element {
return [foundShows objectAtIndex:element];
}
- (TVDBShow*)firstShow {
return [foundShows objectAtIndex:0];
}
//-------------------------------------------------------------------------------------
// Internal NSXML Parser Methods
//-------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// If we are doing a Series search then we want to create a new TVDBShow object to work with for each show we encounter in the search.
if ([elementName isEqualToString:@"seriesid"]) {
currentShow = [[TVDBShow alloc] init];
}
}
- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string {
// This will be the same for all search types as it just collects the data we want.
if (!dataBuffer) {
dataBuffer = [[NSMutableString alloc] initWithString:@""];
}
[dataBuffer appendString:
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// So were doing a Series search. We must pick out all the information we want and put it into the relevant field of foundShow.
if ([elementName isEqualToString:@"seriesid"]) {
[currentShow setSeriesID:[NSString stringWithString:dataBuffer]];
} else if ([elementName isEqualToString:@"language"]) {
[currentShow setLanguage:[NSString stringWithString:dataBuffer]];
} else if ([elementName isEqualToString:@"SeriesName"]) {
[currentShow setSeriesName:[NSString stringWithString:dataBuffer]];
} else if ([elementName isEqualToString:@"Overview"]) {
[currentShow setOverview:[NSString stringWithString:dataBuffer]];
}
if ([elementName isEqualToString:@"id"]) {
// This is the final element in this shows XML tree and because were not interested in this we can use it to close off the assignment of this shows details and add it to the foundShowArray.
[dataBuffer release];
dataBuffer = nil;
// Check that all the fields are filled. If not then fill with blank.
if (![currentShow seriesID]) [currentShow setSeriesID:@"N/A"];
if (![currentShow language]) [currentShow setLanguage:@"N/A"];
if (![currentShow seriesName]) [currentShow setOverview:@"N/A"];
if (![currentShow overview]) [currentShow setOverview:@"N/A"];
[foundShows addObject:currentShow];
[currentShow release];
} else {
[dataBuffer setString:@""];
}
}
@end