Skip to content

Commit

Permalink
Caches the show posters so that they aren't downloaded each time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Price committed Oct 4, 2010
1 parent 6fd0b4e commit 1cc0027
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 82 deletions.
33 changes: 4 additions & 29 deletions Classes/PresetTorrentsController.m
Expand Up @@ -211,7 +211,6 @@ - (void) tableViewSelectionDidChange:(NSNotification *)notification
// Make sure we were able to correctly set a selection before continuing,
// or else searching and the scrollbar will fail.
if ( ([PTTableView selectedRow] > -1) || ([PTTableView selectedRow] == 0) && ([PTTableView selectedRow]) ) {
id showAPI = [[[TheTVDB class] alloc] init];

// Grab the list of episodes
NSString *selectedShowURL = [NSString stringWithFormat:@"http://showrss.karmorra.info/feeds/%@.rss",
Expand All @@ -220,41 +219,17 @@ - (void) tableViewSelectionDidChange:(NSNotification *)notification
maxItems:10]];

// Grab the show description
// TODO: Fix displaying HTML codes (— etc)
NSString *description = [showAPI getValueForKey:@"Overview" andShow:
NSString *description = [TheTVDB getValueForKey:@"Overview" andShow:
[[[PTArrayController selectedObjects] valueForKey:@"name"] objectAtIndex:0]];
[showDescription setString: [TSRegexFun replaceHTMLEntitiesInString:description]];

// Grab the URL of the show poster
// TODO: Cache the images
// TODO: Display placeholder if no image is found.
NSString *posterURL = [showAPI getValueForKey:@"poster" andShow:
[[[PTArrayController selectedObjects] valueForKey:@"name"] objectAtIndex:0]];

// Resize the show poster so that it scales smoothly and still fits the box.
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfURL:
[NSURL URLWithString: [NSString stringWithFormat:@"http://www.thetvdb.com/banners/%@",posterURL]]];
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(129, 187)];

NSSize originalSize = [sourceImage size];

[resizedImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[sourceImage drawInRect: NSMakeRect(0, 0, 129, 187) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];

NSData *resizedData = [resizedImage TIFFRepresentation];


// Display the show poster now that it's been resized.
[showPoster setImage: [[[NSImage alloc] initWithData: resizedData] autorelease]];
[showPoster setImage: [TheTVDB getPosterForShow:
[[[PTArrayController selectedObjects] valueForKey:@"name"] objectAtIndex:0]] ];

// Update the filter predicate to only display the correct quality.
// [self showQualityDidChange:nil];

// Release the API and show images.
[showAPI release];
[sourceImage release];
[resizedImage release];
}
}

Expand Down
4 changes: 3 additions & 1 deletion Classes/TheTVDB.h
Expand Up @@ -22,6 +22,8 @@
@property (retain) NSURL *mirrorURL;
@property (retain) NSString *serverTime;

- (NSString *) getValueForKey:(NSString *)key andShow:(NSString *)show;
+ (NSString *) applicationCacheDirectory;
+ (NSString *) getValueForKey:(NSString *)key andShow:(NSString *)show;
+ (NSImage *) getPosterForShow:(NSString *)showName;

@end
69 changes: 64 additions & 5 deletions Classes/TheTVDB.m
Expand Up @@ -29,20 +29,30 @@ @implementation TheTVDB
if((self = [super init])) {
// Before we can do anything we need to download a list of mirrors.
// TODO: Grab the list of mirrors. Currently only one server is listed, though.
mirrorURL = [NSURL URLWithString:@"http://www.thetvdb.com"];
// mirrorURL = [NSURL URLWithString:@"http://www.thetvdb.com"];

// Get the current server time.
// TODO: This isn't actually saved anywhere but will be used for knowing
// whether we need to update the Cache or not.
serverTime = [[[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.thetvdb.com/api/Updates.php?type=none"]
encoding: NSUTF8StringEncoding
error: NULL] autorelease];
// serverTime = [[[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.thetvdb.com/api/Updates.php?type=none"]
// encoding: NSUTF8StringEncoding
// error: NULL] autorelease];
}

return self;
}

