Skip to content

Commit

Permalink
- Made amazon search work
Browse files Browse the repository at this point in the history
- Changed search profiles so that title can be disabled like other search terms
  • Loading branch information
griff committed Nov 20, 2009
1 parent 518571b commit 8fe20f9
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -4,4 +4,4 @@
build
junk
sparkle_private.pem
Plugins/Amazon/Access
Plugins/Amazon/Access.h
6 changes: 4 additions & 2 deletions App/src/AppController.m
Expand Up @@ -207,13 +207,15 @@ - (void)updateSearchMenu
NSMenu* menu = [[NSMenu alloc] initWithTitle:
NSLocalizedString(@"Search terms", @"Search menu title")];
[menu addItemWithTitle:[menu title] action:nil keyEquivalent:@""];
/*
if([profile mainTag])
{
MZTag* tag = [MZTag tagForIdentifier:[profile mainTag]];
NSMenuItem* mainItem = [menu addItemWithTitle:[tag localizedName] action:NULL keyEquivalent:@""];
[mainItem setState:NSOnState];
[mainItem setIndentationLevel:1];
}
*/
NSInteger i = 0;
for(NSString* tagId in [profile tags])
{
Expand Down Expand Up @@ -334,8 +336,8 @@ - (IBAction)startSearch:(id)sender;
NSString* term = [[searchField stringValue]
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
NSMutableDictionary* dict = [activeProfile searchTerms];
[dict setObject:term forKey:[activeProfile mainTag]];
NSMutableDictionary* dict = [activeProfile searchTerms:term];
//[dict setObject:term forKey:[activeProfile mainTag]];
[searchIndicator startAnimation:searchField];
[searchController setSortDescriptors:nil];
searches++;
Expand Down
2 changes: 1 addition & 1 deletion App/src/SearchProfile.h
Expand Up @@ -29,6 +29,6 @@

- (void)setCheckObject:(id)obj withPrefix:(NSString *)prefix;
- (void)switchItem:(NSMenuItem *)sender;
- (NSMutableDictionary *)searchTerms;
- (NSMutableDictionary *)searchTerms:(NSString *)mainTerm;

@end
14 changes: 9 additions & 5 deletions App/src/SearchProfile.m
Expand Up @@ -28,22 +28,22 @@ + (SearchProfile*)unknownTypeProfile
{
return [self profileWithIdentifier:@"unknown" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZChaptersTagIdent, nil]];
MZTitleTagIdent, MZChaptersTagIdent, nil]];
}

+ (SearchProfile*)tvShowProfile
{
return [self profileWithIdentifier:@"tvShow" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZVideoTypeTagIdent, MZTVShowTagIdent,
MZTitleTagIdent, MZVideoTypeTagIdent, MZTVShowTagIdent,
MZTVSeasonTagIdent, MZTVEpisodeTagIdent, MZChaptersTagIdent, nil]];
}

+ (SearchProfile*)movieProfile
{
return [self profileWithIdentifier:@"movie" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZVideoTypeTagIdent, MZChaptersTagIdent, nil]];
MZTitleTagIdent, MZVideoTypeTagIdent, MZChaptersTagIdent, nil]];
}

+ (id)profileWithIdentifier:(NSString *)ident mainTag:(NSString *)main tag:(NSArray *)tags;
Expand Down Expand Up @@ -130,14 +130,18 @@ - (void)alterState:(NSMenuItem *)sender
[sender setState:(state.state ? NSOnState : NSOffState)];
}

