ApiClient.js 114 KB

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