- (NSString *) getValueForKey:(NSString *)key andShow:(NSString *)show
+ (NSString *) applicationCacheDirectory
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();

basePath = [basePath stringByAppendingPathComponent:@"TVShows 2"];
return [basePath stringByAppendingPathComponent:@"Cache"];
}

+ (NSString *) getValueForKey:(NSString *)key andShow:(NSString *)show
{
// TODO: Save the information returned for each series into the Cache
NSURL *seriesURL = [NSURL URLWithString:[[NSString stringWithString: @"http://www.thetvdb.com/api/GetSeries.php?seriesname="]
Expand Down Expand Up @@ -71,6 +81,55 @@ - (NSString *) getValueForKey:(NSString *)key andShow:(NSString *)show
return value;
}

+ (NSImage *) getPosterForShow:(NSString *)showName
{
// If the TVShows cache directory doesn't exist then create it.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *applicationCacheDirectory = [self applicationCacheDirectory];
NSError *error = nil;

if ( ![fileManager fileExistsAtPath:applicationCacheDirectory isDirectory:NULL] ) {
if (![fileManager createDirectoryAtPath:applicationCacheDirectory withIntermediateDirectories:NO attributes:nil error:&error]) {
TVLog(@"Error creating application cache directory: %@",error);
return nil;
}
}

// If the image already exists then return the data, otherwise we need to download it.
NSString *imagePath = [[[self applicationCacheDirectory] stringByAppendingPathComponent:showName] stringByAppendingFormat:@".tiff"];

if ( [fileManager fileExistsAtPath:imagePath] ) {
DLog(@"Using cached image");
return [[[NSImage alloc] initWithContentsOfFile:imagePath] autorelease];
} else {
// Grab the URL of the show poster
NSString *posterURL = [self getValueForKey:@"poster" andShow: showName];

// Resize the show poster so that it scales smoothly and still fits the box.
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfURL:
[NSURL URLWithString: [NSString stringWithFormat:@"http://www.thetvdb.com/banners/%@",posterURL]]];
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(129, 187)];

NSSize originalSize = [sourceImage size];

[resizedImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[sourceImage drawInRect: NSMakeRect(0, 0, 129, 187)
fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height)
operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];

// Save the image so that we don't have to download it again.
NSData *resizedData = [resizedImage TIFFRepresentation];
[resizedData writeToFile:imagePath atomically:YES];

[sourceImage release];
[resizedImage release];

return resizedImage;
}
}


