ApiClient.js 113 KB

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