ApiClient.js 109 KB

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