- (NSMutableDictionary *)searchTerms
- (NSMutableDictionary *)searchTerms:(NSString *)mainTerm
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for(ProfileState* state in tags)
{
if(state.state)
{
id value = [self valueForUndefinedKey:state.tag];
id value;
if([state.tag isEqual:mainTag])
value = mainTerm;
else
value = [self valueForUndefinedKey:state.tag];
if(value != nil && value != [NSNull null] && value != NSNoSelectionMarker &&
value != NSMultipleValuesMarker && value != NSNotApplicableMarker)
{
Expand Down
2 changes: 2 additions & 0 deletions Framework/src/MZRESTSearch.h
Expand Up @@ -21,6 +21,8 @@
BOOL isFinished;
BOOL isExecuting;
}
+ (Class)restWrapper;

@property(assign) BOOL isFinished;
@property(assign) BOOL isExecuting;

Expand Down
8 changes: 7 additions & 1 deletion Framework/src/MZRESTSearch.m
Expand Up @@ -10,6 +10,12 @@
#import "MZSearchProvider.h"

@implementation MZRESTSearch

+ (Class)restWrapper
{
return [MZRESTWrapper class];
}

@synthesize isFinished;
@synthesize isExecuting;

Expand All @@ -23,7 +29,7 @@ - (id)initWithProvider:(id)theProvider delegate:(id<MZSearchProviderDelegate>)th
parameters = [params retain];
provider = [theProvider retain];
delegate = [theDelegate retain];
wrapper = [[MZRESTWrapper alloc] init];
wrapper = [[[[self class] restWrapper] alloc] init];
wrapper.delegate = self;
}
return self;
Expand Down
12 changes: 10 additions & 2 deletions MetaZ.xcodeproj/project.pbxproj
Expand Up @@ -73,6 +73,7 @@
1B11A02510457B76004C93D3 /* rb_main.rb in Resources */ = {isa = PBXBuildFile; fileRef = 1B11A02410457B76004C93D3 /* rb_main.rb */; };
1B11A02710457FB2004C93D3 /* AtomicParsleyWrapper.rb in Resources */ = {isa = PBXBuildFile; fileRef = 1B11A02610457FB2004C93D3 /* AtomicParsleyWrapper.rb */; };
1B11A28B104DF1EC004C93D3 /* FilesTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B11A28A104DF1EC004C93D3 /* FilesTableView.m */; };
1B15A60E10B608D100EC0FBF /* AmazonSearch.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B15A60D10B608D100EC0FBF /* AmazonSearch.m */; };
1B20209B10B36EC200EE548A /* VideoType.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1B20209910B36EC200EE548A /* VideoType.strings */; };
1B5AC76F10532E5200F9CE36 /* MetaEditPanel.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B5AC76E10532E5200F9CE36 /* MetaEditPanel.m */; };
1B6E43C810845F8200DFE165 /* TCSearch.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B6E43C710845F8200DFE165 /* TCSearch.m */; };
Expand Down Expand Up @@ -384,6 +385,9 @@
1B11A28A104DF1EC004C93D3 /* FilesTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FilesTableView.m; sourceTree = "<group>"; };
1B11A47C104E627F004C93D3 /* MZConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MZConstants.h; sourceTree = "<group>"; };
1B11A47D104E627F004C93D3 /* MZConstants.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MZConstants.m; sourceTree = "<group>"; };
1B15A60C10B608D100EC0FBF /* AmazonSearch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AmazonSearch.h; sourceTree = "<group>"; };
1B15A60D10B608D100EC0FBF /* AmazonSearch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AmazonSearch.m; sourceTree = "<group>"; };
1B15A62610B6316000EC0FBF /* Access.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Access.h; path = Plugins/Amazon/Access.h; sourceTree = SOURCE_ROOT; };
1B20209A10B36EC200EE548A /* English */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = English; path = Framework/English.lproj/VideoType.strings; sourceTree = "<group>"; };
1B5AC76D10532E5200F9CE36 /* MetaEditPanel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MetaEditPanel.h; sourceTree = "<group>"; };
1B5AC76E10532E5200F9CE36 /* MetaEditPanel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MetaEditPanel.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -674,6 +678,7 @@
1B032F5510B0BDC000B28E3A /* Classes */ = {
isa = PBXGroup;
children = (
1B15A62610B6316000EC0FBF /* Access.h */,
1B03307C10B0FA4800B28E3A /* AmazonPlugin.h */,
1B03307D10B0FA4800B28E3A /* AmazonPlugin.m */,
1B032F5610B0BDEE00B28E3A /* SignedAwsSearchRequest.h */,
Expand All @@ -694,6 +699,8 @@
1B82684F10B3EE0300DF101B /* AmazonRequest.m */,
1B8268A310B3F82400DF101B /* AmazonSearchProvider.h */,
1B8268A410B3F82400DF101B /* AmazonSearchProvider.m */,
1B15A60C10B608D100EC0FBF /* AmazonSearch.h */,
1B15A60D10B608D100EC0FBF /* AmazonSearch.m */,
);
name = Classes;
path = Plugins/Amazon/src;
Expand Down Expand Up @@ -1303,15 +1310,15 @@
);
inputPaths = (
"$(SRCROOT)/Release/next_release",
"$(SRCROOT)/Plugins/Amazon/Access",
"$(SRCROOT)/Plugins/Amazon/Access.h",
);
outputPaths = (
"$(TARGET_BUILD_DIR)/include/PlistMacros.h",
"$(TARGET_BUILD_DIR)/include/DummyFileToForceScriptToRunEveryTimeWithoutBeingCreated",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "set -o errexit\n\nPATH=$PATH:/usr/local/bin:/usr/bin:/sw/bin:/opt/local/bin\nbuildid=`/opt/local/bin/git log -n1 --pretty=oneline --format=%h`\nrelease=`cat $SCRIPT_INPUT_FILE_0`\n\necho \"#define BUILDID $buildid\" > $SCRIPT_OUTPUT_FILE_0\necho \"#define BUILDDATE `date +%y.%m.%d.%H`\" >> $SCRIPT_OUTPUT_FILE_0\necho \"#define WHOAMI `whoami`\" >> $SCRIPT_OUTPUT_FILE_0\necho \"#define RELEASE $release\" >> $SCRIPT_OUTPUT_FILE_0 \ncat $SCRIPT_OUTPUT_FILE_1 >> $SCRIPT_OUTPUT_FILE_0 \n\n# Delete the intermediate Info.plist so that Xcode re-preprocesses the Info.plist with our updated macros.\n# Use -f because after a clean build, this file doesn't exist yet, so a plain rm would fail and stop the build.\nrm -f \"${CONFIGURATION_TEMP_DIR}/MetaZ.build/Preprocessed-Info.plist\"\n";
shellScript = "set -o errexit\n\nPATH=$PATH:/usr/local/bin:/usr/bin:/sw/bin:/opt/local/bin\nbuildid=`/opt/local/bin/git log -n1 --pretty=oneline --format=%h`\nrelease=`cat $SCRIPT_INPUT_FILE_0`\n\necho \"#define BUILDID $buildid\" > $SCRIPT_OUTPUT_FILE_0\necho \"#define BUILDDATE `date +%y.%m.%d.%H`\" >> $SCRIPT_OUTPUT_FILE_0\necho \"#define WHOAMI `whoami`\" >> $SCRIPT_OUTPUT_FILE_0\necho \"#define RELEASE $release\" >> $SCRIPT_OUTPUT_FILE_0 \ncat $SCRIPT_INPUT_FILE_1 >> $SCRIPT_OUTPUT_FILE_0 \n\n# Delete the intermediate Info.plist so that Xcode re-preprocesses the Info.plist with our updated macros.\n# Use -f because after a clean build, this file doesn't exist yet, so a plain rm would fail and stop the build.\nrm -f \"${CONFIGURATION_TEMP_DIR}/MetaZ.build/Preprocessed-Info.plist\"\n";
};
1B8262A710B39DE400DF101B /* Make Disk Image */ = {
isa = PBXShellScriptBuildPhase;
Expand Down Expand Up @@ -1380,6 +1387,7 @@
1B032FDE10B0C62800B28E3A /* sha2.c in Sources */,
1B82685010B3EE0300DF101B /* AmazonRequest.m in Sources */,
1B8268A510B3F82400DF101B /* AmazonSearchProvider.m in Sources */,
1B15A60E10B608D100EC0FBF /* AmazonSearch.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 2 additions & 5 deletions Plugins/Amazon/src/AmazonPlugin.m
Expand Up @@ -7,7 +7,7 @@
//

#import "AmazonPlugin.h"

#import "AmazonSearchProvider.h"

@implementation AmazonPlugin

Expand All @@ -26,11 +26,8 @@ - (id)init
self = [super init];
if(self)
{
/*
TCSearchProvider* a = [[[TCSearchProvider alloc] init] autorelease];
AmazonSearchProvider* a = [[[AmazonSearchProvider alloc] init] autorelease];
searchProviders = [[NSArray arrayWithObject:a] retain];
*/
searchProviders = [[NSArray array] retain];
}
return self;
}
Expand Down
21 changes: 21 additions & 0 deletions Plugins/Amazon/src/AmazonSearch.h
@@ -0,0 +1,21 @@
//
// AmazonSearch.h
// MetaZ
//
// Created by Brian Olsen on 20/11/09.
// Copyright 2009 Maven-Group. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <MetaZKit/MetaZKit.h>
#import "AmazonPlugin.h"

