ApiClient.js 101 KB

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