Permalink
Browse files

Get episode info when dropped (so we can ask the user to select the c…

…orrect series at this point, eventually)
  • Loading branch information...
1 parent 3c61c86 commit 46611bbf820ff437815429958c0c203a5d7be000 @dbr committed May 15, 2009
Showing with 116 additions and 28 deletions.
  1. +9 −7 AppCon.m
  2. +57 −5 TableListCon.m
  3. +4 −4 tvdb_api_wrapper.h
  4. +46 −12 tvdb_api_wrapper.m
View
@@ -14,18 +14,20 @@ -(id)init{
}
--(IBAction) rename: (id)sender{
+-(IBAction)rename:(id)sender{
+ [busy setHidden:NO];
+ [busy startAnimation:self];
+
tvdb_api_wrapper *api = [[tvdb_api_wrapper alloc] init];
[api autorelease];
for(id cur_file in theFiles){
- NSMutableDictionary *parsed_name = [api parseName:
- [cur_file objectForKey:@"filename"]];
- NSString *epname = [api getEpisodeNameForSeries:[parsed_name objectForKey:@"file_seriesname"]
- seasno:[parsed_name objectForKey:@"seasno"]
- epno:[parsed_name objectForKey:@"epno"]];
- NSLog(@"%@", parsed_name);
+ NSString *epname;
+
NSLog(@"%@", epname);
+
+ [busy setHidden:YES];
+ [busy stopAnimation:self];
}
}
View
@@ -1,4 +1,5 @@
#import "TableListCon.h"
+#import "tvdb_api_wrapper.h"
@implementation TableListCon
- (void)awakeFromNib
@@ -29,14 +30,58 @@ - (NSDragOperation)tableView:(NSTableView*)tv
- (void)addFileToList:(NSString*)path
{
NSArray *components = [path pathComponents];
- NSString *filename = [components lastObject];
+ NSString *old_filename = [components lastObject];
+
+ tvdb_api_wrapper *api = [[tvdb_api_wrapper alloc] init];
+ [api autorelease];
+
+ // Parse name, get: series_filename, seasno, epno
+ NSMutableDictionary *parsed_name = [api parseName:old_filename];
+ if(!parsed_name) return; // Invalid filename
+
+ NSMutableDictionary *seriesinfo = [api getSeriesId:[parsed_name objectForKey:@"file_seriesname"]];
+ NSLog(@"Got series %@ with ID %@",
+ [seriesinfo objectForKey:@"name"],
+ [seriesinfo objectForKey:@"sid"]);
+
+ NSString *epname = [api getEpNameForSid:
+ [NSNumber numberWithLong:[[seriesinfo objectForKey:@"sid"] doubleValue]]
+ seasno:[parsed_name objectForKey:@"seasno"]
+ epno:[parsed_name objectForKey:@"epno"]];
+ NSString *new_filename;
+
+ if(epname){
+ NSLog(@"Got episode name: %@", epname);
+ new_filename = [NSString stringWithFormat:@"%@ - [%02dx%02d] - %@",
+ [seriesinfo objectForKey:@"name"],
+ [[parsed_name objectForKey:@"seasno"] intValue],
+ [[parsed_name objectForKey:@"epno"] intValue],
+ epname
+ ];
+ }
+ else
+ {
+ new_filename = [NSString stringWithFormat:@"%@ - [%02dx%02d]",
+ [parsed_name objectForKey:@"file_seriesname"],
+ [[parsed_name objectForKey:@"seasno"] intValue],
+ [[parsed_name objectForKey:@"epno"] intValue],
+ epname
+ ];
+
+ }
+
+ NSString *displaystr = [NSString stringWithFormat:@"Old: %@\nNew: %@",
+ old_filename,
+ new_filename
+ ];
// Generate dict of new file
NSDictionary *cfile = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- filename, @"filename",
- [NSNumber numberWithBool:YES], @"rename",
- path, @"path",
- nil];
+ displaystr, @"displaystr",
+ [NSNumber numberWithBool:YES], @"rename",
+ path, @"path",
+ parsed_name, @"parsed_name",
+ nil];
// Add it to the tableView's array controller
[ArrayCon addObject:cfile];
@@ -66,6 +111,9 @@ - (BOOL)tableView:(NSTableView*)tv
row:(int)row
dropOperation:(NSTableViewDropOperation)op
{
+ [busy setHidden:NO];
+ [busy startAnimation:self];
+
NSPasteboard *pboard = [info draggingPasteboard];
if([[pboard types] containsObject:NSFilenamesPboardType])
{
@@ -81,9 +129,13 @@ - (BOOL)tableView:(NSTableView*)tv
}
// Done all files
+ [busy setHidden:YES];
+ [busy stopAnimation:self];
return YES;
}
+ [busy setHidden:YES];
+ [busy stopAnimation:self];
return NO;
}
@end
View
@@ -5,8 +5,8 @@
}
-(NSMutableDictionary*) parseName:(NSString*)name;
--(NSString*)getEpisodeNameForSeries:(NSString*)seriesName
- seasno:(NSNumber*)seasno
- epno:(NSNumber*)epno;
-
+-(NSMutableDictionary*)getSeriesId:(NSString*)seriesName;
+-(NSString*)getEpNameForSid:(NSNumber*)sid
+ seasno:(NSNumber*)seasno
+ epno:(NSNumber*)epno;
@end
View
@@ -21,10 +21,24 @@ -(PyObject*)callMethod:(NSString*)method
PyObject *thefunc = PyObject_GetAttrString(module, [method UTF8String]);
NSInteger num_args = [args count];
PyObject *arg_tuple = PyTuple_New(num_args);
+
for(NSInteger i = 0; i < num_args; i++)
{
- NSString *cur_arg = (NSString*)[args objectAtIndex:i];
- PyTuple_SetItem(arg_tuple, i, PyString_FromString([cur_arg UTF8String]));
+ id cur_arg = [args objectAtIndex:i];
+ PyObject *cur_arg_cast;
+ if( [cur_arg isKindOfClass:[NSString class]] ){
+ cur_arg_cast = PyString_FromString([cur_arg UTF8String]);
+ }
+ else if([cur_arg isKindOfClass:[NSNumber class]])
+ {
+ cur_arg_cast = PyInt_FromLong([cur_arg longValue]);
+ }
+ else
+ {
+ NSLog(@"WARNING: Unknown arg (type %@), casting to string", [cur_arg class]);
+ cur_arg_cast = PyString_FromString([(NSString*)cur_arg UTF8String]);
+ }
+ PyTuple_SetItem(arg_tuple, i, cur_arg_cast);
}
PyObject *result = PyObject_CallObject(thefunc, arg_tuple);
@@ -51,6 +65,10 @@ -(NSMutableDictionary*)pyDictToNSDict:(PyObject*)thePyDict
{
value = [NSNumber numberWithLong:PyInt_AsLong(cur_value)];
}
+ else if(PyLong_Check(cur_value))
+ {
+ value = [NSNumber numberWithLong:PyLong_AsLong(cur_value)];
+ }
else
{
NSLog(@"WARNING: value for %@ was not string or integer, using repr()", key);
@@ -63,21 +81,23 @@ -(NSMutableDictionary*)pyDictToNSDict:(PyObject*)thePyDict
return ret;
}
--(NSNumber*)getSeriesId:(NSString*)seriesName
/* Public'ish methods */
+-(NSMutableDictionary*)getSeriesId:(NSString*)seriesName
{
/* Takes a series name, returns the series ID and name in dictionary */
+ Py_Initialize();
PyObject *module = [self importModule:@"tvdb_api"];
PyObject *tvdb_api = PyObject_GetAttrString(module, "Tvdb");
PyObject *tvdb = PyInstance_New(tvdb_api, nil, nil);
PyObject *py_showinfo = [self callMethod:@"_getSeries"
fromModule:tvdb
- withArgs:[NSArray arrayWithObjects:@"scrubs", nil]];
+ withArgs:[NSArray arrayWithObject:seriesName]];
+
NSMutableDictionary *showinfo = [self pyDictToNSDict:py_showinfo];
- NSLog(@"%@", showinfo);
- return [showinfo objectForKey:@"sid"];
+ Py_Finalize();
+ return showinfo;
}
-(NSMutableDictionary*)parseName:(NSString*)name
@@ -98,15 +118,29 @@ -(NSMutableDictionary*)parseName:(NSString*)name
return ret;
}
--(NSString*)getEpisodeNameForSeries:(NSString*)seriesName
- seasno:(NSNumber*)seasno
- epno:(NSNumber*)epno
+-(NSString*)getEpNameForSid:(NSNumber*)sid
+ seasno:(NSNumber*)seasno
+ epno:(NSNumber*)epno
{
Py_Initialize();
- NSNumber *sid = [self getSeriesId:seriesName];
- NSLog(@"Got series ID of %@", sid);
+ NSLog(@"Getting ep name for %@ - %@x%@", sid, seasno, epno);
+ PyObject *module = [self importModule:@"tvdb_api"];
+ PyObject *tvdb_api = PyObject_GetAttrString(module, "Tvdb");
+ PyObject *tvdb = PyInstance_New(tvdb_api, nil, nil);
+ [self callMethod:@"_getShowData"
+ fromModule:tvdb
+ withArgs:[NSArray arrayWithObject:sid]];
+
+ PyObject *show = [self callMethod:@"__getitem__"
+ fromModule:tvdb
+ withArgs:[NSArray arrayWithObject:sid]];
+
+ PyObject *season = PyObject_GetItem(show, PyInt_FromLong([seasno longValue]));
+ PyObject *episode = PyObject_GetItem(season, PyInt_FromLong([epno longValue]));
+ PyObject *attr = PyObject_GetItem(episode, PyString_FromString("episodename"));
+ NSString *epname = [NSString stringWithUTF8String:PyString_AsString(attr)];
Py_Finalize();
- return [NSString string];
+ return epname;
}
@end

0 comments on commit 46611bb

Please sign in to comment.