ApiClient.js 91 KB

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