Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjibone committed Feb 7, 2012
0 parents commit 7b98b03
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
My TVDB API classes!

What is it?
This set of classes allows the user to use the website thetvdb.com and its API to perform searches for shows in its database and then collect episode information for a certain season and episode number.

Show Search:
This is done using the TVDBShowSearch class and the only input for this is just the show name you would like to search for (i.e. "Peep Show").
This will then use the TVDB API and find all the shows in its database that match the words you typed in.
You are then free to pull out the show information in a number of ways:
• All at once. This returns an NSMutableArray containing all of the shows found. You can then look through these and find the element which is relevant to you. You would normally get the user to select the correct show at this point (if you like).
• Get a specific show. This returns the TVDBShow object that corresponds to the element number that you input.
• Get the first show from the list. This is usually the "top hit" when searching using the TVDB website, so you can be pretty sure that this is the show that you are looking for ;)

Episode Search:
This is done using the TVDBEpisodeSearch class and the inputs for this are:
• The SeriesID for the show you have previously found/
• The season number, and
• The episode number.
This TVDBEpisodeSearch object now contains all the info from the TVDB website for that specific episode. So you can now pull out all the data for it just by asking for it.

Note:
A lot of the methods and classes in this TVDB API are likely to change dramatically as this set of files is still in the pre-version one stage. So, if anything changes please don't cry too much as I warned you guys!

To Do:
• Adding all the fields in the TVDBEpisodeSearch class (as there are some little bits missing!) although some of the ones already missing are probably not needed.
• dealloc methods for all classes and all round better memory management.
• Changing TVDBEpisodeSearch.m/.h to TVDBEpisode as this seems more fitting as they will be holding the episode information.
⁃ Also on the other hand, maybe there should be a new class called TVDBEpisode which just holds episode information so that files that already contain information don't lose it when the class is created or whatever.
• Hide away all the parser methods as they are for XML parsing and shouldn't be called by the user of these classes directly.


34 changes: 34 additions & 0 deletions TVDBEpisodeSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// TVDBEpisodeSearch.h
// VideoMatic
//
// Created by James Reuss on 18/01/2012.
// Copyright (c) 2012 James Reuss. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TVDBEpisodeSearch : NSObject {
NSMutableString *dataBuffer;
}
@property (assign) NSString *episodeID, *seasonID;
@property (assign) NSInteger episodeNumber, seasonNumber;
@property (assign) NSString *episodeName;
@property (assign) NSDate *firstAired;
@property (assign) NSString *guestStars, *director, *writer;
@property (assign) NSString *overview;
@property (assign) NSString *coverPhotoURL;
@property (assign) NSNumber *rating;
@property (assign) NSString *language;

#pragma mark Initialisation
- (id)init;

#pragma mark Search Methods
- (void)findEpisodeInfoForSeriesID:(NSString*)show Season:(NSInteger)seasonNo Episode:(NSInteger)episodeNo;

#pragma mark Internal Methods
- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;

@end
110 changes: 110 additions & 0 deletions TVDBEpisodeSearch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// TVDBEpisodeSearch.m
// VideoMatic
//
// Created by James Reuss on 18/01/2012.
// Copyright (c) 2012 James Reuss. All rights reserved.
//

#import "TVDBEpisodeSearch.h"

@implementation TVDBEpisodeSearch
@synthesize episodeID, seasonID, episodeNumber, seasonNumber, episodeName;
@synthesize firstAired, guestStars, director, writer;
@synthesize overview, coverPhotoURL, rating, language;

//-------------------------------------------------------------------------------------
// Initialisation Methods
//-------------------------------------------------------------------------------------
- (id)init {
self = [super init];
if (self) {
//
}
return self;
}

//-------------------------------------------------------------------------------------
// Search Methods
//-------------------------------------------------------------------------------------
- (void)findEpisodeInfoForSeriesID:(NSString*)seriesID Season:(NSInteger)seasonNo Episode:(NSInteger)episodeNo {
// Set up the url to get the episode info.
NSURL *infoURL = [[NSURL alloc] initWithString:
[NSString stringWithFormat:@"http://www.thetvdb.com/api/7F6CCD965D3EF4EC/series/%@/default/%ld/%ld/en.xml", seriesID, seasonNo, episodeNo]];

// Create a XML parser to search through the returned results for us.
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:infoURL];
[parser setDelegate:self];
[parser parse];

// Clean up.
[parser release];
[infoURL release];
}

