This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtvdb_api_wrapper.m
165 lines (141 loc) · 5.13 KB
/
tvdb_api_wrapper.m
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#import "tvdb_api_wrapper.h"
#import <Python/Python.h>
@implementation tvdb_api_wrapper
-(PyObject*)importModule:(NSString*)name
{
/* Imports a Python module
*/
PyObject *module = PyImport_Import(PyString_FromString([name UTF8String]));
return module;
}
-(PyObject*)callMethod:(NSString*)method
fromModule:(PyObject*)module
withArgs:(NSArray*)args
{
/* Takes a method (from importModule) and calls a method on it,
* takes an array with arguments
*/
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++)
{
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);
return result;
}
-(NSMutableDictionary*)pyDictToNSDict:(PyObject*)thePyDict
{
/* Takes a PyObject dictionary, turns it into a NSMutableDictionary.
* Currently only handles str/unicode/integer data-types
*/
NSMutableDictionary *ret = [NSMutableDictionary dictionary];
PyObject *cur_key, *cur_value;
Py_ssize_t pos = 0;
while(PyDict_Next(thePyDict, &pos, &cur_key, &cur_value)){
NSString *key = [NSString stringWithUTF8String:PyString_AsString(cur_key)];
id value;
if(PyString_Check(cur_value) || PyUnicode_Check(cur_value)){
value = [NSString stringWithUTF8String:PyString_AsString(cur_value)];
}
else if(PyInt_Check(cur_value))
{
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);
value = [NSString stringWithUTF8String:
PyString_AsString(PyObject_Repr(cur_value))];
}
[ret setObject:value forKey:key];
}
return ret;
}
/* 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 arrayWithObject:seriesName]];
NSMutableDictionary *showinfo = [self pyDictToNSDict:py_showinfo];
Py_Finalize();
return showinfo;
}
-(NSMutableDictionary*)parseName:(NSString*)name
{
Py_Initialize();
PyObject *module = [self importModule:@"tvnamer"];
PyObject *valid_name = [self callMethod:@"processSingleName"
fromModule:module
withArgs:[NSArray arrayWithObjects:name, nil]];
if(valid_name == Py_None){
NSLog(@"Not found!");
return nil;
}
NSMutableDictionary *ret = [self pyDictToNSDict:valid_name];
Py_Finalize();
return ret;
}
-(NSString*)getEpNameForSid:(NSNumber*)sid
seasno:(NSNumber*)seasno
epno:(NSNumber*)epno
{
Py_Initialize();
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]));
if(!season){
Py_Finalize();
NSLog(@"Panic, season is null");
return nil;
}
PyObject *episode = PyObject_GetItem(season, PyInt_FromLong([epno longValue]));
if(!episode){
Py_Finalize();
NSLog(@"Panic, episode is null");
return nil;
};
PyObject *attr = PyObject_GetItem(episode, PyString_FromString("episodename"));
if(!attr){
Py_Finalize();
NSLog(@"Panic, attr is null");
return nil;
}
NSString *epname = [NSString stringWithUTF8String:PyString_AsString(attr)];
Py_Finalize();
return epname;
}
@end