@interface AmazonSearch : MZRESTSearch
{
NSDictionary* mapping;

}

- (id)initWithProvider:(id)provider delegate:(id<MZSearchProviderDelegate>)delegate url:(NSURL *)url parameters:(NSDictionary *)params;

@end
155 changes: 155 additions & 0 deletions Plugins/Amazon/src/AmazonSearch.m
@@ -0,0 +1,155 @@
//
// AmazonSearch.m
// MetaZ
//
// Created by Brian Olsen on 20/11/09.
// Copyright 2009 Maven-Group. All rights reserved.
//

#import "AmazonSearch.h"
#import "Access.h"
#import "AmazonRequest.h"

@implementation AmazonSearch

+ (Class)restWrapper
{
return [AmazonRequest class];
}

- (id)initWithProvider:(id)theProvider delegate:(id<MZSearchProviderDelegate>)theDelegate url:(NSURL *)url parameters:(NSDictionary *)params;
{
self = [super initWithProvider:theProvider delegate:theDelegate url:url usingVerb:@"GET" parameters:params];
if(self)
{
NSArray* tags = [NSArray arrayWithObjects:
MZTitleTagIdent,
MZDirectorTagIdent, MZProducerTagIdent,
MZScreenwriterTagIdent, MZActorsTagIdent,
MZShortDescriptionTagIdent,
ASINTagIdent,

MZLongDescriptionTagIdent,
MZAdvisoryTagIdent, MZCopyrightTagIdent,
MZCommentTagIdent, MZArtistTagIdent,
MZTVShowTagIdent, MZTVSeasonTagIdent,
MZTVEpisodeTagIdent, MZTVNetworkTagIdent,
MZSortTitleTagIdent, MZSortAlbumArtistTagIdent,
MZSortAlbumTagIdent, MZSortTVShowTagIdent,
MZGenreTagIdent,
nil];
NSArray* keys = [NSArray arrayWithObjects:
@"ItemAttributes/Title",
@"ItemAttributes/Director", @"ItemAttributes/Creator[@Role='Producer']",
@"ItemAttributes/Creator[@Role='Writer']", @"ItemAttributes/Actor",
@"ItemAttributes/Feature",
@"ASIN",

@"movieTags/info/longDescription",
@"movieTags/info/advisory", @"movieTags/info/copyright",
@"movieTags/info/comments", @"movieTags/info/artist/artistName",
@"movieTags/television/showName", @"movieTags/television/season",
@"movieTags/television/episode", @"movieTags/television/network",
@"movieTags/sorting/name", @"movieTags/sorting/albumArtist",
@"movieTags/sorting/album", @"movieTags/sorting/show",
@"movieTags/info/genre",
nil];
mapping = [[NSDictionary alloc] initWithObjects:tags forKeys:keys];
[wrapper setAccessKeyId:AMAZON_ACCESS_ID];
[wrapper setSecretAccessKey:AMAZON_ACCESS_KEY];
}
return self;
}

