-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplediscover.js
230 lines (225 loc) · 5.09 KB
/
simplediscover.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(function(factory)
{
if (typeof define === "function" && define.amd) define(["descoverer"],
factory);
else factory();
})(function()
{
var API_BASE = "http://api.themoviedb.org/3/";
var API_KEY = document.querySelector("script[data-tmdbkey]").getAttribute("data-tmdbkey");
var CONFIG;
Discoverer = function()
{
var disc_url = API_BASE + "discover/movie";
var properties = {};
var prop_fix = {
"genre": "with_genres",
"people": "with_people",
"person": "with_people",
"date": "primary_release_date",
"year": "primary_release_year"
};
var sort_fix = {
"date": "release_date"
};
var conds = {
"<": "lte",
">": "gte"
};
var obj = {
filters: [],
rawfilters: [],
sort: undefined,
page: 1,
total_pages:1,
addFilter: function(property, cond, value)
{
this.rawfilters.push(
{
prop: property,
val: value,
cond: cond
});
},
loopFilters: function(_cb)
{
var This = this;
if (this.rawfilters.length > 0)
{
var index = this.rawfilters.length - 1;
this.buildFilters(this.rawfilters[index].prop, this.rawfilters[index].val,
this.rawfilters[index].cond, function()
{
This.rawfilters.pop();
This.loopFilters(_cb);
});
}
else
{
_cb();
};
},
buildFilters: function(property, value, cond, callback)
{
var This = this;
if (property == "people" || property == "person")
{
value = this.addPeople(value.split(","), "", function(ids)
{
This.putFilter(property, ids.replace(/(^,)|(,$)/g, ""), cond);
callback();
});
}
else if (property == "genre")
{
getJSON(API_BASE + "genre/movie/list?callback=cb",
{
api_key: API_KEY
}, function(gen)
{
gen = gen.genres;
var genres = {};
gen.forEach(function(g)
{
genres[g.name.toLowerCase()] = g.id;
});
properties.genre = genres;
This.putFilter(property, value, cond);
callback();
});
}
else
{
This.putFilter(property, value, cond);
callback();
};
},
putFilter: function(property, value, cond)
{
this.filters.push(
{
prop: property,
cond: (cond) ? conds[cond] : undefined,
val: (properties[property] ? properties[property][value] : value)
});
},
addSort: function(property, dir)
{
this.sort = (
{
prop: property,
dir
});
},
discover: function(callback)
{
var This = this;
this.loopFilters(function()
{
var sends = This.buildSends();
getJSON(disc_url, sends, function(data)
{
This.total_pages = data.total_pages;
data.config = CONFIG;
callback(data);
});
});
},
buildSends: function()
{
var tmp = {
api_key: API_KEY,
page: this.page
};
this.filters.forEach(function(f)
{
var cond = (f.cond != undefined) ? "." + f.cond : "";
tmp[(prop_fix[f.prop] ? prop_fix[f.prop] : f.prop) + cond] = f.val;
});
if (this.sort) tmp['sort_by'] = (sort_fix[this.sort.prop] ? sort_fix[
this.sort.prop] : (prop_fix[this.sort.prop] ? prop_fix[this.sort.prop] :
this.sort.prop)) + "." + this.sort.dir;
return tmp;
},
addPeople: function(value, found, callback)
{
var fnds = found.split(",");
var index = value.length - fnds.length;
var This = this;
getJSON(API_BASE + "search/person?callback=cb",
{
api_key: API_KEY,
query: value[index]
}, function(data)
{
var id = parseInt(data.results[0].id);
found += "," + id;
found.trim(",");
if (index > 0) This.addPeople(value, found, callback);
else callback(found);
}, "json");
}
};
return obj;
};
getJSON(API_BASE+"configuration",{api_key:API_KEY},function(response){
CONFIG = response;
});
function getJSON(_url, _data, _success, _fail, _datatype, _method)
{
var url = _url.url || _url || "";
var data = _url.data || _data || "";
var success = _url.success || _success || function(response) {};
var fail = _url.fail || _fail || function(response) {};
var datatype = _url.datatype || _datatype || "json";
var method = _url.method || _method || "GET";
url = url + serialize(data);
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
};
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == XMLHttpRequest.DONE)
{
if (xmlhttp.status == 200)
{
var response = xmlhttp.responseText;
eval("response = " + response);
function cb(dta)
{
return dta;
};
success(response);
}
else if (xmlhttp.status == 400)
{
fail("status:" + xmlhttp.status);
}
else
{
fail("status:" + xmlhttp.status)
};
};
};
xmlhttp.open(method, url, true);
xmlhttp.send();
function serialize(obj)
{
var q = url.indexOf("?") > -1;
var str = (q) ? "" : "?";
for (var key in obj)
{
if (str != "" || str != "?") str += "&";
str += key + "=" + encodeURIComponent(obj[key]);
};
return str;
};
};
return Discoverer;
});