ApiClient.js 114 KB

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