- (void)dealloc
{
[mapping release];
[super dealloc];
}

#pragma mark - MZRESTWrapperDelegate

- (void)wrapper:(MZRESTWrapper *)theWrapper didRetrieveData:(NSData *)data
{
//NSLog(@"Got amazon response:\n%@", [theWrapper responseAsText]);
NSXMLDocument* doc = [theWrapper responseAsXml];

NSString* errorMessage = [doc stringForXPath:@"/ItemSearchResponse/Items/Request/Errors/Error/Code" error:NULL];
if(![errorMessage isEqual:@""])
NSLog(@"Amazon error: %@", errorMessage);

NSString* totalResults = [doc stringForXPath:@"/ItemSearchResponse/Items/TotalResults" error:NULL];
NSString* totalPages = [doc stringForXPath:@"/ItemSearchResponse/Items/TotalPages" error:NULL];

NSArray* items = [doc nodesForXPath:@"/ItemSearchResponse/Items/Item" error:NULL];
NSMutableArray* results = [NSMutableArray array];
NSLog(@"Got results %d", [items count]);
for(NSXMLElement* item in items)
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for(NSString* xpath in [mapping allKeys])
{
NSString* tagId = [mapping objectForKey:xpath];
MZTag* tag = [MZTag tagForIdentifier:tagId];
NSString* value = [item stringForXPath:xpath error:NULL];
if([value length] > 0)
{
id obj = [tag objectFromString:value];
if(obj)
[dict setObject:obj forKey:tagId];
}
}

NSString* asin = [dict objectForKey:ASINTagIdent];

NSArray* reviews = [item nodesForXPath:@"EditorialReviews/EditorialReview/Content" error:NULL];
if([reviews count] > 0)
{
NSString* review = [[reviews objectAtIndex:0] stringValue];
[dict setObject:review forKey:MZLongDescriptionTagIdent];
}

MZTag* ratingTag = [MZTag tagForIdentifier:MZRatingTagIdent];
NSString* rating = [item stringForXPath:@"ItemAttributes/AudienceRating" error:NULL];
NSNumber* ratingNr = [ratingTag objectFromString:rating];
if([ratingNr intValue] != MZNoRating)
[dict setObject:ratingNr forKey:MZRatingTagIdent];
/*
NSInteger ratingNr = [ratingNames indexOfObject:rating];
if(ratingNr != NSNotFound)
{
[dict setObject:[NSNumber numberWithInt:ratingNr] forKey:MZRatingTagIdent];
}
*/


NSString* coverArtLarge = [item stringForXPath:@"LargeImage/URL" error:NULL];
if([coverArtLarge length] > 0)
{
/*
NSLog(@"ASIN %@", asin);
NSLog(@"Image large url: %@", coverArtLarge);
*/
NSURL* url = [NSURL URLWithString:coverArtLarge];
MZRemoteData* data = [MZRemoteData dataWithURL:url];
[dict setObject:data forKey:MZPictureTagIdent];
if([NSThread mainThread] != [NSThread currentThread])
[data performSelectorOnMainThread:@selector(loadData) withObject:nil waitUntilDone:NO];
else
[data loadData];
}

MZSearchResult* result = [MZSearchResult resultWithOwner:provider dictionary:dict];
[results addObject:result];
}

NSLog(@"Parsed results %d", [results count]);
[delegate searchProvider:provider result:results];

// TODO Make more requests for other pages
[delegate searchFinished];
self.isExecuting = NO;
self.isFinished = YES;
}

@end
2 changes: 2 additions & 0 deletions Plugins/Amazon/src/AmazonSearchProvider.h
Expand Up @@ -8,9 +8,11 @@

#import <Cocoa/Cocoa.h>
#import <MetaZKit/MetaZKit.h>
#import "AmazonSearch.h"

@interface AmazonSearchProvider : NSObject <MZSearchProvider>
{
AmazonSearch* search;
NSImage* icon;
NSArray* supportedSearchTags;
NSMenu* menu;
Expand Down

0 comments on commit 8fe20f9

Please sign in to comment.