ApiClient.js 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, window) {
  5. /**
  6. * Creates a new api client instance
  7. * @param {String} serverProtocol
  8. * @param {String} serverHostName
  9. * @param {String} serverPortNumber
  10. * @param {String} clientName
  11. * @param {String} applicationVersion
  12. */
  13. return function (serverProtocol, serverHostName, serverPortNumber, clientName, applicationVersion) {
  14. if (!serverProtocol) {
  15. throw new Error("Must supply a serverProtocol, e.g. http:");
  16. }
  17. if (!serverHostName) {
  18. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  19. }
  20. var self = this;
  21. var deviceName = "Web Browser";
  22. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  23. var currentUserId;
  24. var webSocket;
  25. /**
  26. * Gets the server host name.
  27. */
  28. self.serverHostName = function () {
  29. return serverHostName;
  30. };
  31. /**
  32. * Gets or sets the current user id.
  33. */
  34. self.currentUserId = function (val) {
  35. if (val !== undefined) {
  36. currentUserId = val;
  37. } else {
  38. return currentUserId;
  39. }
  40. };
  41. deviceName = (function () {
  42. var name = "";
  43. if ($.browser.chrome) {
  44. name = "Chrome";
  45. } else if ($.browser.safari) {
  46. name = "Safari";
  47. } else if ($.browser.webkit) {
  48. name = "WebKit";
  49. } else if ($.browser.msie) {
  50. name = "Internet Explorer";
  51. } else if ($.browser.opera) {
  52. name = "Opera";
  53. } else if ($.browser.firefox || $.browser.mozilla) {
  54. name = "Firefox";
  55. }
  56. if (name) {
  57. if ($.browser.version) {
  58. name += " " + $.browser.version;
  59. }
  60. } else {
  61. name = "Web Browser";
  62. }
  63. if ($.browser.ipad) {
  64. name += " Ipad";
  65. } else if ($.browser.iphone) {
  66. name += " Iphone";
  67. } else if ($.browser.android) {
  68. name += " Android";
  69. }
  70. return name;
  71. }());
  72. self.deviceId = function () {
  73. return deviceId;
  74. };
  75. self.encodeName = function (name) {
  76. name = name.split('/').join('-');
  77. name = name.split('?').join('-');
  78. var val = $.param({ name: name });
  79. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  80. };
  81. /**
  82. * Wraps around jQuery ajax methods to add additional info to the request.
  83. */
  84. self.ajax = function (request) {
  85. if (!request) {
  86. throw new Error("Request cannot be null");
  87. }
  88. if (clientName) {
  89. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '", Version="' + applicationVersion + '"';
  90. if (currentUserId) {
  91. auth += ', UserId="' + currentUserId + '"';
  92. }
  93. request.headers = {
  94. Authorization: auth
  95. };
  96. }
  97. return $.ajax(request);
  98. };
  99. /**
  100. * Creates an api url based on a handler name and query string parameters
  101. * @param {String} name
  102. * @param {Object} params
  103. */
  104. self.getUrl = function (name, params) {
  105. if (!name) {
  106. throw new Error("Url name cannot be empty");
  107. }
  108. var url = serverProtocol + "//" + serverHostName;
  109. if (serverPortNumber) {
  110. url += ":" + serverPortNumber;
  111. }
  112. url += "/mediabrowser/" + name;
  113. if (params) {
  114. url += "?" + $.param(params);
  115. }
  116. return url;
  117. };
  118. self.openWebSocket = function (port) {
  119. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  120. webSocket = new WebSocket(url);
  121. webSocket.onmessage = function (msg) {
  122. msg = JSON.parse(msg.data);
  123. $(self).trigger("websocketmessage", [msg]);
  124. };
  125. webSocket.onopen = function () {
  126. setTimeout(function () {
  127. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId + "|" + applicationVersion + "|" + deviceName);
  128. $(self).trigger("websocketopen");
  129. }, 500);
  130. };
  131. webSocket.onerror = function () {
  132. setTimeout(function () {
  133. $(self).trigger("websocketerror");
  134. }, 0);
  135. };
  136. webSocket.onclose = function () {
  137. setTimeout(function () {
  138. $(self).trigger("websocketclose");
  139. }, 0);
  140. };
  141. };
  142. self.closeWebSocket = function () {
  143. if (webSocket && webSocket.readyState === WebSocket.OPEN) {
  144. webSocket.close();
  145. }
  146. };
  147. self.sendWebSocketMessage = function (name, data) {
  148. var msg = { MessageType: name };
  149. if (data) {
  150. msg.Data = data;
  151. }
  152. msg = JSON.stringify(msg);
  153. webSocket.send(msg);
  154. };
  155. self.isWebSocketOpen = function () {
  156. return webSocket && webSocket.readyState === WebSocket.OPEN;
  157. };
  158. self.isWebSocketOpenOrConnecting = function () {
  159. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  160. };
  161. /**
  162. * Gets an item from the server
  163. * Omit itemId to get the root folder.
  164. */
  165. self.getItem = function (userId, itemId) {
  166. if (!userId) {
  167. throw new Error("null userId");
  168. }
  169. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  170. return self.ajax({
  171. type: "GET",
  172. url: url,
  173. dataType: "json"
  174. });
  175. };
  176. /**
  177. * Gets the root folder from the server
  178. */
  179. self.getRootFolder = function (userId) {
  180. if (!userId) {
  181. throw new Error("null userId");
  182. }
  183. var url = self.getUrl("Users/" + userId + "/Items/Root");
  184. return self.ajax({
  185. type: "GET",
  186. url: url,
  187. dataType: "json"
  188. });
  189. };
  190. self.getNotificationSummary = function (userId) {
  191. if (!userId) {
  192. throw new Error("null userId");
  193. }
  194. var url = self.getUrl("Notifications/" + userId + "/Summary");
  195. return self.ajax({
  196. type: "GET",
  197. url: url,
  198. dataType: "json"
  199. });
  200. };
  201. self.getNotifications = function (userId, options) {
  202. if (!userId) {
  203. throw new Error("null userId");
  204. }
  205. var url = self.getUrl("Notifications/" + userId, options || {});
  206. return self.ajax({
  207. type: "GET",
  208. url: url,
  209. dataType: "json"
  210. });
  211. };
  212. self.markNotificationsRead = function (userId, idList, isRead) {
  213. if (!userId) {
  214. throw new Error("null userId");
  215. }
  216. if (!idList || !idList.length) {
  217. throw new Error("null idList");
  218. }
  219. var suffix = isRead ? "Read" : "Unread";
  220. var params = {
  221. UserId: userId,
  222. Ids: idList.join(',')
  223. };
  224. var url = self.getUrl("Notifications/" + userId + "/" + suffix, params);
  225. return self.ajax({
  226. type: "POST",
  227. url: url
  228. });
  229. };
  230. function getRemoteImagePrefix(options) {
  231. var urlPrefix;
  232. if (options.artist) {
  233. urlPrefix = "Artists/" + self.encodeName(options.artist);
  234. delete options.artist;
  235. } else if (options.person) {
  236. urlPrefix = "Persons/" + self.encodeName(options.person);
  237. delete options.person;
  238. } else if (options.genre) {
  239. urlPrefix = "Genres/" + self.encodeName(options.genre);
  240. delete options.genre;
  241. } else if (options.musicGenre) {
  242. urlPrefix = "MusicGenres/" + self.encodeName(options.musicGenre);
  243. delete options.musicGenre;
  244. } else if (options.gameGenre) {
  245. urlPrefix = "GameGenres/" + self.encodeName(options.gameGenre);
  246. delete options.gameGenre;
  247. } else if (options.studio) {
  248. urlPrefix = "Studios/" + self.encodeName(options.studio);
  249. delete options.studio;
  250. } else {
  251. urlPrefix = "Items/" + options.itemId;
  252. delete options.itemId;
  253. }
  254. return urlPrefix;
  255. }
  256. self.getRemoteImageProviders = function (options) {
  257. if (!options) {
  258. throw new Error("null options");
  259. }
  260. var urlPrefix = getRemoteImagePrefix(options);
  261. var url = self.getUrl(urlPrefix + "/RemoteImages/Providers", options);
  262. return self.ajax({
  263. type: "GET",
  264. url: url,
  265. dataType: "json"
  266. });
  267. };
  268. self.getAvailableRemoteImages = function (options) {
  269. if (!options) {
  270. throw new Error("null options");
  271. }
  272. var urlPrefix = getRemoteImagePrefix(options);
  273. var url = self.getUrl(urlPrefix + "/RemoteImages", options);
  274. return self.ajax({
  275. type: "GET",
  276. url: url,
  277. dataType: "json"
  278. });
  279. };
  280. self.downloadRemoteImage = function (options) {
  281. if (!options) {
  282. throw new Error("null options");
  283. }
  284. var urlPrefix = getRemoteImagePrefix(options);
  285. var url = self.getUrl(urlPrefix + "/RemoteImages/Download", options);
  286. return self.ajax({
  287. type: "POST",
  288. url: url
  289. });
  290. };
  291. self.getLiveTvServices = function (options) {
  292. var url = self.getUrl("LiveTv/Services", options || {});
  293. return self.ajax({
  294. type: "GET",
  295. url: url,
  296. dataType: "json"
  297. });
  298. };
  299. self.getLiveTvChannel = function (id, userId) {
  300. if (!id) {
  301. throw new Error("null id");
  302. }
  303. var options = {
  304. };
  305. if (userId) {
  306. options.userId = userId;
  307. }
  308. var url = self.getUrl("LiveTv/Channels/" + id, options);
  309. return self.ajax({
  310. type: "GET",
  311. url: url,
  312. dataType: "json"
  313. });
  314. };
  315. self.getLiveTvChannels = function (options) {
  316. var url = self.getUrl("LiveTv/Channels", options || {});
  317. return self.ajax({
  318. type: "GET",
  319. url: url,
  320. dataType: "json"
  321. });
  322. };
  323. self.getLiveTvPrograms = function (options) {
  324. var url = self.getUrl("LiveTv/Programs", options || {});
  325. return self.ajax({
  326. type: "GET",
  327. url: url,
  328. dataType: "json"
  329. });
  330. };
  331. self.getLiveTvRecordings = function (options) {
  332. var url = self.getUrl("LiveTv/Recordings", options || {});
  333. return self.ajax({
  334. type: "GET",
  335. url: url,
  336. dataType: "json"
  337. });
  338. };
  339. self.getLiveTvRecording = function (id, userId) {
  340. if (!id) {
  341. throw new Error("null id");
  342. }
  343. var options = {
  344. };
  345. if (userId) {
  346. options.userId = userId;
  347. }
  348. var url = self.getUrl("LiveTv/Recordings/" + id, options);
  349. return self.ajax({
  350. type: "GET",
  351. url: url,
  352. dataType: "json"
  353. });
  354. };
  355. self.getLiveTvProgram = function (id, userId) {
  356. if (!id) {
  357. throw new Error("null id");
  358. }
  359. var options = {
  360. };
  361. if (userId) {
  362. options.userId = userId;
  363. }
  364. var url = self.getUrl("LiveTv/Programs/" + id, options);
  365. return self.ajax({
  366. type: "GET",
  367. url: url,
  368. dataType: "json"
  369. });
  370. };
  371. self.deleteLiveTvRecording = function (id) {
  372. if (!id) {
  373. throw new Error("null id");
  374. }
  375. var url = self.getUrl("LiveTv/Recordings/" + id);
  376. return self.ajax({
  377. type: "DELETE",
  378. url: url
  379. });
  380. };
  381. self.cancelLiveTvTimer = function (id) {
  382. if (!id) {
  383. throw new Error("null id");
  384. }
  385. var url = self.getUrl("LiveTv/Timers/" + id);
  386. return self.ajax({
  387. type: "DELETE",
  388. url: url
  389. });
  390. };
  391. self.getLiveTvTimers = function (options) {
  392. var url = self.getUrl("LiveTv/Timers", options || {});
  393. return self.ajax({
  394. type: "GET",
  395. url: url,
  396. dataType: "json"
  397. });
  398. };
  399. self.getLiveTvTimer = function (id) {
  400. if (!id) {
  401. throw new Error("null id");
  402. }
  403. var url = self.getUrl("LiveTv/Timers/" + id);
  404. return self.ajax({
  405. type: "GET",
  406. url: url,
  407. dataType: "json"
  408. });
  409. };
  410. self.getNewLiveTvTimerDefaults = function () {
  411. var url = self.getUrl("LiveTv/Timers/Defaults");
  412. return self.ajax({
  413. type: "GET",
  414. url: url,
  415. dataType: "json"
  416. });
  417. };
  418. self.createLiveTvTimer = function (item) {
  419. if (!item) {
  420. throw new Error("null item");
  421. }
  422. var url = self.getUrl("LiveTv/Timers");
  423. return self.ajax({
  424. type: "POST",
  425. url: url,
  426. data: JSON.stringify(item),
  427. contentType: "application/json"
  428. });
  429. };
  430. self.updateLiveTvTimer = function (item) {
  431. if (!item) {
  432. throw new Error("null item");
  433. }
  434. var url = self.getUrl("LiveTv/Timers/" + item.Id);
  435. return self.ajax({
  436. type: "POST",
  437. url: url,
  438. data: JSON.stringify(item),
  439. contentType: "application/json"
  440. });
  441. };
  442. self.getLiveTvSeriesTimers = function (options) {
  443. var url = self.getUrl("LiveTv/SeriesTimers", options || {});
  444. return self.ajax({
  445. type: "GET",
  446. url: url,
  447. dataType: "json"
  448. });
  449. };
  450. self.getLiveTvSeriesTimer = function (id) {
  451. if (!id) {
  452. throw new Error("null id");
  453. }
  454. var url = self.getUrl("LiveTv/SeriesTimers/" + id);
  455. return self.ajax({
  456. type: "GET",
  457. url: url,
  458. dataType: "json"
  459. });
  460. };
  461. self.cancelLiveTvSeriesTimer = function (id) {
  462. if (!id) {
  463. throw new Error("null id");
  464. }
  465. var url = self.getUrl("LiveTv/SeriesTimers/" + id);
  466. return self.ajax({
  467. type: "DELETE",
  468. url: url
  469. });
  470. };
  471. self.createLiveTvSeriesTimer = function (item) {
  472. if (!item) {
  473. throw new Error("null item");
  474. }
  475. var url = self.getUrl("LiveTv/SeriesTimers");
  476. return self.ajax({
  477. type: "POST",
  478. url: url,
  479. data: JSON.stringify(item),
  480. contentType: "application/json"
  481. });
  482. };
  483. self.updateLiveTvSeriesTimer = function (item) {
  484. if (!item) {
  485. throw new Error("null item");
  486. }
  487. var url = self.getUrl("LiveTv/SeriesTimers/" + item.Id);
  488. return self.ajax({
  489. type: "POST",
  490. url: url,
  491. data: JSON.stringify(item),
  492. contentType: "application/json"
  493. });
  494. };
  495. /**
  496. * Gets the current server status
  497. */
  498. self.getSystemInfo = function () {
  499. var url = self.getUrl("System/Info");
  500. return self.ajax({
  501. type: "GET",
  502. url: url,
  503. dataType: "json"
  504. });
  505. };
  506. self.getInstantMixFromSong = function (itemId, options) {
  507. var url = self.getUrl("Songs/" + itemId + "/InstantMix", options);
  508. return self.ajax({
  509. type: "GET",
  510. url: url,
  511. dataType: "json"
  512. });
  513. };
  514. self.getInstantMixFromAlbum = function (itemId, options) {
  515. var url = self.getUrl("Albums/" + itemId + "/InstantMix", options);
  516. return self.ajax({
  517. type: "GET",
  518. url: url,
  519. dataType: "json"
  520. });
  521. };
  522. self.getInstantMixFromArtist = function (name, options) {
  523. var url = self.getUrl("Artists/" + self.encodeName(name) + "/InstantMix", options);
  524. return self.ajax({
  525. type: "GET",
  526. url: url,
  527. dataType: "json"
  528. });
  529. };
  530. self.getInstantMixFromMusicGenre = function (name, options) {
  531. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/InstantMix", options);
  532. return self.ajax({
  533. type: "GET",
  534. url: url,
  535. dataType: "json"
  536. });
  537. };
  538. self.getEpisodes = function (itemId, options) {
  539. var url = self.getUrl("Shows/" + itemId + "/Episodes", options);
  540. return self.ajax({
  541. type: "GET",
  542. url: url,
  543. dataType: "json"
  544. });
  545. };
  546. self.getSeasons = function (itemId, options) {
  547. var url = self.getUrl("Shows/" + itemId + "/Seasons", options);
  548. return self.ajax({
  549. type: "GET",
  550. url: url,
  551. dataType: "json"
  552. });
  553. };
  554. self.getSimilarMovies = function (itemId, options) {
  555. var url = self.getUrl("Movies/" + itemId + "/Similar", options);
  556. return self.ajax({
  557. type: "GET",
  558. url: url,
  559. dataType: "json"
  560. });
  561. };
  562. self.getSimilarTrailers = function (itemId, options) {
  563. var url = self.getUrl("Trailers/" + itemId + "/Similar", options);
  564. return self.ajax({
  565. type: "GET",
  566. url: url,
  567. dataType: "json"
  568. });
  569. };
  570. self.getSimilarShows = function (itemId, options) {
  571. var url = self.getUrl("Shows/" + itemId + "/Similar", options);
  572. return self.ajax({
  573. type: "GET",
  574. url: url,
  575. dataType: "json"
  576. });
  577. };
  578. self.getSimilarAlbums = function (itemId, options) {
  579. var url = self.getUrl("Albums/" + itemId + "/Similar", options);
  580. return self.ajax({
  581. type: "GET",
  582. url: url,
  583. dataType: "json"
  584. });
  585. };
  586. self.getSimilarGames = function (itemId, options) {
  587. var url = self.getUrl("Games/" + itemId + "/Similar", options);
  588. return self.ajax({
  589. type: "GET",
  590. url: url,
  591. dataType: "json"
  592. });
  593. };
  594. /**
  595. * Gets all cultures known to the server
  596. */
  597. self.getCultures = function () {
  598. var url = self.getUrl("Localization/cultures");
  599. return self.ajax({
  600. type: "GET",
  601. url: url,
  602. dataType: "json"
  603. });
  604. };
  605. /**
  606. * Gets all countries known to the server
  607. */
  608. self.getCountries = function () {
  609. var url = self.getUrl("Localization/countries");
  610. return self.ajax({
  611. type: "GET",
  612. url: url,
  613. dataType: "json"
  614. });
  615. };
  616. /**
  617. * Gets plugin security info
  618. */
  619. self.getPluginSecurityInfo = function () {
  620. var url = self.getUrl("Plugins/SecurityInfo");
  621. return self.ajax({
  622. type: "GET",
  623. url: url,
  624. dataType: "json"
  625. });
  626. };
  627. /**
  628. * Gets the directory contents of a path on the server
  629. */
  630. self.getDirectoryContents = function (path, options) {
  631. if (!path) {
  632. throw new Error("null path");
  633. }
  634. options = options || {};
  635. options.path = path;
  636. var url = self.getUrl("Environment/DirectoryContents", options);
  637. return self.ajax({
  638. type: "GET",
  639. url: url,
  640. dataType: "json"
  641. });
  642. };
  643. /**
  644. * Gets a list of physical drives from the server
  645. */
  646. self.getDrives = function () {
  647. var url = self.getUrl("Environment/Drives");
  648. return self.ajax({
  649. type: "GET",
  650. url: url,
  651. dataType: "json"
  652. });
  653. };
  654. /**
  655. * Gets a list of network devices from the server
  656. */
  657. self.getNetworkDevices = function () {
  658. var url = self.getUrl("Environment/NetworkDevices");
  659. return self.ajax({
  660. type: "GET",
  661. url: url,
  662. dataType: "json"
  663. });
  664. };
  665. /**
  666. * Cancels a package installation
  667. */
  668. self.cancelPackageInstallation = function (installationId) {
  669. if (!installationId) {
  670. throw new Error("null installationId");
  671. }
  672. var url = self.getUrl("Packages/Installing/" + installationId);
  673. return self.ajax({
  674. type: "DELETE",
  675. url: url
  676. });
  677. };
  678. /**
  679. * Refreshes metadata for an item
  680. */
  681. self.refreshItem = function (itemId, force, recursive) {
  682. if (!itemId) {
  683. throw new Error("null itemId");
  684. }
  685. var url = self.getUrl("Items/" + itemId + "/Refresh", {
  686. forced: force || false,
  687. recursive: recursive || false
  688. });
  689. return self.ajax({
  690. type: "POST",
  691. url: url
  692. });
  693. };
  694. self.refreshArtist = function (name, force) {
  695. if (!name) {
  696. throw new Error("null name");
  697. }
  698. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Refresh", {
  699. forced: force || false
  700. });
  701. return self.ajax({
  702. type: "POST",
  703. url: url
  704. });
  705. };
  706. self.refreshGenre = function (name, force) {
  707. if (!name) {
  708. throw new Error("null name");
  709. }
  710. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Refresh", {
  711. forced: force || false
  712. });
  713. return self.ajax({
  714. type: "POST",
  715. url: url
  716. });
  717. };
  718. self.refreshMusicGenre = function (name, force) {
  719. if (!name) {
  720. throw new Error("null name");
  721. }
  722. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Refresh", {
  723. forced: force || false
  724. });
  725. return self.ajax({
  726. type: "POST",
  727. url: url
  728. });
  729. };
  730. self.refreshGameGenre = function (name, force) {
  731. if (!name) {
  732. throw new Error("null name");
  733. }
  734. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Refresh", {
  735. forced: force || false
  736. });
  737. return self.ajax({
  738. type: "POST",
  739. url: url
  740. });
  741. };
  742. self.refreshPerson = function (name, force) {
  743. if (!name) {
  744. throw new Error("null name");
  745. }
  746. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Refresh", {
  747. forced: force || false
  748. });
  749. return self.ajax({
  750. type: "POST",
  751. url: url
  752. });
  753. };
  754. self.refreshStudio = function (name, force) {
  755. if (!name) {
  756. throw new Error("null name");
  757. }
  758. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Refresh", {
  759. forced: force || false
  760. });
  761. return self.ajax({
  762. type: "POST",
  763. url: url
  764. });
  765. };
  766. /**
  767. * Installs or updates a new plugin
  768. */
  769. self.installPlugin = function (name, guid, updateClass, version) {
  770. if (!name) {
  771. throw new Error("null name");
  772. }
  773. if (!updateClass) {
  774. throw new Error("null updateClass");
  775. }
  776. var options = {
  777. updateClass: updateClass,
  778. AssemblyGuid: guid
  779. };
  780. if (version) {
  781. options.version = version;
  782. }
  783. var url = self.getUrl("Packages/Installed/" + name, options);
  784. return self.ajax({
  785. type: "POST",
  786. url: url
  787. });
  788. };
  789. /**
  790. * Instructs the server to perform a restart.
  791. */
  792. self.restartServer = function () {
  793. var url = self.getUrl("System/Restart");
  794. return self.ajax({
  795. type: "POST",
  796. url: url
  797. });
  798. };
  799. /**
  800. * Instructs the server to perform a shutdown.
  801. */
  802. self.shutdownServer = function () {
  803. var url = self.getUrl("System/Shutdown");
  804. return self.ajax({
  805. type: "POST",
  806. url: url
  807. });
  808. };
  809. /**
  810. * Gets information about an installable package
  811. */
  812. self.getPackageInfo = function (name, guid) {
  813. if (!name) {
  814. throw new Error("null name");
  815. }
  816. var options = {
  817. AssemblyGuid: guid
  818. };
  819. var url = self.getUrl("Packages/" + name, options);
  820. return self.ajax({
  821. type: "GET",
  822. url: url,
  823. dataType: "json"
  824. });
  825. };
  826. /**
  827. * Gets the latest available application update (if any)
  828. */
  829. self.getAvailableApplicationUpdate = function () {
  830. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  831. return self.ajax({
  832. type: "GET",
  833. url: url,
  834. dataType: "json"
  835. });
  836. };
  837. /**
  838. * Gets the latest available plugin updates (if any)
  839. */
  840. self.getAvailablePluginUpdates = function () {
  841. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  842. return self.ajax({
  843. type: "GET",
  844. url: url,
  845. dataType: "json"
  846. });
  847. };
  848. /**
  849. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  850. */
  851. self.getVirtualFolders = function (userId) {
  852. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  853. url = self.getUrl(url);
  854. return self.ajax({
  855. type: "GET",
  856. url: url,
  857. dataType: "json"
  858. });
  859. };
  860. /**
  861. * Gets all the paths of the locations in the physical root.
  862. */
  863. self.getPhysicalPaths = function () {
  864. var url = self.getUrl("Library/PhysicalPaths");
  865. return self.ajax({
  866. type: "GET",
  867. url: url,
  868. dataType: "json"
  869. });
  870. };
  871. /**
  872. * Gets the current server configuration
  873. */
  874. self.getServerConfiguration = function () {
  875. var url = self.getUrl("System/Configuration");
  876. return self.ajax({
  877. type: "GET",
  878. url: url,
  879. dataType: "json"
  880. });
  881. };
  882. /**
  883. * Gets the server's scheduled tasks
  884. */
  885. self.getScheduledTasks = function (options) {
  886. options = options || {};
  887. var url = self.getUrl("ScheduledTasks", options);
  888. return self.ajax({
  889. type: "GET",
  890. url: url,
  891. dataType: "json"
  892. });
  893. };
  894. /**
  895. * Starts a scheduled task
  896. */
  897. self.startScheduledTask = function (id) {
  898. if (!id) {
  899. throw new Error("null id");
  900. }
  901. var url = self.getUrl("ScheduledTasks/Running/" + id);
  902. return self.ajax({
  903. type: "POST",
  904. url: url
  905. });
  906. };
  907. /**
  908. * Gets a scheduled task
  909. */
  910. self.getScheduledTask = function (id) {
  911. if (!id) {
  912. throw new Error("null id");
  913. }
  914. var url = self.getUrl("ScheduledTasks/" + id);
  915. return self.ajax({
  916. type: "GET",
  917. url: url,
  918. dataType: "json"
  919. });
  920. };
  921. self.getNextUpEpisodes = function (options) {
  922. var url = self.getUrl("Shows/NextUp", options);
  923. return self.ajax({
  924. type: "GET",
  925. url: url,
  926. dataType: "json"
  927. });
  928. };
  929. /**
  930. * Stops a scheduled task
  931. */
  932. self.stopScheduledTask = function (id) {
  933. if (!id) {
  934. throw new Error("null id");
  935. }
  936. var url = self.getUrl("ScheduledTasks/Running/" + id);
  937. return self.ajax({
  938. type: "DELETE",
  939. url: url
  940. });
  941. };
  942. /**
  943. * Gets the configuration of a plugin
  944. * @param {String} Id
  945. */
  946. self.getPluginConfiguration = function (id) {
  947. if (!id) {
  948. throw new Error("null Id");
  949. }
  950. var url = self.getUrl("Plugins/" + id + "/Configuration");
  951. return self.ajax({
  952. type: "GET",
  953. url: url,
  954. dataType: "json"
  955. });
  956. };
  957. /**
  958. * Gets a list of plugins that are available to be installed
  959. */
  960. self.getAvailablePlugins = function (options) {
  961. options = $.extend({}, options || {});
  962. options.PackageType = "UserInstalled";
  963. var url = self.getUrl("Packages", options);
  964. return self.ajax({
  965. type: "GET",
  966. url: url,
  967. dataType: "json"
  968. });
  969. };
  970. /**
  971. * Uninstalls a plugin
  972. * @param {String} Id
  973. */
  974. self.uninstallPlugin = function (id) {
  975. if (!id) {
  976. throw new Error("null Id");
  977. }
  978. var url = self.getUrl("Plugins/" + id);
  979. return self.ajax({
  980. type: "DELETE",
  981. url: url
  982. });
  983. };
  984. /**
  985. * Removes a virtual folder from either the default view or a user view
  986. * @param {String} name
  987. */
  988. self.removeVirtualFolder = function (name, userId, refreshLibrary) {
  989. if (!name) {
  990. throw new Error("null name");
  991. }
  992. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  993. url += "/" + name;
  994. url = self.getUrl(url, {
  995. refreshLibrary: refreshLibrary ? true : false
  996. });
  997. return self.ajax({
  998. type: "DELETE",
  999. url: url
  1000. });
  1001. };
  1002. /**
  1003. * Adds a virtual folder to either the default view or a user view
  1004. * @param {String} name
  1005. */
  1006. self.addVirtualFolder = function (name, type, userId, refreshLibrary) {
  1007. if (!name) {
  1008. throw new Error("null name");
  1009. }
  1010. var options = {};
  1011. if (type) {
  1012. options.collectionType = type;
  1013. }
  1014. options.refreshLibrary = refreshLibrary ? true : false;
  1015. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  1016. url += "/" + name;
  1017. url = self.getUrl(url, options);
  1018. return self.ajax({
  1019. type: "POST",
  1020. url: url
  1021. });
  1022. };
  1023. /**
  1024. * Renames a virtual folder, within either the default view or a user view
  1025. * @param {String} name
  1026. */
  1027. self.renameVirtualFolder = function (name, newName, userId, refreshLibrary) {
  1028. if (!name) {
  1029. throw new Error("null name");
  1030. }
  1031. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  1032. url += "/" + name + "/Name";
  1033. url = self.getUrl(url, {
  1034. refreshLibrary: refreshLibrary ? true : false,
  1035. newName: newName
  1036. });
  1037. return self.ajax({
  1038. type: "POST",
  1039. url: url
  1040. });
  1041. };
  1042. /**
  1043. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  1044. * @param {String} name
  1045. */
  1046. self.addMediaPath = function (virtualFolderName, mediaPath, userId, refreshLibrary) {
  1047. if (!virtualFolderName) {
  1048. throw new Error("null virtualFolderName");
  1049. }
  1050. if (!mediaPath) {
  1051. throw new Error("null mediaPath");
  1052. }
  1053. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  1054. url += "/" + virtualFolderName + "/Paths";
  1055. url = self.getUrl(url, {
  1056. refreshLibrary: refreshLibrary ? true : false,
  1057. path: mediaPath
  1058. });
  1059. return self.ajax({
  1060. type: "POST",
  1061. url: url
  1062. });
  1063. };
  1064. /**
  1065. * Removes a media path from a virtual folder, within either the default view or a user view
  1066. * @param {String} name
  1067. */
  1068. self.removeMediaPath = function (virtualFolderName, mediaPath, userId, refreshLibrary) {
  1069. if (!virtualFolderName) {
  1070. throw new Error("null virtualFolderName");
  1071. }
  1072. if (!mediaPath) {
  1073. throw new Error("null mediaPath");
  1074. }
  1075. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  1076. url += "/" + virtualFolderName + "/Paths";
  1077. url = self.getUrl(url, {
  1078. refreshLibrary: refreshLibrary ? true : false,
  1079. path: mediaPath
  1080. });
  1081. return self.ajax({
  1082. type: "DELETE",
  1083. url: url
  1084. });
  1085. };
  1086. /**
  1087. * Deletes a user
  1088. * @param {String} id
  1089. */
  1090. self.deleteUser = function (id) {
  1091. if (!id) {
  1092. throw new Error("null id");
  1093. }
  1094. var url = self.getUrl("Users/" + id);
  1095. return self.ajax({
  1096. type: "DELETE",
  1097. url: url
  1098. });
  1099. };
  1100. /**
  1101. * Deletes a user image
  1102. * @param {String} userId
  1103. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  1104. */
  1105. self.deleteUserImage = function (userId, imageType, imageIndex) {
  1106. if (!userId) {
  1107. throw new Error("null userId");
  1108. }
  1109. if (!imageType) {
  1110. throw new Error("null imageType");
  1111. }
  1112. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  1113. if (imageIndex != null) {
  1114. url += "/" + imageIndex;
  1115. }
  1116. return self.ajax({
  1117. type: "DELETE",
  1118. url: url
  1119. });
  1120. };
  1121. self.deleteItemImage = function (itemId, itemType, itemName, imageType, imageIndex) {
  1122. if (!imageType) {
  1123. throw new Error("null imageType");
  1124. }
  1125. if (!itemType) {
  1126. throw new Error("null itemType");
  1127. }
  1128. var url;
  1129. if (itemType == "Artist") {
  1130. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1131. }
  1132. else if (itemType == "Channel") {
  1133. url = self.getUrl("LiveTv/Channels/" + itemId + "/Images");
  1134. }
  1135. else if (itemType == "Genre") {
  1136. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1137. } else if (itemType == "GameGenre") {
  1138. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1139. } else if (itemType == "MusicGenre") {
  1140. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1141. } else if (itemType == "Person") {
  1142. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1143. } else if (itemType == "Studio") {
  1144. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1145. } else {
  1146. url = self.getUrl("Items/" + itemId + "/Images");
  1147. }
  1148. url += "/" + imageType;
  1149. if (imageIndex != null) {
  1150. url += "/" + imageIndex;
  1151. }
  1152. return self.ajax({
  1153. type: "DELETE",
  1154. url: url
  1155. });
  1156. };
  1157. self.deleteItem = function (itemId) {
  1158. if (!itemId) {
  1159. throw new Error("null itemId");
  1160. }
  1161. var url = self.getUrl("Items/" + itemId);
  1162. return self.ajax({
  1163. type: "DELETE",
  1164. url: url
  1165. });
  1166. };
  1167. self.stopActiveEncodings = function () {
  1168. var url = self.getUrl("Videos/ActiveEncodings", {
  1169. deviceId: deviceId
  1170. });
  1171. return self.ajax({
  1172. type: "DELETE",
  1173. url: url
  1174. });
  1175. };
  1176. self.updateItemImageIndex = function (itemId, itemType, itemName, imageType, imageIndex, newIndex) {
  1177. if (!imageType) {
  1178. throw new Error("null imageType");
  1179. }
  1180. if (!itemType) {
  1181. throw new Error("null itemType");
  1182. }
  1183. var url;
  1184. var options = { newIndex: newIndex };
  1185. if (itemType == "Artist") {
  1186. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1187. }
  1188. else if (itemType == "Channel") {
  1189. url = self.getUrl("LiveTv/Channels/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1190. }
  1191. else if (itemType == "Genre") {
  1192. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1193. } else if (itemType == "GameGenre") {
  1194. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1195. } else if (itemType == "MusicGenre") {
  1196. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1197. } else if (itemType == "Person") {
  1198. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1199. } else if (itemType == "Studio") {
  1200. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1201. } else {
  1202. url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  1203. }
  1204. return self.ajax({
  1205. type: "POST",
  1206. url: url
  1207. });
  1208. };
  1209. self.getItemImageInfos = function (itemId, itemType, itemName) {
  1210. if (!itemType) {
  1211. throw new Error("null itemType");
  1212. }
  1213. var url;
  1214. if (itemType == "Artist") {
  1215. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1216. }
  1217. else if (itemType == "Channel") {
  1218. url = self.getUrl("LiveTv/Channels/" + itemId + "/Images");
  1219. }
  1220. else if (itemType == "Genre") {
  1221. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1222. } else if (itemType == "GameGenre") {
  1223. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1224. } else if (itemType == "MusicGenre") {
  1225. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1226. } else if (itemType == "Person") {
  1227. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1228. } else if (itemType == "Studio") {
  1229. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1230. } else {
  1231. url = self.getUrl("Items/" + itemId + "/Images");
  1232. }
  1233. return self.ajax({
  1234. type: "GET",
  1235. url: url,
  1236. dataType: "json"
  1237. });
  1238. };
  1239. self.getCriticReviews = function (itemId, options) {
  1240. if (!itemId) {
  1241. throw new Error("null itemId");
  1242. }
  1243. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  1244. return self.ajax({
  1245. type: "GET",
  1246. url: url,
  1247. dataType: "json"
  1248. });
  1249. };
  1250. self.getSessions = function (options) {
  1251. var url = self.getUrl("Sessions", options);
  1252. return self.ajax({
  1253. type: "GET",
  1254. url: url,
  1255. dataType: "json"
  1256. });
  1257. };
  1258. /**
  1259. * Uploads a user image
  1260. * @param {String} userId
  1261. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  1262. * @param {Object} file The file from the input element
  1263. */
  1264. self.uploadUserImage = function (userId, imageType, file) {
  1265. if (!userId) {
  1266. throw new Error("null userId");
  1267. }
  1268. if (!imageType) {
  1269. throw new Error("null imageType");
  1270. }
  1271. if (!file) {
  1272. throw new Error("File must be an image.");
  1273. }
  1274. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  1275. throw new Error("File must be an image.");
  1276. }
  1277. var deferred = $.Deferred();
  1278. var reader = new FileReader();
  1279. reader.onerror = function () {
  1280. deferred.reject();
  1281. };
  1282. reader.onabort = function () {
  1283. deferred.reject();
  1284. };
  1285. // Closure to capture the file information.
  1286. reader.onload = function (e) {
  1287. // Split by a comma to remove the url: prefix
  1288. var data = e.target.result.split(',')[1];
  1289. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  1290. self.ajax({
  1291. type: "POST",
  1292. url: url,
  1293. data: data,
  1294. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1295. }).done(function (result) {
  1296. deferred.resolveWith(null, [result]);
  1297. }).fail(function () {
  1298. deferred.reject();
  1299. });
  1300. };
  1301. // Read in the image file as a data URL.
  1302. reader.readAsDataURL(file);
  1303. return deferred.promise();
  1304. };
  1305. self.uploadItemImage = function (itemId, itemType, itemName, imageType, file) {
  1306. if (!itemId) {
  1307. throw new Error("null itemId");
  1308. }
  1309. if (!imageType) {
  1310. throw new Error("null imageType");
  1311. }
  1312. if (!file) {
  1313. throw new Error("File must be an image.");
  1314. }
  1315. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  1316. throw new Error("File must be an image.");
  1317. }
  1318. var url;
  1319. if (itemType == "Artist") {
  1320. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1321. }
  1322. else if (itemType == "Channel") {
  1323. url = self.getUrl("LiveTv/Channels/" + itemId + "/Images");
  1324. }
  1325. else if (itemType == "Genre") {
  1326. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1327. } else if (itemType == "GameGenre") {
  1328. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1329. } else if (itemType == "MusicGenre") {
  1330. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1331. } else if (itemType == "Person") {
  1332. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1333. } else if (itemType == "Studio") {
  1334. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1335. } else {
  1336. url = self.getUrl("Items/" + itemId + "/Images");
  1337. }
  1338. url += "/" + imageType;
  1339. var deferred = $.Deferred();
  1340. var reader = new FileReader();
  1341. reader.onerror = function () {
  1342. deferred.reject();
  1343. };
  1344. reader.onabort = function () {
  1345. deferred.reject();
  1346. };
  1347. // Closure to capture the file information.
  1348. reader.onload = function (e) {
  1349. // Split by a comma to remove the url: prefix
  1350. var data = e.target.result.split(',')[1];
  1351. self.ajax({
  1352. type: "POST",
  1353. url: url,
  1354. data: data,
  1355. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1356. }).done(function (result) {
  1357. deferred.resolveWith(null, [result]);
  1358. }).fail(function () {
  1359. deferred.reject();
  1360. });
  1361. };
  1362. // Read in the image file as a data URL.
  1363. reader.readAsDataURL(file);
  1364. return deferred.promise();
  1365. };
  1366. /**
  1367. * Gets the list of installed plugins on the server
  1368. */
  1369. self.getInstalledPlugins = function () {
  1370. var url = self.getUrl("Plugins");
  1371. return self.ajax({
  1372. type: "GET",
  1373. url: url,
  1374. dataType: "json"
  1375. });
  1376. };
  1377. /**
  1378. * Gets a user by id
  1379. * @param {String} id
  1380. */
  1381. self.getUser = function (id) {
  1382. if (!id) {
  1383. throw new Error("Must supply a userId");
  1384. }
  1385. var url = self.getUrl("Users/" + id);
  1386. return self.ajax({
  1387. type: "GET",
  1388. url: url,
  1389. dataType: "json"
  1390. });
  1391. };
  1392. /**
  1393. * Gets a studio
  1394. */
  1395. self.getStudio = function (name, userId) {
  1396. if (!name) {
  1397. throw new Error("null name");
  1398. }
  1399. var options = {};
  1400. if (userId) {
  1401. options.userId = userId;
  1402. }
  1403. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  1404. return self.ajax({
  1405. type: "GET",
  1406. url: url,
  1407. dataType: "json"
  1408. });
  1409. };
  1410. /**
  1411. * Gets a genre
  1412. */
  1413. self.getGenre = function (name, userId) {
  1414. if (!name) {
  1415. throw new Error("null name");
  1416. }
  1417. var options = {};
  1418. if (userId) {
  1419. options.userId = userId;
  1420. }
  1421. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  1422. return self.ajax({
  1423. type: "GET",
  1424. url: url,
  1425. dataType: "json"
  1426. });
  1427. };
  1428. self.getMusicGenre = function (name, userId) {
  1429. if (!name) {
  1430. throw new Error("null name");
  1431. }
  1432. var options = {};
  1433. if (userId) {
  1434. options.userId = userId;
  1435. }
  1436. var url = self.getUrl("MusicGenres/" + self.encodeName(name), options);
  1437. return self.ajax({
  1438. type: "GET",
  1439. url: url,
  1440. dataType: "json"
  1441. });
  1442. };
  1443. self.getGameGenre = function (name, userId) {
  1444. if (!name) {
  1445. throw new Error("null name");
  1446. }
  1447. var options = {};
  1448. if (userId) {
  1449. options.userId = userId;
  1450. }
  1451. var url = self.getUrl("GameGenres/" + self.encodeName(name), options);
  1452. return self.ajax({
  1453. type: "GET",
  1454. url: url,
  1455. dataType: "json"
  1456. });
  1457. };
  1458. /**
  1459. * Gets an artist
  1460. */
  1461. self.getArtist = function (name, userId) {
  1462. if (!name) {
  1463. throw new Error("null name");
  1464. }
  1465. var options = {};
  1466. if (userId) {
  1467. options.userId = userId;
  1468. }
  1469. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  1470. return self.ajax({
  1471. type: "GET",
  1472. url: url,
  1473. dataType: "json"
  1474. });
  1475. };
  1476. /**
  1477. * Gets a year
  1478. */
  1479. self.getYear = function (yea, userId) {
  1480. if (!name) {
  1481. throw new Error("null name");
  1482. }
  1483. var options = {};
  1484. if (userId) {
  1485. options.userId = userId;
  1486. }
  1487. var url = self.getUrl("Years/" + self.encodeName(name), options);
  1488. return self.ajax({
  1489. type: "GET",
  1490. url: url,
  1491. dataType: "json"
  1492. });
  1493. };
  1494. /**
  1495. * Gets a Person
  1496. */
  1497. self.getPerson = function (name, userId) {
  1498. if (!name) {
  1499. throw new Error("null name");
  1500. }
  1501. var options = {};
  1502. if (userId) {
  1503. options.userId = userId;
  1504. }
  1505. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  1506. return self.ajax({
  1507. type: "GET",
  1508. url: url,
  1509. dataType: "json"
  1510. });
  1511. };
  1512. self.getPublicUsers = function () {
  1513. var url = self.getUrl("users/public");
  1514. return self.ajax({
  1515. type: "GET",
  1516. url: url,
  1517. dataType: "json"
  1518. });
  1519. };
  1520. /**
  1521. * Gets all users from the server
  1522. */
  1523. self.getUsers = function (options) {
  1524. var url = self.getUrl("users", options || {});
  1525. return self.ajax({
  1526. type: "GET",
  1527. url: url,
  1528. dataType: "json"
  1529. });
  1530. };
  1531. /**
  1532. * Gets all available parental ratings from the server
  1533. */
  1534. self.getParentalRatings = function () {
  1535. var url = self.getUrl("Localization/ParentalRatings");
  1536. return self.ajax({
  1537. type: "GET",
  1538. url: url,
  1539. dataType: "json"
  1540. });
  1541. };
  1542. /**
  1543. * Gets a list of all available conrete BaseItem types from the server
  1544. */
  1545. self.getItemTypes = function (options) {
  1546. var url = self.getUrl("Library/ItemTypes", options);
  1547. return self.ajax({
  1548. type: "GET",
  1549. url: url,
  1550. dataType: "json"
  1551. });
  1552. };
  1553. /**
  1554. * Constructs a url for a user image
  1555. * @param {String} userId
  1556. * @param {Object} options
  1557. * Options supports the following properties:
  1558. * width - download the image at a fixed width
  1559. * height - download the image at a fixed height
  1560. * maxWidth - download the image at a maxWidth
  1561. * maxHeight - download the image at a maxHeight
  1562. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1563. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1564. */
  1565. self.getUserImageUrl = function (userId, options) {
  1566. if (!userId) {
  1567. throw new Error("null userId");
  1568. }
  1569. options = options || {
  1570. };
  1571. var url = "Users/" + userId + "/Images/" + options.type;
  1572. if (options.index != null) {
  1573. url += "/" + options.index;
  1574. }
  1575. // Don't put these on the query string
  1576. delete options.type;
  1577. delete options.index;
  1578. return self.getUrl(url, options);
  1579. };
  1580. /**
  1581. * Constructs a url for a person image
  1582. * @param {String} name
  1583. * @param {Object} options
  1584. * Options supports the following properties:
  1585. * width - download the image at a fixed width
  1586. * height - download the image at a fixed height
  1587. * maxWidth - download the image at a maxWidth
  1588. * maxHeight - download the image at a maxHeight
  1589. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1590. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1591. */
  1592. self.getPersonImageUrl = function (name, options) {
  1593. if (!name) {
  1594. throw new Error("null name");
  1595. }
  1596. options = options || {
  1597. };
  1598. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1599. if (options.index != null) {
  1600. url += "/" + options.index;
  1601. }
  1602. // Don't put these on the query string
  1603. delete options.type;
  1604. delete options.index;
  1605. return self.getUrl(url, options);
  1606. };
  1607. /**
  1608. * Constructs a url for a year image
  1609. * @param {String} year
  1610. * @param {Object} options
  1611. * Options supports the following properties:
  1612. * width - download the image at a fixed width
  1613. * height - download the image at a fixed height
  1614. * maxWidth - download the image at a maxWidth
  1615. * maxHeight - download the image at a maxHeight
  1616. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1617. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1618. */
  1619. self.getYearImageUrl = function (year, options) {
  1620. if (!year) {
  1621. throw new Error("null year");
  1622. }
  1623. options = options || {
  1624. };
  1625. var url = "Years/" + year + "/Images/" + options.type;
  1626. if (options.index != null) {
  1627. url += "/" + options.index;
  1628. }
  1629. // Don't put these on the query string
  1630. delete options.type;
  1631. delete options.index;
  1632. return self.getUrl(url, options);
  1633. };
  1634. /**
  1635. * Constructs a url for a genre image
  1636. * @param {String} name
  1637. * @param {Object} options
  1638. * Options supports the following properties:
  1639. * width - download the image at a fixed width
  1640. * height - download the image at a fixed height
  1641. * maxWidth - download the image at a maxWidth
  1642. * maxHeight - download the image at a maxHeight
  1643. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1644. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1645. */
  1646. self.getGenreImageUrl = function (name, options) {
  1647. if (!name) {
  1648. throw new Error("null name");
  1649. }
  1650. options = options || {
  1651. };
  1652. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1653. if (options.index != null) {
  1654. url += "/" + options.index;
  1655. }
  1656. // Don't put these on the query string
  1657. delete options.type;
  1658. delete options.index;
  1659. return self.getUrl(url, options);
  1660. };
  1661. /**
  1662. * Constructs a url for a genre image
  1663. * @param {String} name
  1664. * @param {Object} options
  1665. * Options supports the following properties:
  1666. * width - download the image at a fixed width
  1667. * height - download the image at a fixed height
  1668. * maxWidth - download the image at a maxWidth
  1669. * maxHeight - download the image at a maxHeight
  1670. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1671. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1672. */
  1673. self.getMusicGenreImageUrl = function (name, options) {
  1674. if (!name) {
  1675. throw new Error("null name");
  1676. }
  1677. options = options || {
  1678. };
  1679. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1680. if (options.index != null) {
  1681. url += "/" + options.index;
  1682. }
  1683. // Don't put these on the query string
  1684. delete options.type;
  1685. delete options.index;
  1686. return self.getUrl(url, options);
  1687. };
  1688. /**
  1689. * Constructs a url for a genre image
  1690. * @param {String} name
  1691. * @param {Object} options
  1692. * Options supports the following properties:
  1693. * width - download the image at a fixed width
  1694. * height - download the image at a fixed height
  1695. * maxWidth - download the image at a maxWidth
  1696. * maxHeight - download the image at a maxHeight
  1697. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1698. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1699. */
  1700. self.getGameGenreImageUrl = function (name, options) {
  1701. if (!name) {
  1702. throw new Error("null name");
  1703. }
  1704. options = options || {
  1705. };
  1706. var url = "GameGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1707. if (options.index != null) {
  1708. url += "/" + options.index;
  1709. }
  1710. // Don't put these on the query string
  1711. delete options.type;
  1712. delete options.index;
  1713. return self.getUrl(url, options);
  1714. };
  1715. /**
  1716. * Constructs a url for a artist image
  1717. * @param {String} name
  1718. * @param {Object} options
  1719. * Options supports the following properties:
  1720. * width - download the image at a fixed width
  1721. * height - download the image at a fixed height
  1722. * maxWidth - download the image at a maxWidth
  1723. * maxHeight - download the image at a maxHeight
  1724. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1725. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1726. */
  1727. self.getArtistImageUrl = function (name, options) {
  1728. if (!name) {
  1729. throw new Error("null name");
  1730. }
  1731. options = options || {
  1732. };
  1733. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1734. if (options.index != null) {
  1735. url += "/" + options.index;
  1736. }
  1737. // Don't put these on the query string
  1738. delete options.type;
  1739. delete options.index;
  1740. return self.getUrl(url, options);
  1741. };
  1742. /**
  1743. * Constructs a url for a studio image
  1744. * @param {String} name
  1745. * @param {Object} options
  1746. * Options supports the following properties:
  1747. * width - download the image at a fixed width
  1748. * height - download the image at a fixed height
  1749. * maxWidth - download the image at a maxWidth
  1750. * maxHeight - download the image at a maxHeight
  1751. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1752. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1753. */
  1754. self.getStudioImageUrl = function (name, options) {
  1755. if (!name) {
  1756. throw new Error("null name");
  1757. }
  1758. options = options || {
  1759. };
  1760. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1761. if (options.index != null) {
  1762. url += "/" + options.index;
  1763. }
  1764. // Don't put these on the query string
  1765. delete options.type;
  1766. delete options.index;
  1767. return self.getUrl(url, options);
  1768. };
  1769. /**
  1770. * Constructs a url for an item image
  1771. * @param {String} itemId
  1772. * @param {Object} options
  1773. * Options supports the following properties:
  1774. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1775. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1776. * width - download the image at a fixed width
  1777. * height - download the image at a fixed height
  1778. * maxWidth - download the image at a maxWidth
  1779. * maxHeight - download the image at a maxHeight
  1780. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1781. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1782. */
  1783. self.getImageUrl = function (itemId, options) {
  1784. if (!itemId) {
  1785. throw new Error("itemId cannot be empty");
  1786. }
  1787. options = options || {
  1788. };
  1789. var url = "Items/" + itemId + "/Images/" + options.type;
  1790. if (options.index != null) {
  1791. url += "/" + options.index;
  1792. }
  1793. // Don't put these on the query string
  1794. delete options.type;
  1795. delete options.index;
  1796. return self.getUrl(url, options);
  1797. };
  1798. /**
  1799. * Constructs a url for an item logo image
  1800. * If the item doesn't have a logo, it will inherit a logo from a parent
  1801. * @param {Object} item A BaseItem
  1802. * @param {Object} options
  1803. * Options supports the following properties:
  1804. * width - download the image at a fixed width
  1805. * height - download the image at a fixed height
  1806. * maxWidth - download the image at a maxWidth
  1807. * maxHeight - download the image at a maxHeight
  1808. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1809. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1810. */
  1811. self.getLogoImageUrl = function (item, options) {
  1812. if (!item) {
  1813. throw new Error("null item");
  1814. }
  1815. options = options || {
  1816. };
  1817. options.imageType = "logo";
  1818. var logoItemId = item.ImageTags && item.ImageTags.Logo ? item.Id : item.ParentLogoItemId;
  1819. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1820. };
  1821. self.getThumbImageUrl = function (item, options) {
  1822. if (!item) {
  1823. throw new Error("null item");
  1824. }
  1825. options = options || {
  1826. };
  1827. options.imageType = "thumb";
  1828. var itemId = item.ImageTags && item.ImageTags.Thumb ? item.Id : item.ParentThumbItemId;
  1829. return itemId ? self.getImageUrl(itemId, options) : null;
  1830. };
  1831. /**
  1832. * Constructs an array of backdrop image url's for an item
  1833. * If the item doesn't have any backdrops, it will inherit them from a parent
  1834. * @param {Object} item A BaseItem
  1835. * @param {Object} options
  1836. * Options supports the following properties:
  1837. * width - download the image at a fixed width
  1838. * height - download the image at a fixed height
  1839. * maxWidth - download the image at a maxWidth
  1840. * maxHeight - download the image at a maxHeight
  1841. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1842. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1843. */
  1844. self.getBackdropImageUrl = function (item, options) {
  1845. if (!item) {
  1846. throw new Error("null item");
  1847. }
  1848. options = options || {
  1849. };
  1850. options.imageType = "backdrop";
  1851. var backdropItemId;
  1852. var backdropCount;
  1853. if (!item.BackdropCount) {
  1854. backdropItemId = item.ParentBackdropItemId;
  1855. backdropCount = item.ParentBackdropCount || 0;
  1856. } else {
  1857. backdropItemId = item.Id;
  1858. backdropCount = item.BackdropCount;
  1859. }
  1860. if (!backdropItemId) {
  1861. return [];
  1862. }
  1863. var files = [];
  1864. for (var i = 0; i < backdropCount; i++) {
  1865. options.imageIndex = i;
  1866. files[i] = self.getImageUrl(backdropItemId, options);
  1867. }
  1868. return files;
  1869. };
  1870. /**
  1871. * Authenticates a user
  1872. * @param {String} name
  1873. * @param {String} password
  1874. */
  1875. self.authenticateUserByName = function (name, password) {
  1876. if (!name) {
  1877. throw new Error("null name");
  1878. }
  1879. var url = self.getUrl("Users/authenticatebyname");
  1880. var postData = {
  1881. password: MediaBrowser.SHA1(password || ""),
  1882. Username: name
  1883. };
  1884. return self.ajax({
  1885. type: "POST",
  1886. url: url,
  1887. data: JSON.stringify(postData),
  1888. dataType: "json",
  1889. contentType: "application/json"
  1890. });
  1891. };
  1892. /**
  1893. * Authenticates a user
  1894. * @param {String} userId
  1895. * @param {String} password
  1896. */
  1897. self.authenticateUser = function (userId, password) {
  1898. if (!userId) {
  1899. throw new Error("null userId");
  1900. }
  1901. var url = self.getUrl("Users/" + userId + "/authenticate");
  1902. var postData = {
  1903. password: MediaBrowser.SHA1(password || "")
  1904. };
  1905. return self.ajax({
  1906. type: "POST",
  1907. url: url,
  1908. data: JSON.stringify(postData),
  1909. dataType: "json",
  1910. contentType: "application/json"
  1911. });
  1912. };
  1913. /**
  1914. * Updates a user's password
  1915. * @param {String} userId
  1916. * @param {String} currentPassword
  1917. * @param {String} newPassword
  1918. */
  1919. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1920. if (!userId) {
  1921. throw new Error("null userId");
  1922. }
  1923. var url = self.getUrl("Users/" + userId + "/Password");
  1924. var postData = {
  1925. };
  1926. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1927. if (newPassword) {
  1928. postData.newPassword = newPassword;
  1929. }
  1930. return self.ajax({
  1931. type: "POST",
  1932. url: url,
  1933. data: postData
  1934. });
  1935. };
  1936. /**
  1937. * Resets a user's password
  1938. * @param {String} userId
  1939. */
  1940. self.resetUserPassword = function (userId) {
  1941. if (!userId) {
  1942. throw new Error("null userId");
  1943. }
  1944. var url = self.getUrl("Users/" + userId + "/Password");
  1945. var postData = {
  1946. };
  1947. postData.resetPassword = true;
  1948. return self.ajax({
  1949. type: "POST",
  1950. url: url,
  1951. data: postData
  1952. });
  1953. };
  1954. /**
  1955. * Updates the server's configuration
  1956. * @param {Object} configuration
  1957. */
  1958. self.updateServerConfiguration = function (configuration) {
  1959. if (!configuration) {
  1960. throw new Error("null configuration");
  1961. }
  1962. var url = self.getUrl("System/Configuration");
  1963. return self.ajax({
  1964. type: "POST",
  1965. url: url,
  1966. data: JSON.stringify(configuration),
  1967. contentType: "application/json"
  1968. });
  1969. };
  1970. self.updateItem = function (item) {
  1971. if (!item) {
  1972. throw new Error("null item");
  1973. }
  1974. var url = self.getUrl("Items/" + item.Id);
  1975. return self.ajax({
  1976. type: "POST",
  1977. url: url,
  1978. data: JSON.stringify(item),
  1979. contentType: "application/json"
  1980. });
  1981. };
  1982. self.updateLiveTvChannel = function (item) {
  1983. if (!item) {
  1984. throw new Error("null item");
  1985. }
  1986. var url = self.getUrl("LiveTv/Channels/" + item.Id);
  1987. return self.ajax({
  1988. type: "POST",
  1989. url: url,
  1990. data: JSON.stringify(item),
  1991. contentType: "application/json"
  1992. });
  1993. };
  1994. self.updateArtist = function (item) {
  1995. if (!item) {
  1996. throw new Error("null item");
  1997. }
  1998. var url = self.getUrl("Artists/" + self.encodeName(item.Name));
  1999. return self.ajax({
  2000. type: "POST",
  2001. url: url,
  2002. data: JSON.stringify(item),
  2003. contentType: "application/json"
  2004. });
  2005. };
  2006. self.updatePerson = function (item) {
  2007. if (!item) {
  2008. throw new Error("null item");
  2009. }
  2010. var url = self.getUrl("Persons/" + self.encodeName(item.Name));
  2011. return self.ajax({
  2012. type: "POST",
  2013. url: url,
  2014. data: JSON.stringify(item),
  2015. contentType: "application/json"
  2016. });
  2017. };
  2018. self.updateStudio = function (item) {
  2019. if (!item) {
  2020. throw new Error("null item");
  2021. }
  2022. var url = self.getUrl("Studios/" + self.encodeName(item.Name));
  2023. return self.ajax({
  2024. type: "POST",
  2025. url: url,
  2026. data: JSON.stringify(item),
  2027. contentType: "application/json"
  2028. });
  2029. };
  2030. self.updateGenre = function (item) {
  2031. if (!item) {
  2032. throw new Error("null item");
  2033. }
  2034. var url = self.getUrl("Genres/" + self.encodeName(item.Name));
  2035. return self.ajax({
  2036. type: "POST",
  2037. url: url,
  2038. data: JSON.stringify(item),
  2039. contentType: "application/json"
  2040. });
  2041. };
  2042. self.updateMusicGenre = function (item) {
  2043. if (!item) {
  2044. throw new Error("null item");
  2045. }
  2046. var url = self.getUrl("MusicGenres/" + self.encodeName(item.Name));
  2047. return self.ajax({
  2048. type: "POST",
  2049. url: url,
  2050. data: JSON.stringify(item),
  2051. contentType: "application/json"
  2052. });
  2053. };
  2054. self.updateGameGenre = function (item) {
  2055. if (!item) {
  2056. throw new Error("null item");
  2057. }
  2058. var url = self.getUrl("GameGenres/" + self.encodeName(item.Name));
  2059. return self.ajax({
  2060. type: "POST",
  2061. url: url,
  2062. data: JSON.stringify(item),
  2063. contentType: "application/json"
  2064. });
  2065. };
  2066. /**
  2067. * Updates plugin security info
  2068. */
  2069. self.updatePluginSecurityInfo = function (info) {
  2070. var url = self.getUrl("Plugins/SecurityInfo");
  2071. return self.ajax({
  2072. type: "POST",
  2073. url: url,
  2074. data: JSON.stringify(info),
  2075. contentType: "application/json"
  2076. });
  2077. };
  2078. /**
  2079. * Creates a user
  2080. * @param {Object} user
  2081. */
  2082. self.createUser = function (user) {
  2083. if (!user) {
  2084. throw new Error("null user");
  2085. }
  2086. var url = self.getUrl("Users");
  2087. return self.ajax({
  2088. type: "POST",
  2089. url: url,
  2090. data: JSON.stringify(user),
  2091. dataType: "json",
  2092. contentType: "application/json"
  2093. });
  2094. };
  2095. /**
  2096. * Updates a user
  2097. * @param {Object} user
  2098. */
  2099. self.updateUser = function (user) {
  2100. if (!user) {
  2101. throw new Error("null user");
  2102. }
  2103. var url = self.getUrl("Users/" + user.Id);
  2104. return self.ajax({
  2105. type: "POST",
  2106. url: url,
  2107. data: JSON.stringify(user),
  2108. contentType: "application/json"
  2109. });
  2110. };
  2111. /**
  2112. * Updates the Triggers for a ScheduledTask
  2113. * @param {String} id
  2114. * @param {Object} triggers
  2115. */
  2116. self.updateScheduledTaskTriggers = function (id, triggers) {
  2117. if (!id) {
  2118. throw new Error("null id");
  2119. }
  2120. if (!triggers) {
  2121. throw new Error("null triggers");
  2122. }
  2123. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  2124. return self.ajax({
  2125. type: "POST",
  2126. url: url,
  2127. data: JSON.stringify(triggers),
  2128. contentType: "application/json"
  2129. });
  2130. };
  2131. /**
  2132. * Updates a plugin's configuration
  2133. * @param {String} Id
  2134. * @param {Object} configuration
  2135. */
  2136. self.updatePluginConfiguration = function (id, configuration) {
  2137. if (!id) {
  2138. throw new Error("null Id");
  2139. }
  2140. if (!configuration) {
  2141. throw new Error("null configuration");
  2142. }
  2143. var url = self.getUrl("Plugins/" + id + "/Configuration");
  2144. return self.ajax({
  2145. type: "POST",
  2146. url: url,
  2147. data: JSON.stringify(configuration),
  2148. contentType: "application/json"
  2149. });
  2150. };
  2151. self.getAncestorItems = function (itemId, userId) {
  2152. if (!itemId) {
  2153. throw new Error("null itemId");
  2154. }
  2155. var options = {};
  2156. if (userId) {
  2157. options.userId = userId;
  2158. }
  2159. var url = self.getUrl("Items/" + itemId + "/Ancestors", options);
  2160. return self.ajax({
  2161. type: "GET",
  2162. url: url,
  2163. dataType: "json"
  2164. });
  2165. };
  2166. /**
  2167. * Gets items based on a query, typically for children of a folder
  2168. * @param {String} userId
  2169. * @param {Object} options
  2170. * Options accepts the following properties:
  2171. * itemId - Localize the search to a specific folder (root if omitted)
  2172. * startIndex - Use for paging
  2173. * limit - Use to limit results to a certain number of items
  2174. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  2175. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  2176. * sortOrder - ascending/descending
  2177. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  2178. * index - the name of the dynamic, localized index function
  2179. * dynamicSortBy - the name of the dynamic localized sort function
  2180. * recursive - Whether or not the query should be recursive
  2181. * searchTerm - search term to use as a filter
  2182. */
  2183. self.getItems = function (userId, options) {
  2184. if (!userId) {
  2185. throw new Error("null userId");
  2186. }
  2187. var url = self.getUrl("Users/" + userId + "/Items", options);
  2188. return self.ajax({
  2189. type: "GET",
  2190. url: url,
  2191. dataType: "json"
  2192. });
  2193. };
  2194. /**
  2195. Gets artists from an item
  2196. */
  2197. self.getArtists = function (userId, options) {
  2198. if (!userId) {
  2199. throw new Error("null userId");
  2200. }
  2201. options = options || {};
  2202. options.userId = userId;
  2203. var url = self.getUrl("Artists", options);
  2204. return self.ajax({
  2205. type: "GET",
  2206. url: url,
  2207. dataType: "json"
  2208. });
  2209. };
  2210. /**
  2211. Gets genres from an item
  2212. */
  2213. self.getGenres = function (userId, options) {
  2214. if (!userId) {
  2215. throw new Error("null userId");
  2216. }
  2217. options = options || {};
  2218. options.userId = userId;
  2219. var url = self.getUrl("Genres", options);
  2220. return self.ajax({
  2221. type: "GET",
  2222. url: url,
  2223. dataType: "json"
  2224. });
  2225. };
  2226. self.getMusicGenres = function (userId, options) {
  2227. if (!userId) {
  2228. throw new Error("null userId");
  2229. }
  2230. options = options || {};
  2231. options.userId = userId;
  2232. var url = self.getUrl("MusicGenres", options);
  2233. return self.ajax({
  2234. type: "GET",
  2235. url: url,
  2236. dataType: "json"
  2237. });
  2238. };
  2239. self.getGameGenres = function (userId, options) {
  2240. if (!userId) {
  2241. throw new Error("null userId");
  2242. }
  2243. options = options || {};
  2244. options.userId = userId;
  2245. var url = self.getUrl("GameGenres", options);
  2246. return self.ajax({
  2247. type: "GET",
  2248. url: url,
  2249. dataType: "json"
  2250. });
  2251. };
  2252. /**
  2253. Gets people from an item
  2254. */
  2255. self.getPeople = function (userId, options) {
  2256. if (!userId) {
  2257. throw new Error("null userId");
  2258. }
  2259. options = options || {};
  2260. options.userId = userId;
  2261. var url = self.getUrl("Persons", options);
  2262. return self.ajax({
  2263. type: "GET",
  2264. url: url,
  2265. dataType: "json"
  2266. });
  2267. };
  2268. /**
  2269. Gets studios from an item
  2270. */
  2271. self.getStudios = function (userId, options) {
  2272. if (!userId) {
  2273. throw new Error("null userId");
  2274. }
  2275. options = options || {};
  2276. options.userId = userId;
  2277. var url = self.getUrl("Studios", options);
  2278. return self.ajax({
  2279. type: "GET",
  2280. url: url,
  2281. dataType: "json"
  2282. });
  2283. };
  2284. /**
  2285. * Gets local trailers for an item
  2286. */
  2287. self.getLocalTrailers = function (userId, itemId) {
  2288. if (!userId) {
  2289. throw new Error("null userId");
  2290. }
  2291. if (!itemId) {
  2292. throw new Error("null itemId");
  2293. }
  2294. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  2295. return self.ajax({
  2296. type: "GET",
  2297. url: url,
  2298. dataType: "json"
  2299. });
  2300. };
  2301. self.getAdditionalVideoParts = function (userId, itemId) {
  2302. if (!itemId) {
  2303. throw new Error("null itemId");
  2304. }
  2305. var options = {};
  2306. if (userId) {
  2307. options.userId = userId;
  2308. }
  2309. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  2310. return self.ajax({
  2311. type: "GET",
  2312. url: url,
  2313. dataType: "json"
  2314. });
  2315. };
  2316. /**
  2317. * Gets theme songs for an item
  2318. */
  2319. self.getThemeSongs = function (userId, itemId) {
  2320. if (!itemId) {
  2321. throw new Error("null itemId");
  2322. }
  2323. var options = {};
  2324. if (userId) {
  2325. options.userId = userId;
  2326. }
  2327. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  2328. return self.ajax({
  2329. type: "GET",
  2330. url: url,
  2331. dataType: "json"
  2332. });
  2333. };
  2334. self.getThemeVideos = function (userId, itemId) {
  2335. if (!itemId) {
  2336. throw new Error("null itemId");
  2337. }
  2338. var options = {};
  2339. if (userId) {
  2340. options.userId = userId;
  2341. }
  2342. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  2343. return self.ajax({
  2344. type: "GET",
  2345. url: url,
  2346. dataType: "json"
  2347. });
  2348. };
  2349. self.getSearchHints = function (options) {
  2350. var url = self.getUrl("Search/Hints", options);
  2351. return self.ajax({
  2352. type: "GET",
  2353. url: url,
  2354. dataType: "json"
  2355. });
  2356. };
  2357. /**
  2358. * Gets special features for an item
  2359. */
  2360. self.getSpecialFeatures = function (userId, itemId) {
  2361. if (!userId) {
  2362. throw new Error("null userId");
  2363. }
  2364. if (!itemId) {
  2365. throw new Error("null itemId");
  2366. }
  2367. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  2368. return self.ajax({
  2369. type: "GET",
  2370. url: url,
  2371. dataType: "json"
  2372. });
  2373. };
  2374. self.getDateParamValue = function (date) {
  2375. function formatDigit(i) {
  2376. return i < 10 ? "0" + i : i;
  2377. }
  2378. var d = date;
  2379. return "" + d.getFullYear() + formatDigit(d.getMonth() + 1) + formatDigit(d.getDate()) + formatDigit(d.getHours()) + formatDigit(d.getMinutes()) + formatDigit(d.getSeconds());
  2380. };
  2381. self.markPlayed = function (userId, itemId, date) {
  2382. if (!userId) {
  2383. throw new Error("null userId");
  2384. }
  2385. if (!itemId) {
  2386. throw new Error("null itemId");
  2387. }
  2388. var options = {};
  2389. if (date) {
  2390. options.DatePlayed = self.getDateParamValue(date);
  2391. }
  2392. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId, options);
  2393. return self.ajax({
  2394. type: "POST",
  2395. url: url,
  2396. dataType: "json"
  2397. });
  2398. };
  2399. self.markUnplayed = function (userId, itemId) {
  2400. if (!userId) {
  2401. throw new Error("null userId");
  2402. }
  2403. if (!itemId) {
  2404. throw new Error("null itemId");
  2405. }
  2406. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  2407. return self.ajax({
  2408. type: "DELETE",
  2409. url: url,
  2410. dataType: "json"
  2411. });
  2412. };
  2413. /**
  2414. * Updates a user's favorite status for an item.
  2415. * @param {String} userId
  2416. * @param {String} itemId
  2417. * @param {Boolean} isFavorite
  2418. */
  2419. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  2420. if (!userId) {
  2421. throw new Error("null userId");
  2422. }
  2423. if (!itemId) {
  2424. throw new Error("null itemId");
  2425. }
  2426. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  2427. var method = isFavorite ? "POST" : "DELETE";
  2428. return self.ajax({
  2429. type: method,
  2430. url: url,
  2431. dataType: "json"
  2432. });
  2433. };
  2434. /**
  2435. * Updates a user's personal rating for an item
  2436. * @param {String} userId
  2437. * @param {String} itemId
  2438. * @param {Boolean} likes
  2439. */
  2440. self.updateUserItemRating = function (userId, itemId, likes) {
  2441. if (!userId) {
  2442. throw new Error("null userId");
  2443. }
  2444. if (!itemId) {
  2445. throw new Error("null itemId");
  2446. }
  2447. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  2448. likes: likes
  2449. });
  2450. return self.ajax({
  2451. type: "POST",
  2452. url: url,
  2453. dataType: "json"
  2454. });
  2455. };
  2456. /**
  2457. * Updates a user's favorite status for an item by name.
  2458. * @param {String} userId
  2459. * @param {String} name
  2460. * @param {Boolean} isFavorite
  2461. */
  2462. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  2463. if (!userId) {
  2464. throw new Error("null userId");
  2465. }
  2466. if (!name) {
  2467. throw new Error("null name");
  2468. }
  2469. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  2470. var method = isFavorite ? "POST" : "DELETE";
  2471. return self.ajax({
  2472. type: method,
  2473. url: url,
  2474. dataType: "json"
  2475. });
  2476. };
  2477. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  2478. if (!userId) {
  2479. throw new Error("null userId");
  2480. }
  2481. if (!name) {
  2482. throw new Error("null name");
  2483. }
  2484. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  2485. var method = isFavorite ? "POST" : "DELETE";
  2486. return self.ajax({
  2487. type: method,
  2488. url: url,
  2489. dataType: "json"
  2490. });
  2491. };
  2492. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2493. if (!userId) {
  2494. throw new Error("null userId");
  2495. }
  2496. if (!name) {
  2497. throw new Error("null name");
  2498. }
  2499. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2500. var method = isFavorite ? "POST" : "DELETE";
  2501. return self.ajax({
  2502. type: method,
  2503. url: url,
  2504. dataType: "json"
  2505. });
  2506. };
  2507. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2508. if (!userId) {
  2509. throw new Error("null userId");
  2510. }
  2511. if (!name) {
  2512. throw new Error("null name");
  2513. }
  2514. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2515. var method = isFavorite ? "POST" : "DELETE";
  2516. return self.ajax({
  2517. type: method,
  2518. url: url,
  2519. dataType: "json"
  2520. });
  2521. };
  2522. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2523. if (!userId) {
  2524. throw new Error("null userId");
  2525. }
  2526. if (!name) {
  2527. throw new Error("null name");
  2528. }
  2529. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2530. var method = isFavorite ? "POST" : "DELETE";
  2531. return self.ajax({
  2532. type: method,
  2533. url: url,
  2534. dataType: "json"
  2535. });
  2536. };
  2537. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2538. if (!userId) {
  2539. throw new Error("null userId");
  2540. }
  2541. if (!name) {
  2542. throw new Error("null name");
  2543. }
  2544. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2545. var method = isFavorite ? "POST" : "DELETE";
  2546. return self.ajax({
  2547. type: method,
  2548. url: url,
  2549. dataType: "json"
  2550. });
  2551. };
  2552. /**
  2553. * Updates a user's rating for an item by name.
  2554. * @param {String} userId
  2555. * @param {String} name
  2556. * @param {Boolean} likes
  2557. */
  2558. self.updateArtistRating = function (userId, name, likes) {
  2559. if (!userId) {
  2560. throw new Error("null userId");
  2561. }
  2562. if (!name) {
  2563. throw new Error("null name");
  2564. }
  2565. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2566. likes: likes
  2567. });
  2568. return self.ajax({
  2569. type: "POST",
  2570. url: url,
  2571. dataType: "json"
  2572. });
  2573. };
  2574. self.updatePersonRating = function (userId, name, likes) {
  2575. if (!userId) {
  2576. throw new Error("null userId");
  2577. }
  2578. if (!name) {
  2579. throw new Error("null name");
  2580. }
  2581. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2582. likes: likes
  2583. });
  2584. return self.ajax({
  2585. type: "POST",
  2586. url: url,
  2587. dataType: "json"
  2588. });
  2589. };
  2590. self.updateStudioRating = function (userId, name, likes) {
  2591. if (!userId) {
  2592. throw new Error("null userId");
  2593. }
  2594. if (!name) {
  2595. throw new Error("null name");
  2596. }
  2597. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2598. likes: likes
  2599. });
  2600. return self.ajax({
  2601. type: "POST",
  2602. url: url,
  2603. dataType: "json"
  2604. });
  2605. };
  2606. self.updateGenreRating = function (userId, name, likes) {
  2607. if (!userId) {
  2608. throw new Error("null userId");
  2609. }
  2610. if (!name) {
  2611. throw new Error("null name");
  2612. }
  2613. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2614. likes: likes
  2615. });
  2616. return self.ajax({
  2617. type: "POST",
  2618. url: url,
  2619. dataType: "json"
  2620. });
  2621. };
  2622. self.updateMusicGenreRating = function (userId, name, likes) {
  2623. if (!userId) {
  2624. throw new Error("null userId");
  2625. }
  2626. if (!name) {
  2627. throw new Error("null name");
  2628. }
  2629. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2630. likes: likes
  2631. });
  2632. return self.ajax({
  2633. type: "POST",
  2634. url: url,
  2635. dataType: "json"
  2636. });
  2637. };
  2638. self.updateGameGenreRating = function (userId, name, likes) {
  2639. if (!userId) {
  2640. throw new Error("null userId");
  2641. }
  2642. if (!name) {
  2643. throw new Error("null name");
  2644. }
  2645. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2646. likes: likes
  2647. });
  2648. return self.ajax({
  2649. type: "POST",
  2650. url: url,
  2651. dataType: "json"
  2652. });
  2653. };
  2654. /**
  2655. * Clears a user's rating for an item by name.
  2656. * @param {String} userId
  2657. * @param {String} name
  2658. */
  2659. self.clearArtistRating = function (userId, name) {
  2660. if (!userId) {
  2661. throw new Error("null userId");
  2662. }
  2663. if (!name) {
  2664. throw new Error("null name");
  2665. }
  2666. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2667. return self.ajax({
  2668. type: "DELETE",
  2669. url: url,
  2670. dataType: "json"
  2671. });
  2672. };
  2673. self.clearPersonRating = function (userId, name) {
  2674. if (!userId) {
  2675. throw new Error("null userId");
  2676. }
  2677. if (!name) {
  2678. throw new Error("null name");
  2679. }
  2680. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  2681. return self.ajax({
  2682. type: "DELETE",
  2683. url: url,
  2684. dataType: "json"
  2685. });
  2686. };
  2687. self.clearStudioRating = function (userId, name) {
  2688. if (!userId) {
  2689. throw new Error("null userId");
  2690. }
  2691. if (!name) {
  2692. throw new Error("null name");
  2693. }
  2694. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2695. return self.ajax({
  2696. type: "DELETE",
  2697. url: url,
  2698. dataType: "json"
  2699. });
  2700. };
  2701. self.clearGenreRating = function (userId, name) {
  2702. if (!userId) {
  2703. throw new Error("null userId");
  2704. }
  2705. if (!name) {
  2706. throw new Error("null name");
  2707. }
  2708. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2709. return self.ajax({
  2710. type: "DELETE",
  2711. url: url,
  2712. dataType: "json"
  2713. });
  2714. };
  2715. self.clearMusicGenreRating = function (userId, name) {
  2716. if (!userId) {
  2717. throw new Error("null userId");
  2718. }
  2719. if (!name) {
  2720. throw new Error("null name");
  2721. }
  2722. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2723. return self.ajax({
  2724. type: "DELETE",
  2725. url: url,
  2726. dataType: "json"
  2727. });
  2728. };
  2729. self.clearGameGenreRating = function (userId, name) {
  2730. if (!userId) {
  2731. throw new Error("null userId");
  2732. }
  2733. if (!name) {
  2734. throw new Error("null name");
  2735. }
  2736. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2737. return self.ajax({
  2738. type: "DELETE",
  2739. url: url,
  2740. dataType: "json"
  2741. });
  2742. };
  2743. self.getItemCounts = function (userId) {
  2744. var options = {};
  2745. if (userId) {
  2746. options.userId = userId;
  2747. }
  2748. var url = self.getUrl("Items/Counts", options);
  2749. return self.ajax({
  2750. type: "GET",
  2751. url: url,
  2752. dataType: "json"
  2753. });
  2754. };
  2755. /**
  2756. * Clears a user's personal rating for an item
  2757. * @param {String} userId
  2758. * @param {String} itemId
  2759. */
  2760. self.clearUserItemRating = function (userId, itemId) {
  2761. if (!userId) {
  2762. throw new Error("null userId");
  2763. }
  2764. if (!itemId) {
  2765. throw new Error("null itemId");
  2766. }
  2767. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2768. return self.ajax({
  2769. type: "DELETE",
  2770. url: url,
  2771. dataType: "json"
  2772. });
  2773. };
  2774. /**
  2775. * Reports the user has started playing something
  2776. * @param {String} userId
  2777. * @param {String} itemId
  2778. */
  2779. self.reportPlaybackStart = function (userId, itemId, canSeek, queueableMediaTypes) {
  2780. if (!userId) {
  2781. throw new Error("null userId");
  2782. }
  2783. if (!itemId) {
  2784. throw new Error("null itemId");
  2785. }
  2786. canSeek = canSeek || false;
  2787. queueableMediaTypes = queueableMediaTypes || '';
  2788. if (self.isWebSocketOpen()) {
  2789. var deferred = $.Deferred();
  2790. var msg = [itemId, canSeek, queueableMediaTypes];
  2791. self.sendWebSocketMessage("PlaybackStart", msg.join('|'));
  2792. deferred.resolveWith(null, []);
  2793. return deferred.promise();
  2794. }
  2795. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, {
  2796. CanSeek: canSeek,
  2797. QueueableMediaTypes: queueableMediaTypes
  2798. });
  2799. return self.ajax({
  2800. type: "POST",
  2801. url: url
  2802. });
  2803. };
  2804. /**
  2805. * Reports progress viewing an item
  2806. * @param {String} userId
  2807. * @param {String} itemId
  2808. */
  2809. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused, isMuted) {
  2810. if (!userId) {
  2811. throw new Error("null userId");
  2812. }
  2813. if (!itemId) {
  2814. throw new Error("null itemId");
  2815. }
  2816. if (self.isWebSocketOpen()) {
  2817. var deferred = $.Deferred();
  2818. var msgData = itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused) + "|" + (isMuted == null ? "" : isMuted);
  2819. self.sendWebSocketMessage("PlaybackProgress", msgData);
  2820. deferred.resolveWith(null, []);
  2821. return deferred.promise();
  2822. }
  2823. var params = {
  2824. isPaused: isPaused,
  2825. isMuted: isMuted
  2826. };
  2827. if (positionTicks) {
  2828. params.positionTicks = positionTicks;
  2829. }
  2830. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2831. return self.ajax({
  2832. type: "POST",
  2833. url: url
  2834. });
  2835. };
  2836. /**
  2837. * Reports a user has stopped playing an item
  2838. * @param {String} userId
  2839. * @param {String} itemId
  2840. */
  2841. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2842. if (!userId) {
  2843. throw new Error("null userId");
  2844. }
  2845. if (!itemId) {
  2846. throw new Error("null itemId");
  2847. }
  2848. if (self.isWebSocketOpen()) {
  2849. var deferred = $.Deferred();
  2850. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2851. deferred.resolveWith(null, []);
  2852. return deferred.promise();
  2853. }
  2854. var params = {
  2855. };
  2856. if (positionTicks) {
  2857. params.positionTicks = positionTicks;
  2858. }
  2859. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2860. return self.ajax({
  2861. type: "DELETE",
  2862. url: url
  2863. });
  2864. };
  2865. self.sendBrowseCommand = function (sessionId, options) {
  2866. if (!sessionId) {
  2867. throw new Error("null sessionId");
  2868. }
  2869. if (!options) {
  2870. throw new Error("null options");
  2871. }
  2872. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2873. return self.ajax({
  2874. type: "POST",
  2875. url: url
  2876. });
  2877. };
  2878. self.sendPlayCommand = function (sessionId, options) {
  2879. if (!sessionId) {
  2880. throw new Error("null sessionId");
  2881. }
  2882. if (!options) {
  2883. throw new Error("null options");
  2884. }
  2885. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2886. return self.ajax({
  2887. type: "POST",
  2888. url: url
  2889. });
  2890. };
  2891. self.sendSystemCommand = function (sessionId, command) {
  2892. if (!sessionId) {
  2893. throw new Error("null sessionId");
  2894. }
  2895. if (!command) {
  2896. throw new Error("null command");
  2897. }
  2898. var url = self.getUrl("Sessions/" + sessionId + "/System/" + command);
  2899. return self.ajax({
  2900. type: "POST",
  2901. url: url
  2902. });
  2903. };
  2904. self.sendMessageCommand = function (sessionId, options) {
  2905. if (!sessionId) {
  2906. throw new Error("null sessionId");
  2907. }
  2908. if (!options) {
  2909. throw new Error("null options");
  2910. }
  2911. var url = self.getUrl("Sessions/" + sessionId + "/Message", options);
  2912. return self.ajax({
  2913. type: "POST",
  2914. url: url
  2915. });
  2916. };
  2917. self.sendPlayStateCommand = function (sessionId, command, options) {
  2918. if (!sessionId) {
  2919. throw new Error("null sessionId");
  2920. }
  2921. if (!command) {
  2922. throw new Error("null command");
  2923. }
  2924. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2925. return self.ajax({
  2926. type: "POST",
  2927. url: url
  2928. });
  2929. };
  2930. self.createPackageReview = function (review) {
  2931. var url = self.getUrl("PackageReviews/" + review.id, review);
  2932. return self.ajax({
  2933. type: "POST",
  2934. url: url,
  2935. });
  2936. };
  2937. self.getPackageReviews = function (packageId, minRating, maxRating, limit, forceTitle) {
  2938. if (!packageId) {
  2939. throw new Error("null packageId");
  2940. }
  2941. var options = {};
  2942. if (minRating) {
  2943. options.MinRating = minRating;
  2944. }
  2945. if (maxRating) {
  2946. options.MaxRating = maxRating;
  2947. }
  2948. if (limit) {
  2949. options.Limit = limit;
  2950. }
  2951. if (forceTitle) {
  2952. options.ForceTitle = true;
  2953. }
  2954. var url = self.getUrl("PackageReviews/" + packageId, options);
  2955. return self.ajax({
  2956. type: "GET",
  2957. url: url,
  2958. dataType: "json"
  2959. });
  2960. };
  2961. };
  2962. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout, window);
  2963. /**
  2964. * Provides a friendly way to create an api client instance using information from the browser's current url
  2965. */
  2966. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2967. var loc = window.location;
  2968. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2969. };
  2970. /**
  2971. *
  2972. * Secure Hash Algorithm (SHA1)
  2973. * http://www.webtoolkit.info/
  2974. *
  2975. **/
  2976. MediaBrowser.SHA1 = function (msg) {
  2977. function rotate_left(n, s) {
  2978. var t4 = (n << s) | (n >>> (32 - s));
  2979. return t4;
  2980. }
  2981. function lsb_hex(val) {
  2982. var str = "";
  2983. var i;
  2984. var vh;
  2985. var vl;
  2986. for (i = 0; i <= 6; i += 2) {
  2987. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2988. vl = (val >>> (i * 4)) & 0x0f;
  2989. str += vh.toString(16) + vl.toString(16);
  2990. }
  2991. return str;
  2992. }
  2993. function cvt_hex(val) {
  2994. var str = "";
  2995. var i;
  2996. var v;
  2997. for (i = 7; i >= 0; i--) {
  2998. v = (val >>> (i * 4)) & 0x0f;
  2999. str += v.toString(16);
  3000. }
  3001. return str;
  3002. }
  3003. function Utf8Encode(string) {
  3004. string = string.replace(/\r\n/g, "\n");
  3005. var utftext = "";
  3006. for (var n = 0; n < string.length; n++) {
  3007. var c = string.charCodeAt(n);
  3008. if (c < 128) {
  3009. utftext += String.fromCharCode(c);
  3010. }
  3011. else if ((c > 127) && (c < 2048)) {
  3012. utftext += String.fromCharCode((c >> 6) | 192);
  3013. utftext += String.fromCharCode((c & 63) | 128);
  3014. }
  3015. else {
  3016. utftext += String.fromCharCode((c >> 12) | 224);
  3017. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  3018. utftext += String.fromCharCode((c & 63) | 128);
  3019. }
  3020. }
  3021. return utftext;
  3022. }
  3023. var blockstart;
  3024. var i, j;
  3025. var W = new Array(80);
  3026. var H0 = 0x67452301;
  3027. var H1 = 0xEFCDAB89;
  3028. var H2 = 0x98BADCFE;
  3029. var H3 = 0x10325476;
  3030. var H4 = 0xC3D2E1F0;
  3031. var A, B, C, D, E;
  3032. var temp;
  3033. msg = Utf8Encode(msg);
  3034. var msg_len = msg.length;
  3035. var word_array = new Array();
  3036. for (i = 0; i < msg_len - 3; i += 4) {
  3037. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  3038. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  3039. word_array.push(j);
  3040. }
  3041. switch (msg_len % 4) {
  3042. case 0:
  3043. i = 0x080000000;
  3044. break;
  3045. case 1:
  3046. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  3047. break;
  3048. case 2:
  3049. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  3050. break;
  3051. case 3:
  3052. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  3053. break;
  3054. }
  3055. word_array.push(i);
  3056. while ((word_array.length % 16) != 14) word_array.push(0);
  3057. word_array.push(msg_len >>> 29);
  3058. word_array.push((msg_len << 3) & 0x0ffffffff);
  3059. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  3060. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  3061. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  3062. A = H0;
  3063. B = H1;
  3064. C = H2;
  3065. D = H3;
  3066. E = H4;
  3067. for (i = 0; i <= 19; i++) {
  3068. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  3069. E = D;
  3070. D = C;
  3071. C = rotate_left(B, 30);
  3072. B = A;
  3073. A = temp;
  3074. }
  3075. for (i = 20; i <= 39; i++) {
  3076. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  3077. E = D;
  3078. D = C;
  3079. C = rotate_left(B, 30);
  3080. B = A;
  3081. A = temp;
  3082. }
  3083. for (i = 40; i <= 59; i++) {
  3084. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  3085. E = D;
  3086. D = C;
  3087. C = rotate_left(B, 30);
  3088. B = A;
  3089. A = temp;
  3090. }
  3091. for (i = 60; i <= 79; i++) {
  3092. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  3093. E = D;
  3094. D = C;
  3095. C = rotate_left(B, 30);
  3096. B = A;
  3097. A = temp;
  3098. }
  3099. H0 = (H0 + A) & 0x0ffffffff;
  3100. H1 = (H1 + B) & 0x0ffffffff;
  3101. H2 = (H2 + C) & 0x0ffffffff;
  3102. H3 = (H3 + D) & 0x0ffffffff;
  3103. H4 = (H4 + E) & 0x0ffffffff;
  3104. }
  3105. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  3106. return temp.toLowerCase();
  3107. };
  3108. (function (jQuery, window, undefined) {
  3109. "use strict";
  3110. var matched, browser;
  3111. jQuery.uaMatch = function (ua) {
  3112. ua = ua.toLowerCase();
  3113. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  3114. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  3115. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  3116. /(msie) ([\w.]+)/.exec(ua) ||
  3117. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  3118. [];
  3119. var platform_match = /(ipad)/.exec(ua) ||
  3120. /(iphone)/.exec(ua) ||
  3121. /(android)/.exec(ua) ||
  3122. [];
  3123. var browser = match[1] || "";
  3124. if (ua.indexOf("like gecko") != -1 && ua.indexOf('webkit') == -1 && ua.indexOf('opera') == -1) {
  3125. browser = "msie";
  3126. }
  3127. return {
  3128. browser: browser,
  3129. version: match[2] || "0",
  3130. platform: platform_match[0] || ""
  3131. };
  3132. };
  3133. matched = jQuery.uaMatch(window.navigator.userAgent);
  3134. browser = {};
  3135. if (matched.browser) {
  3136. browser[matched.browser] = true;
  3137. browser.version = matched.version;
  3138. }
  3139. if (matched.platform) {
  3140. browser[matched.platform] = true
  3141. }
  3142. // Chrome is Webkit, but Webkit is also Safari.
  3143. if (browser.chrome) {
  3144. browser.webkit = true;
  3145. } else if (browser.webkit) {
  3146. browser.safari = true;
  3147. }
  3148. jQuery.browser = browser;
  3149. })(jQuery, window);