- (void) dealloc
{
Expand Down
94 changes: 47 additions & 47 deletions Resources/en.lproj/TVShowsPref.xib
Expand Up @@ -1571,6 +1571,50 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA</bytes>
<int key="NSvFlags">256</int>
<object class="NSMutableArray" key="NSSubviews">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSImageView" id="1056219518">
<reference key="NSNextResponder" ref="241592026"/>
<int key="NSvFlags">268</int>
<object class="NSMutableSet" key="NSDragTypes">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="set.sortedObjects">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>Apple PDF pasteboard type</string>
<string>Apple PICT pasteboard type</string>
<string>Apple PNG pasteboard type</string>
<string>NSFilenamesPboardType</string>
<string>NeXT Encapsulated PostScript v1.2 pasteboard type</string>
<string>NeXT TIFF v4.0 pasteboard type</string>
</object>
</object>
<string key="NSFrame">{{190, 207}, {135, 194}}</string>
<reference key="NSSuperview" ref="241592026"/>
<bool key="NSViewIsLayerTreeHost">YES</bool>
<object class="NSShadow" key="NSViewShadow">
<double key="NSShadowHoriz">1</double>
<double key="NSShadowVert">-1</double>
<double key="NSShadowBlurRadius">1</double>
<object class="NSColor" key="NSShadowColor" id="2539">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MC4zNjg2MTMxMjM5AA</bytes>
</object>
</object>
<int key="NSViewLayerContentsRedrawPolicy">2</int>
<bool key="NSEnabled">YES</bool>
<object class="NSImageCell" key="NSCell" id="79062818">
<int key="NSCellFlags">67239424</int>
<int key="NSCellFlags2">33685504</int>
<object class="NSCustomResource" key="NSContents" id="412595049">
<string key="NSClassName">NSImage</string>
<string key="NSResourceName">posterArtPlaceholder</string>
</object>
<reference key="NSSupport" ref="26"/>
<int key="NSAlign">2</int>
<int key="NSScale">0</int>
<int key="NSStyle">0</int>
<bool key="NSAnimates">NO</bool>
</object>
<bool key="NSEditable">YES</bool>
</object>
<object class="NSScrollView" id="456449400">
<reference key="NSNextResponder" ref="241592026"/>
<int key="NSvFlags">4364</int>
Expand Down Expand Up @@ -2223,50 +2267,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA</bytes>
<reference key="NSTextColor" ref="73395071"/>
</object>
</object>
<object class="NSImageView" id="1056219518">
<reference key="NSNextResponder" ref="241592026"/>
<int key="NSvFlags">268</int>
<object class="NSMutableSet" key="NSDragTypes">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="set.sortedObjects">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>Apple PDF pasteboard type</string>
<string>Apple PICT pasteboard type</string>
<string>Apple PNG pasteboard type</string>
<string>NSFilenamesPboardType</string>
<string>NeXT Encapsulated PostScript v1.2 pasteboard type</string>
<string>NeXT TIFF v4.0 pasteboard type</string>
</object>
</object>
<string key="NSFrame">{{190, 207}, {135, 194}}</string>
<reference key="NSSuperview" ref="241592026"/>
<bool key="NSViewIsLayerTreeHost">YES</bool>
<object class="NSShadow" key="NSViewShadow">
<double key="NSShadowHoriz">1</double>
<double key="NSShadowVert">-1</double>
<double key="NSShadowBlurRadius">1</double>
<object class="NSColor" key="NSShadowColor" id="2539">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MC4zNjg2MTMxMjM5AA</bytes>
</object>
</object>
<int key="NSViewLayerContentsRedrawPolicy">2</int>
<bool key="NSEnabled">YES</bool>
<object class="NSImageCell" key="NSCell" id="79062818">
<int key="NSCellFlags">67239424</int>
<int key="NSCellFlags2">33685504</int>
<object class="NSCustomResource" key="NSContents" id="412595049">
<string key="NSClassName">NSImage</string>
<string key="NSResourceName">posterArtPlaceholder</string>
</object>
<reference key="NSSupport" ref="26"/>
<int key="NSAlign">2</int>
<int key="NSScale">0</int>
<int key="NSStyle">0</int>
<bool key="NSAnimates">NO</bool>
</object>
<bool key="NSEditable">YES</bool>
</object>
<object class="NSScrollView" id="868051296">
<reference key="NSNextResponder" ref="241592026"/>
<int key="NSvFlags">4352</int>
Expand Down Expand Up @@ -9291,9 +9291,9 @@ bW1vZG8gY29uc2VxdWF0Lg</bytes>
</object>
</object>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{589, 168}, {669, 450}}</string>
<string>{{312, 166}, {669, 450}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{589, 168}, {669, 450}}</string>
<string>{{312, 166}, {669, 450}}</string>
<boolean value="NO"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>ESScrollView</string>
Expand Down Expand Up @@ -9370,7 +9370,7 @@ bW1vZG8gY29uc2VxdWF0Lg</bytes>
</object>
</object>
<nil key="sourceID"/>
<int key="maxID">1999</int>
<int key="maxID">2007</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
Expand Down

0 comments on commit 1cc0027

Please sign in to comment.