ApiClient.js 107 KB

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