ApiClient.js 102 KB

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