ApiClient.js 111 KB

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