ApiClient.js 107 KB

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