ApiClient.js 101 KB

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