ApiClient.js 114 KB

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