ApiClient.js 113 KB

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