ApiClient.js 112 KB

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