ApiClient.js 101 KB

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