ApiClient.js 112 KB

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