ApiClient.js 112 KB

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