ApiClient.js 105 KB

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