ApiClient.js 104 KB

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