ApiClient.js 102 KB

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