//-------------------------------------------------------------------------------------
// Internal NSXML Parser Methods
//-------------------------------------------------------------------------------------
- (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:@"id"]) {
episodeID = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"seasonid"]) {
seasonID = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"EpisodeNumber"]) {
episodeNumber = [dataBuffer integerValue];
} else if ([elementName isEqualToString:@"SeasonNumber"]) {
seasonNumber = [dataBuffer integerValue];
} else if ([elementName isEqualToString:@"EpisodeName"]) {
episodeName = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"FirstAired"]) {
firstAired = [NSDate dateWithString:dataBuffer];
} else if ([elementName isEqualToString:@"GuestStars"]) {
guestStars = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"Director"]) {
director = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"Writer"]) {
writer = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"Overview"]) {
overview = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"filename"]) {
coverPhotoURL = [NSString stringWithString:dataBuffer];
} else if ([elementName isEqualToString:@"Rating"]) {
rating = [NSNumber numberWithFloat:[dataBuffer floatValue]];
} else if ([elementName isEqualToString:@"Language"]) {
language = [NSString stringWithString:dataBuffer];
}

if ([elementName isEqualToString:@"Episode"]) {
// 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 (!episodeID) episodeID = @"N/A";
if (!seasonID) seasonID = @"N/A";
if (!episodeNumber) episodeNumber = 0;
if (!seasonNumber) seasonNumber = 0;
if (!firstAired) firstAired = [NSDate dateWithString:@"1930-01-01 00:00:01 +0000"];
if (!guestStars) guestStars = @"N/A";
if (!director) director = @"N/A";
if (!writer) writer = @"N/A";
if (!overview) overview = @"N/A";
if (!coverPhotoURL) coverPhotoURL = @"N/A";
if (!rating) rating = [NSNumber numberWithInt:0];
if (!language) language = @"N/A";
} else {
[dataBuffer setString:@""];
}
}

@end
23 changes: 23 additions & 0 deletions TVDBShow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// TVDBShow.h
// Media Sorter
//
// Created by James Reuss on 21/12/2011.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TVDBShow : NSObject
@property (retain) NSString *seriesID;
@property (retain) NSString *language;
@property (retain) NSString *seriesName;
@property (retain) NSString *overview;

// Instance Methods
- (void)setShowWithSeriesID:(NSString*)newSeriesID Language:(NSString*)newLanguage SeriesName:(NSString*)newSeries Overview:(NSString*)newOverview;

// Class Methods
+ (TVDBShow*)showWithSeriesID:(NSString*)newSeriesID Language:(NSString*)newLanguage SeriesName:(NSString*)newSeries Overview:(NSString*)newOverview;

@end
33 changes: 33 additions & 0 deletions TVDBShow.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// TVDBShow.m
// Media Sorter
//
// Created by James Reuss on 21/12/2011.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "TVDBShow.h"

@implementation TVDBShow
@synthesize seriesID, language, seriesName, overview;

//-------------------------------------------------------------------------------------
// Instance Methods
//-------------------------------------------------------------------------------------
- (void)setShowWithSeriesID:(NSString*)newSeriesID Language:(NSString*)newLanguage SeriesName:(NSString*)newSeries Overview:(NSString*)newOverview {
[self setSeriesID:newSeriesID];
[self setLanguage:newLanguage];
[self setSeriesName:newSeries];
[self setLanguage:newLanguage];
}

//-------------------------------------------------------------------------------------
// Class Methods
//-------------------------------------------------------------------------------------
+ (TVDBShow*)showWithSeriesID:(NSString*)newSeriesID Language:(NSString*)newLanguage SeriesName:(NSString*)newSeries Overview:(NSString*)newOverview {
TVDBShow *returnableShow = [[TVDBShow alloc] init];
[returnableShow setShowWithSeriesID:newSeriesID Language:newLanguage SeriesName:newSeries Overview:newOverview];
return [returnableShow autorelease];
}

@end
32 changes: 32 additions & 0 deletions TVDBShowSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// TVDB API.h
// VideoMatic
//
// Created by James Reuss on 18/01/2012.
// Copyright (c) 2012 James Reuss. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TVDBShow.h"

@interface TVDBShowSearch : NSObject {
NSMutableArray *foundShows;
TVDBShow *currentShow;
NSMutableString *dataBuffer;
}

#pragma mark Initialisation
- (id)init;

#pragma mark Search Methods
- (void)searchForShowWithName:(NSString*)searchTerm;
- (NSMutableArray*)getFoundShows;
- (TVDBShow*)getShowElement:(NSUInteger)element;
- (TVDBShow*)firstShow;

#pragma mark Internal Methods
- (void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;

@end
104 changes: 104 additions & 0 deletions TVDBShowSearch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 7b98b03

Please sign in to comment.