ApiClient.js 100 KB

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