ApiClient.js 112 KB

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