ApiClient.js 103 KB

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