ApiClient.js 112 KB

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