-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplediscover.min.js
1 lines (1 loc) · 2.97 KB
/
simplediscover.min.js
1
!function(t){"function"==typeof define&&define.amd?define(["descoverer"],t):t()}(function(){function getJSON(_url,_data,_success,_fail,_datatype,_method){function serialize(t){var e=url.indexOf("?")>-1?"":"?";for(var r in t)""==e&&"?"==e||(e+="&"),e+=r+"="+encodeURIComponent(t[r]);return e}var url=_url.url||_url||"",data=_url.data||_data||"",success=_url.success||_success||function(t){},fail=_url.fail||_fail||function(t){},datatype=_url.datatype||_datatype||"json",method=_url.method||_method||"GET";url+=serialize(data);var xmlhttp;xmlhttp=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),xmlhttp.onreadystatechange=function(){function cb(t){return t}if(xmlhttp.readyState==XMLHttpRequest.DONE)if(200==xmlhttp.status){var response=xmlhttp.responseText;eval("response = "+response),success(response)}else fail(400==xmlhttp.status?"status:"+xmlhttp.status:"status:"+xmlhttp.status)},xmlhttp.open(method,url,!0),xmlhttp.send()}var API_BASE="http://api.themoviedb.org/3/",API_KEY=document.querySelector("script[data-tmdbkey]").getAttribute("data-tmdbkey"),CONFIG;return Discoverer=function(){var t=API_BASE+"discover/movie",e={},r={genre:"with_genres",people:"with_people",person:"with_people",date:"primary_release_date",year:"primary_release_year"},i={date:"release_date"},s={"<":"lte",">":"gte"};return{filters:[],rawfilters:[],sort:void 0,page:1,total_pages:1,addFilter:function(t,e,r){this.rawfilters.push({prop:t,val:r,cond:e})},loopFilters:function(t){var e=this;if(this.rawfilters.length>0){var r=this.rawfilters.length-1;this.buildFilters(this.rawfilters[r].prop,this.rawfilters[r].val,this.rawfilters[r].cond,function(){e.rawfilters.pop(),e.loopFilters(t)})}else t()},buildFilters:function(t,r,i,s){var o=this;"people"==t||"person"==t?r=this.addPeople(r.split(","),"",function(e){o.putFilter(t,e.replace(/(^,)|(,$)/g,""),i),s()}):"genre"==t?getJSON(API_BASE+"genre/movie/list?callback=cb",{api_key:API_KEY},function(a){var n={};(a=a.genres).forEach(function(t){n[t.name.toLowerCase()]=t.id}),e.genre=n,o.putFilter(t,r,i),s()}):(o.putFilter(t,r,i),s())},putFilter:function(t,r,i){this.filters.push({prop:t,cond:i?s[i]:void 0,val:e[t]?e[t][r]:r})},addSort:function(t,e){this.sort={prop:t}},discover:function(e){var r=this;this.loopFilters(function(){var i=r.buildSends();getJSON(t,i,function(t){r.total_pages=t.total_pages,t.config=CONFIG,e(t)})})},buildSends:function(){var t={api_key:API_KEY,page:this.page};return this.filters.forEach(function(e){var i=void 0!=e.cond?"."+e.cond:"";t[(r[e.prop]?r[e.prop]:e.prop)+i]=e.val}),this.sort&&(t.sort_by=(i[this.sort.prop]?i[this.sort.prop]:r[this.sort.prop]?r[this.sort.prop]:this.sort.prop)+"."+this.sort.dir),t},addPeople:function(t,e,r){var i=e.split(","),s=t.length-i.length,o=this;getJSON(API_BASE+"search/person?callback=cb",{api_key:API_KEY,query:t[s]},function(i){var a=parseInt(i.results[0].id);(e+=","+a).trim(","),s>0?o.addPeople(t,e,r):r(e)},"json")}}},getJSON(API_BASE+"configuration",{api_key:API_KEY},function(t){CONFIG=t}),Discoverer});