ApiClient.js 114 KB

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