ApiClient.js 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532
  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, type, userId) {
  681. if (!name) {
  682. throw new Error("null name");
  683. }
  684. var options = {};
  685. if (type) {
  686. options.collectionType = type;
  687. }
  688. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  689. url += "/" + name;
  690. url = self.getUrl(url, options);
  691. return self.ajax({
  692. type: "POST",
  693. url: url
  694. });
  695. };
  696. /**
  697. * Renames a virtual folder, within either the default view or a user view
  698. * @param {String} name
  699. */
  700. self.renameVirtualFolder = function (name, newName, userId) {
  701. if (!name) {
  702. throw new Error("null name");
  703. }
  704. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  705. url += "/" + name + "/Name";
  706. url = self.getUrl(url, { newName: newName });
  707. return self.ajax({
  708. type: "POST",
  709. url: url
  710. });
  711. };
  712. /**
  713. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  714. * @param {String} name
  715. */
  716. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  717. if (!virtualFolderName) {
  718. throw new Error("null virtualFolderName");
  719. }
  720. if (!mediaPath) {
  721. throw new Error("null mediaPath");
  722. }
  723. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  724. url += "/" + virtualFolderName + "/Paths";
  725. url = self.getUrl(url, { path: mediaPath });
  726. return self.ajax({
  727. type: "POST",
  728. url: url
  729. });
  730. };
  731. /**
  732. * Removes a media path from a virtual folder, within either the default view or a user view
  733. * @param {String} name
  734. */
  735. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  736. if (!virtualFolderName) {
  737. throw new Error("null virtualFolderName");
  738. }
  739. if (!mediaPath) {
  740. throw new Error("null mediaPath");
  741. }
  742. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  743. url += "/" + virtualFolderName + "/Paths";
  744. url = self.getUrl(url, { path: mediaPath });
  745. return self.ajax({
  746. type: "DELETE",
  747. url: url
  748. });
  749. };
  750. /**
  751. * Deletes a user
  752. * @param {String} id
  753. */
  754. self.deleteUser = function (id) {
  755. if (!id) {
  756. throw new Error("null id");
  757. }
  758. var url = self.getUrl("Users/" + id);
  759. return self.ajax({
  760. type: "DELETE",
  761. url: url
  762. });
  763. };
  764. /**
  765. * Deletes a user image
  766. * @param {String} userId
  767. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  768. */
  769. self.deleteUserImage = function (userId, imageType, imageIndex) {
  770. if (!userId) {
  771. throw new Error("null userId");
  772. }
  773. if (!imageType) {
  774. throw new Error("null imageType");
  775. }
  776. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  777. if (imageIndex != null) {
  778. url += "/" + imageIndex;
  779. }
  780. return self.ajax({
  781. type: "DELETE",
  782. url: url
  783. });
  784. };
  785. self.deleteItemImage = function (itemId, imageType, imageIndex) {
  786. if (!itemId) {
  787. throw new Error("null itemId");
  788. }
  789. if (!imageType) {
  790. throw new Error("null imageType");
  791. }
  792. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  793. if (imageIndex != null) {
  794. url += "/" + imageIndex;
  795. }
  796. return self.ajax({
  797. type: "DELETE",
  798. url: url
  799. });
  800. };
  801. self.updateItemImageIndex = function (itemId, imageType, imageIndex, newIndex) {
  802. if (!itemId) {
  803. throw new Error("null itemId");
  804. }
  805. if (!imageType) {
  806. throw new Error("null imageType");
  807. }
  808. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", { newIndex: newIndex });
  809. return self.ajax({
  810. type: "POST",
  811. url: url
  812. });
  813. };
  814. self.getItemImageInfos = function (itemId) {
  815. if (!itemId) {
  816. throw new Error("null itemId");
  817. }
  818. var url = self.getUrl("Items/" + itemId + "/Images");
  819. return self.ajax({
  820. type: "GET",
  821. url: url,
  822. dataType: "json"
  823. });
  824. };
  825. self.getCriticReviews = function (itemId, options) {
  826. if (!itemId) {
  827. throw new Error("null itemId");
  828. }
  829. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  830. return self.ajax({
  831. type: "GET",
  832. url: url,
  833. dataType: "json"
  834. });
  835. };
  836. self.getSessions = function (options) {
  837. var url = self.getUrl("Sessions", options);
  838. return self.ajax({
  839. type: "GET",
  840. url: url,
  841. dataType: "json"
  842. });
  843. };
  844. /**
  845. * Uploads a user image
  846. * @param {String} userId
  847. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  848. * @param {Object} file The file from the input element
  849. */
  850. self.uploadUserImage = function (userId, imageType, file) {
  851. if (!userId) {
  852. throw new Error("null userId");
  853. }
  854. if (!imageType) {
  855. throw new Error("null imageType");
  856. }
  857. if (!file) {
  858. throw new Error("File must be an image.");
  859. }
  860. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  861. throw new Error("File must be an image.");
  862. }
  863. var deferred = $.Deferred();
  864. var reader = new FileReader();
  865. reader.onerror = function () {
  866. deferred.reject();
  867. };
  868. reader.onabort = function () {
  869. deferred.reject();
  870. };
  871. // Closure to capture the file information.
  872. reader.onload = function (e) {
  873. // Split by a comma to remove the url: prefix
  874. var data = e.target.result.split(',')[1];
  875. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  876. self.ajax({
  877. type: "POST",
  878. url: url,
  879. data: data,
  880. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  881. }).done(function (result) {
  882. deferred.resolveWith(null, [result]);
  883. }).fail(function () {
  884. deferred.reject();
  885. });
  886. };
  887. // Read in the image file as a data URL.
  888. reader.readAsDataURL(file);
  889. return deferred.promise();
  890. };
  891. self.uploadItemImage = function (itemId, imageType, file) {
  892. if (!itemId) {
  893. throw new Error("null itemId");
  894. }
  895. if (!imageType) {
  896. throw new Error("null imageType");
  897. }
  898. if (!file) {
  899. throw new Error("File must be an image.");
  900. }
  901. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  902. throw new Error("File must be an image.");
  903. }
  904. var deferred = $.Deferred();
  905. var reader = new FileReader();
  906. reader.onerror = function () {
  907. deferred.reject();
  908. };
  909. reader.onabort = function () {
  910. deferred.reject();
  911. };
  912. // Closure to capture the file information.
  913. reader.onload = function (e) {
  914. // Split by a comma to remove the url: prefix
  915. var data = e.target.result.split(',')[1];
  916. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  917. self.ajax({
  918. type: "POST",
  919. url: url,
  920. data: data,
  921. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  922. }).done(function (result) {
  923. deferred.resolveWith(null, [result]);
  924. }).fail(function () {
  925. deferred.reject();
  926. });
  927. };
  928. // Read in the image file as a data URL.
  929. reader.readAsDataURL(file);
  930. return deferred.promise();
  931. };
  932. /**
  933. * Gets the list of installed plugins on the server
  934. */
  935. self.getInstalledPlugins = function () {
  936. var url = self.getUrl("Plugins");
  937. return self.ajax({
  938. type: "GET",
  939. url: url,
  940. dataType: "json"
  941. });
  942. };
  943. /**
  944. * Gets a user by id
  945. * @param {String} id
  946. */
  947. self.getUser = function (id) {
  948. if (!id) {
  949. throw new Error("Must supply a userId");
  950. }
  951. var url = self.getUrl("Users/" + id);
  952. return self.ajax({
  953. type: "GET",
  954. url: url,
  955. dataType: "json"
  956. });
  957. };
  958. /**
  959. * Gets a studio
  960. */
  961. self.getStudio = function (name, userId) {
  962. if (!name) {
  963. throw new Error("null name");
  964. }
  965. var options = {};
  966. if (userId) {
  967. options.userId = userId;
  968. }
  969. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  970. return self.ajax({
  971. type: "GET",
  972. url: url,
  973. dataType: "json"
  974. });
  975. };
  976. /**
  977. * Gets a genre
  978. */
  979. self.getGenre = function (name, userId) {
  980. if (!name) {
  981. throw new Error("null name");
  982. }
  983. var options = {};
  984. if (userId) {
  985. options.userId = userId;
  986. }
  987. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  988. return self.ajax({
  989. type: "GET",
  990. url: url,
  991. dataType: "json"
  992. });
  993. };
  994. self.getMusicGenre = function (name, userId) {
  995. if (!name) {
  996. throw new Error("null name");
  997. }
  998. var options = {};
  999. if (userId) {
  1000. options.userId = userId;
  1001. }
  1002. var url = self.getUrl("MusicGenres/" + self.encodeName(name), options);
  1003. return self.ajax({
  1004. type: "GET",
  1005. url: url,
  1006. dataType: "json"
  1007. });
  1008. };
  1009. self.getGameGenre = function (name, userId) {
  1010. if (!name) {
  1011. throw new Error("null name");
  1012. }
  1013. var options = {};
  1014. if (userId) {
  1015. options.userId = userId;
  1016. }
  1017. var url = self.getUrl("GameGenres/" + self.encodeName(name), options);
  1018. return self.ajax({
  1019. type: "GET",
  1020. url: url,
  1021. dataType: "json"
  1022. });
  1023. };
  1024. /**
  1025. * Gets an artist
  1026. */
  1027. self.getArtist = function (name, userId) {
  1028. if (!name) {
  1029. throw new Error("null name");
  1030. }
  1031. var options = {};
  1032. if (userId) {
  1033. options.userId = userId;
  1034. }
  1035. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  1036. return self.ajax({
  1037. type: "GET",
  1038. url: url,
  1039. dataType: "json"
  1040. });
  1041. };
  1042. /**
  1043. * Gets a year
  1044. */
  1045. self.getYear = function (yea, userId) {
  1046. if (!name) {
  1047. throw new Error("null name");
  1048. }
  1049. var options = {};
  1050. if (userId) {
  1051. options.userId = userId;
  1052. }
  1053. var url = self.getUrl("Years/" + self.encodeName(name), options);
  1054. return self.ajax({
  1055. type: "GET",
  1056. url: url,
  1057. dataType: "json"
  1058. });
  1059. };
  1060. /**
  1061. * Gets a Person
  1062. */
  1063. self.getPerson = function (name, userId) {
  1064. if (!name) {
  1065. throw new Error("null name");
  1066. }
  1067. var options = {};
  1068. if (userId) {
  1069. options.userId = userId;
  1070. }
  1071. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  1072. return self.ajax({
  1073. type: "GET",
  1074. url: url,
  1075. dataType: "json"
  1076. });
  1077. };
  1078. self.getPublicUsers = function () {
  1079. var url = self.getUrl("users/public");
  1080. return self.ajax({
  1081. type: "GET",
  1082. url: url,
  1083. dataType: "json"
  1084. });
  1085. };
  1086. /**
  1087. * Gets all users from the server
  1088. */
  1089. self.getUsers = function (options) {
  1090. var url = self.getUrl("users", options || {});
  1091. return self.ajax({
  1092. type: "GET",
  1093. url: url,
  1094. dataType: "json"
  1095. });
  1096. };
  1097. /**
  1098. * Gets all available parental ratings from the server
  1099. */
  1100. self.getParentalRatings = function () {
  1101. var url = self.getUrl("Localization/ParentalRatings");
  1102. return self.ajax({
  1103. type: "GET",
  1104. url: url,
  1105. dataType: "json"
  1106. });
  1107. };
  1108. /**
  1109. * Gets a list of all available conrete BaseItem types from the server
  1110. */
  1111. self.getItemTypes = function (options) {
  1112. var url = self.getUrl("Library/ItemTypes", options);
  1113. return self.ajax({
  1114. type: "GET",
  1115. url: url,
  1116. dataType: "json"
  1117. });
  1118. };
  1119. /**
  1120. * Constructs a url for a user image
  1121. * @param {String} userId
  1122. * @param {Object} options
  1123. * Options supports the following properties:
  1124. * width - download the image at a fixed width
  1125. * height - download the image at a fixed height
  1126. * maxWidth - download the image at a maxWidth
  1127. * maxHeight - download the image at a maxHeight
  1128. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1129. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1130. */
  1131. self.getUserImageUrl = function (userId, options) {
  1132. if (!userId) {
  1133. throw new Error("null userId");
  1134. }
  1135. options = options || {
  1136. };
  1137. var url = "Users/" + userId + "/Images/" + options.type;
  1138. if (options.index != null) {
  1139. url += "/" + options.index;
  1140. }
  1141. // Don't put these on the query string
  1142. delete options.type;
  1143. delete options.index;
  1144. return self.getUrl(url, options);
  1145. };
  1146. /**
  1147. * Constructs a url for a person image
  1148. * @param {String} name
  1149. * @param {Object} options
  1150. * Options supports the following properties:
  1151. * width - download the image at a fixed width
  1152. * height - download the image at a fixed height
  1153. * maxWidth - download the image at a maxWidth
  1154. * maxHeight - download the image at a maxHeight
  1155. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1156. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1157. */
  1158. self.getPersonImageUrl = function (name, options) {
  1159. if (!name) {
  1160. throw new Error("null name");
  1161. }
  1162. options = options || {
  1163. };
  1164. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1165. if (options.index != null) {
  1166. url += "/" + options.index;
  1167. }
  1168. // Don't put these on the query string
  1169. delete options.type;
  1170. delete options.index;
  1171. return self.getUrl(url, options);
  1172. };
  1173. /**
  1174. * Constructs a url for a year image
  1175. * @param {String} year
  1176. * @param {Object} options
  1177. * Options supports the following properties:
  1178. * width - download the image at a fixed width
  1179. * height - download the image at a fixed height
  1180. * maxWidth - download the image at a maxWidth
  1181. * maxHeight - download the image at a maxHeight
  1182. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1183. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1184. */
  1185. self.getYearImageUrl = function (year, options) {
  1186. if (!year) {
  1187. throw new Error("null year");
  1188. }
  1189. options = options || {
  1190. };
  1191. var url = "Years/" + year + "/Images/" + options.type;
  1192. if (options.index != null) {
  1193. url += "/" + options.index;
  1194. }
  1195. // Don't put these on the query string
  1196. delete options.type;
  1197. delete options.index;
  1198. return self.getUrl(url, options);
  1199. };
  1200. /**
  1201. * Constructs a url for a genre image
  1202. * @param {String} name
  1203. * @param {Object} options
  1204. * Options supports the following properties:
  1205. * width - download the image at a fixed width
  1206. * height - download the image at a fixed height
  1207. * maxWidth - download the image at a maxWidth
  1208. * maxHeight - download the image at a maxHeight
  1209. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1210. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1211. */
  1212. self.getGenreImageUrl = function (name, options) {
  1213. if (!name) {
  1214. throw new Error("null name");
  1215. }
  1216. options = options || {
  1217. };
  1218. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1219. if (options.index != null) {
  1220. url += "/" + options.index;
  1221. }
  1222. // Don't put these on the query string
  1223. delete options.type;
  1224. delete options.index;
  1225. return self.getUrl(url, options);
  1226. };
  1227. /**
  1228. * Constructs a url for a genre image
  1229. * @param {String} name
  1230. * @param {Object} options
  1231. * Options supports the following properties:
  1232. * width - download the image at a fixed width
  1233. * height - download the image at a fixed height
  1234. * maxWidth - download the image at a maxWidth
  1235. * maxHeight - download the image at a maxHeight
  1236. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1237. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1238. */
  1239. self.getMusicGenreImageUrl = function (name, options) {
  1240. if (!name) {
  1241. throw new Error("null name");
  1242. }
  1243. options = options || {
  1244. };
  1245. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1246. if (options.index != null) {
  1247. url += "/" + options.index;
  1248. }
  1249. // Don't put these on the query string
  1250. delete options.type;
  1251. delete options.index;
  1252. return self.getUrl(url, options);
  1253. };
  1254. /**
  1255. * Constructs a url for a genre image
  1256. * @param {String} name
  1257. * @param {Object} options
  1258. * Options supports the following properties:
  1259. * width - download the image at a fixed width
  1260. * height - download the image at a fixed height
  1261. * maxWidth - download the image at a maxWidth
  1262. * maxHeight - download the image at a maxHeight
  1263. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1264. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1265. */
  1266. self.getGameGenreImageUrl = function (name, options) {
  1267. if (!name) {
  1268. throw new Error("null name");
  1269. }
  1270. options = options || {
  1271. };
  1272. var url = "GameGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1273. if (options.index != null) {
  1274. url += "/" + options.index;
  1275. }
  1276. // Don't put these on the query string
  1277. delete options.type;
  1278. delete options.index;
  1279. return self.getUrl(url, options);
  1280. };
  1281. /**
  1282. * Constructs a url for a artist image
  1283. * @param {String} name
  1284. * @param {Object} options
  1285. * Options supports the following properties:
  1286. * width - download the image at a fixed width
  1287. * height - download the image at a fixed height
  1288. * maxWidth - download the image at a maxWidth
  1289. * maxHeight - download the image at a maxHeight
  1290. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1291. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1292. */
  1293. self.getArtistImageUrl = function (name, options) {
  1294. if (!name) {
  1295. throw new Error("null name");
  1296. }
  1297. options = options || {
  1298. };
  1299. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1300. if (options.index != null) {
  1301. url += "/" + options.index;
  1302. }
  1303. // Don't put these on the query string
  1304. delete options.type;
  1305. delete options.index;
  1306. return self.getUrl(url, options);
  1307. };
  1308. /**
  1309. * Constructs a url for a studio image
  1310. * @param {String} name
  1311. * @param {Object} options
  1312. * Options supports the following properties:
  1313. * width - download the image at a fixed width
  1314. * height - download the image at a fixed height
  1315. * maxWidth - download the image at a maxWidth
  1316. * maxHeight - download the image at a maxHeight
  1317. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1318. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1319. */
  1320. self.getStudioImageUrl = function (name, options) {
  1321. if (!name) {
  1322. throw new Error("null name");
  1323. }
  1324. options = options || {
  1325. };
  1326. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1327. if (options.index != null) {
  1328. url += "/" + options.index;
  1329. }
  1330. // Don't put these on the query string
  1331. delete options.type;
  1332. delete options.index;
  1333. return self.getUrl(url, options);
  1334. };
  1335. /**
  1336. * Constructs a url for an item image
  1337. * @param {String} itemId
  1338. * @param {Object} options
  1339. * Options supports the following properties:
  1340. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1341. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1342. * width - download the image at a fixed width
  1343. * height - download the image at a fixed height
  1344. * maxWidth - download the image at a maxWidth
  1345. * maxHeight - download the image at a maxHeight
  1346. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1347. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1348. */
  1349. self.getImageUrl = function (itemId, options) {
  1350. if (!itemId) {
  1351. throw new Error("itemId cannot be empty");
  1352. }
  1353. options = options || {
  1354. };
  1355. var url = "Items/" + itemId + "/Images/" + options.type;
  1356. if (options.index != null) {
  1357. url += "/" + options.index;
  1358. }
  1359. // Don't put these on the query string
  1360. delete options.type;
  1361. delete options.index;
  1362. return self.getUrl(url, options);
  1363. };
  1364. /**
  1365. * Constructs a url for an item logo image
  1366. * If the item doesn't have a logo, it will inherit a logo from a parent
  1367. * @param {Object} item A BaseItem
  1368. * @param {Object} options
  1369. * Options supports the following properties:
  1370. * width - download the image at a fixed width
  1371. * height - download the image at a fixed height
  1372. * maxWidth - download the image at a maxWidth
  1373. * maxHeight - download the image at a maxHeight
  1374. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1375. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1376. */
  1377. self.getLogoImageUrl = function (item, options) {
  1378. if (!item) {
  1379. throw new Error("null item");
  1380. }
  1381. options = options || {
  1382. };
  1383. options.imageType = "logo";
  1384. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1385. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1386. };
  1387. /**
  1388. * Constructs an array of backdrop image url's for an item
  1389. * If the item doesn't have any backdrops, it will inherit them from a parent
  1390. * @param {Object} item A BaseItem
  1391. * @param {Object} options
  1392. * Options supports the following properties:
  1393. * width - download the image at a fixed width
  1394. * height - download the image at a fixed height
  1395. * maxWidth - download the image at a maxWidth
  1396. * maxHeight - download the image at a maxHeight
  1397. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1398. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1399. */
  1400. self.getBackdropImageUrl = function (item, options) {
  1401. if (!item) {
  1402. throw new Error("null item");
  1403. }
  1404. options = options || {
  1405. };
  1406. options.imageType = "backdrop";
  1407. var backdropItemId;
  1408. var backdropCount;
  1409. if (!item.BackdropCount) {
  1410. backdropItemId = item.ParentBackdropItemId;
  1411. backdropCount = item.ParentBackdropCount || 0;
  1412. } else {
  1413. backdropItemId = item.Id;
  1414. backdropCount = item.BackdropCount;
  1415. }
  1416. if (!backdropItemId) {
  1417. return [];
  1418. }
  1419. var files = [];
  1420. for (var i = 0; i < backdropCount; i++) {
  1421. options.imageIndex = i;
  1422. files[i] = self.getImageUrl(backdropItemId, options);
  1423. }
  1424. return files;
  1425. };
  1426. /**
  1427. * Authenticates a user
  1428. * @param {String} name
  1429. * @param {String} password
  1430. */
  1431. self.authenticateUserByName = function (name, password) {
  1432. if (!name) {
  1433. throw new Error("null name");
  1434. }
  1435. var url = self.getUrl("Users/" + name + "/authenticatebyname");
  1436. var postData = {
  1437. password: MediaBrowser.SHA1(password || "")
  1438. };
  1439. return self.ajax({
  1440. type: "POST",
  1441. url: url,
  1442. data: JSON.stringify(postData),
  1443. dataType: "json",
  1444. contentType: "application/json"
  1445. });
  1446. };
  1447. /**
  1448. * Authenticates a user
  1449. * @param {String} userId
  1450. * @param {String} password
  1451. */
  1452. self.authenticateUser = function (userId, password) {
  1453. if (!userId) {
  1454. throw new Error("null userId");
  1455. }
  1456. var url = self.getUrl("Users/" + userId + "/authenticate");
  1457. var postData = {
  1458. password: MediaBrowser.SHA1(password || "")
  1459. };
  1460. return self.ajax({
  1461. type: "POST",
  1462. url: url,
  1463. data: JSON.stringify(postData),
  1464. dataType: "json",
  1465. contentType: "application/json"
  1466. });
  1467. };
  1468. /**
  1469. * Updates a user's password
  1470. * @param {String} userId
  1471. * @param {String} currentPassword
  1472. * @param {String} newPassword
  1473. */
  1474. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1475. if (!userId) {
  1476. throw new Error("null userId");
  1477. }
  1478. var url = self.getUrl("Users/" + userId + "/Password");
  1479. var postData = {
  1480. };
  1481. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1482. if (newPassword) {
  1483. postData.newPassword = newPassword;
  1484. }
  1485. return self.ajax({
  1486. type: "POST",
  1487. url: url,
  1488. data: postData
  1489. });
  1490. };
  1491. /**
  1492. * Resets a user's password
  1493. * @param {String} userId
  1494. */
  1495. self.resetUserPassword = function (userId) {
  1496. if (!userId) {
  1497. throw new Error("null userId");
  1498. }
  1499. var url = self.getUrl("Users/" + userId + "/Password");
  1500. var postData = {
  1501. };
  1502. postData.resetPassword = true;
  1503. return self.ajax({
  1504. type: "POST",
  1505. url: url,
  1506. data: postData
  1507. });
  1508. };
  1509. /**
  1510. * Updates the server's configuration
  1511. * @param {Object} configuration
  1512. */
  1513. self.updateServerConfiguration = function (configuration) {
  1514. if (!configuration) {
  1515. throw new Error("null configuration");
  1516. }
  1517. var url = self.getUrl("System/Configuration");
  1518. return self.ajax({
  1519. type: "POST",
  1520. url: url,
  1521. data: JSON.stringify(configuration),
  1522. contentType: "application/json"
  1523. });
  1524. };
  1525. self.updateItem = function (item) {
  1526. if (!item) {
  1527. throw new Error("null item");
  1528. }
  1529. var url = self.getUrl("Items/" + item.Id);
  1530. return self.ajax({
  1531. type: "POST",
  1532. url: url,
  1533. data: JSON.stringify(item),
  1534. contentType: "application/json"
  1535. });
  1536. };
  1537. self.updateArtist = function (item) {
  1538. if (!item) {
  1539. throw new Error("null item");
  1540. }
  1541. var url = self.getUrl("Artists/" + self.encodeName(item.Name));
  1542. return self.ajax({
  1543. type: "POST",
  1544. url: url,
  1545. data: JSON.stringify(item),
  1546. contentType: "application/json"
  1547. });
  1548. };
  1549. self.updatePerson = function (item) {
  1550. if (!item) {
  1551. throw new Error("null item");
  1552. }
  1553. var url = self.getUrl("Persons/" + self.encodeName(item.Name));
  1554. return self.ajax({
  1555. type: "POST",
  1556. url: url,
  1557. data: JSON.stringify(item),
  1558. contentType: "application/json"
  1559. });
  1560. };
  1561. self.updateStudio = function (item) {
  1562. if (!item) {
  1563. throw new Error("null item");
  1564. }
  1565. var url = self.getUrl("Studios/" + self.encodeName(item.Name));
  1566. return self.ajax({
  1567. type: "POST",
  1568. url: url,
  1569. data: JSON.stringify(item),
  1570. contentType: "application/json"
  1571. });
  1572. };
  1573. self.updateGenre = function (item) {
  1574. if (!item) {
  1575. throw new Error("null item");
  1576. }
  1577. var url = self.getUrl("Genres/" + self.encodeName(item.Name));
  1578. return self.ajax({
  1579. type: "POST",
  1580. url: url,
  1581. data: JSON.stringify(item),
  1582. contentType: "application/json"
  1583. });
  1584. };
  1585. self.updateMusicGenre = function (item) {
  1586. if (!item) {
  1587. throw new Error("null item");
  1588. }
  1589. var url = self.getUrl("MusicGenres/" + self.encodeName(item.Name));
  1590. return self.ajax({
  1591. type: "POST",
  1592. url: url,
  1593. data: JSON.stringify(item),
  1594. contentType: "application/json"
  1595. });
  1596. };
  1597. self.updateGameGenre = function (item) {
  1598. if (!item) {
  1599. throw new Error("null item");
  1600. }
  1601. var url = self.getUrl("GameGenres/" + self.encodeName(item.Name));
  1602. return self.ajax({
  1603. type: "POST",
  1604. url: url,
  1605. data: JSON.stringify(item),
  1606. contentType: "application/json"
  1607. });
  1608. };
  1609. /**
  1610. * Updates plugin security info
  1611. */
  1612. self.updatePluginSecurityInfo = function (info) {
  1613. var url = self.getUrl("Plugins/SecurityInfo");
  1614. return self.ajax({
  1615. type: "POST",
  1616. url: url,
  1617. data: JSON.stringify(info),
  1618. contentType: "application/json"
  1619. });
  1620. };
  1621. /**
  1622. * Creates a user
  1623. * @param {Object} user
  1624. */
  1625. self.createUser = function (user) {
  1626. if (!user) {
  1627. throw new Error("null user");
  1628. }
  1629. var url = self.getUrl("Users");
  1630. return self.ajax({
  1631. type: "POST",
  1632. url: url,
  1633. data: JSON.stringify(user),
  1634. dataType: "json",
  1635. contentType: "application/json"
  1636. });
  1637. };
  1638. /**
  1639. * Updates a user
  1640. * @param {Object} user
  1641. */
  1642. self.updateUser = function (user) {
  1643. if (!user) {
  1644. throw new Error("null user");
  1645. }
  1646. var url = self.getUrl("Users/" + user.Id);
  1647. return self.ajax({
  1648. type: "POST",
  1649. url: url,
  1650. data: JSON.stringify(user),
  1651. contentType: "application/json"
  1652. });
  1653. };
  1654. /**
  1655. * Updates the Triggers for a ScheduledTask
  1656. * @param {String} id
  1657. * @param {Object} triggers
  1658. */
  1659. self.updateScheduledTaskTriggers = function (id, triggers) {
  1660. if (!id) {
  1661. throw new Error("null id");
  1662. }
  1663. if (!triggers) {
  1664. throw new Error("null triggers");
  1665. }
  1666. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1667. return self.ajax({
  1668. type: "POST",
  1669. url: url,
  1670. data: JSON.stringify(triggers),
  1671. contentType: "application/json"
  1672. });
  1673. };
  1674. /**
  1675. * Updates a plugin's configuration
  1676. * @param {String} Id
  1677. * @param {Object} configuration
  1678. */
  1679. self.updatePluginConfiguration = function (id, configuration) {
  1680. if (!id) {
  1681. throw new Error("null Id");
  1682. }
  1683. if (!configuration) {
  1684. throw new Error("null configuration");
  1685. }
  1686. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1687. return self.ajax({
  1688. type: "POST",
  1689. url: url,
  1690. data: JSON.stringify(configuration),
  1691. contentType: "application/json"
  1692. });
  1693. };
  1694. /**
  1695. * Gets items based on a query, typically for children of a folder
  1696. * @param {String} userId
  1697. * @param {Object} options
  1698. * Options accepts the following properties:
  1699. * itemId - Localize the search to a specific folder (root if omitted)
  1700. * startIndex - Use for paging
  1701. * limit - Use to limit results to a certain number of items
  1702. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1703. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1704. * sortOrder - ascending/descending
  1705. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1706. * index - the name of the dynamic, localized index function
  1707. * dynamicSortBy - the name of the dynamic localized sort function
  1708. * recursive - Whether or not the query should be recursive
  1709. * searchTerm - search term to use as a filter
  1710. */
  1711. self.getItems = function (userId, options) {
  1712. if (!userId) {
  1713. throw new Error("null userId");
  1714. }
  1715. var url = self.getUrl("Users/" + userId + "/Items", options);
  1716. return self.ajax({
  1717. type: "GET",
  1718. url: url,
  1719. dataType: "json"
  1720. });
  1721. };
  1722. /**
  1723. Gets artists from an item
  1724. */
  1725. self.getArtists = function (userId, options) {
  1726. if (!userId) {
  1727. throw new Error("null userId");
  1728. }
  1729. options = options || {};
  1730. options.userId = userId;
  1731. var url = self.getUrl("Artists", options);
  1732. return self.ajax({
  1733. type: "GET",
  1734. url: url,
  1735. dataType: "json"
  1736. });
  1737. };
  1738. /**
  1739. Gets genres from an item
  1740. */
  1741. self.getGenres = function (userId, options) {
  1742. if (!userId) {
  1743. throw new Error("null userId");
  1744. }
  1745. options = options || {};
  1746. options.userId = userId;
  1747. var url = self.getUrl("Genres", options);
  1748. return self.ajax({
  1749. type: "GET",
  1750. url: url,
  1751. dataType: "json"
  1752. });
  1753. };
  1754. self.getMusicGenres = function (userId, options) {
  1755. if (!userId) {
  1756. throw new Error("null userId");
  1757. }
  1758. options = options || {};
  1759. options.userId = userId;
  1760. var url = self.getUrl("MusicGenres", options);
  1761. return self.ajax({
  1762. type: "GET",
  1763. url: url,
  1764. dataType: "json"
  1765. });
  1766. };
  1767. self.getGameGenres = function (userId, options) {
  1768. if (!userId) {
  1769. throw new Error("null userId");
  1770. }
  1771. options = options || {};
  1772. options.userId = userId;
  1773. var url = self.getUrl("GameGenres", options);
  1774. return self.ajax({
  1775. type: "GET",
  1776. url: url,
  1777. dataType: "json"
  1778. });
  1779. };
  1780. /**
  1781. Gets people from an item
  1782. */
  1783. self.getPeople = function (userId, options) {
  1784. if (!userId) {
  1785. throw new Error("null userId");
  1786. }
  1787. options = options || {};
  1788. options.userId = userId;
  1789. var url = self.getUrl("Persons", options);
  1790. return self.ajax({
  1791. type: "GET",
  1792. url: url,
  1793. dataType: "json"
  1794. });
  1795. };
  1796. /**
  1797. Gets studios from an item
  1798. */
  1799. self.getStudios = function (userId, options) {
  1800. if (!userId) {
  1801. throw new Error("null userId");
  1802. }
  1803. options = options || {};
  1804. options.userId = userId;
  1805. var url = self.getUrl("Studios", options);
  1806. return self.ajax({
  1807. type: "GET",
  1808. url: url,
  1809. dataType: "json"
  1810. });
  1811. };
  1812. /**
  1813. * Gets local trailers for an item
  1814. */
  1815. self.getLocalTrailers = function (userId, itemId) {
  1816. if (!userId) {
  1817. throw new Error("null userId");
  1818. }
  1819. if (!itemId) {
  1820. throw new Error("null itemId");
  1821. }
  1822. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1823. return self.ajax({
  1824. type: "GET",
  1825. url: url,
  1826. dataType: "json"
  1827. });
  1828. };
  1829. self.getAdditionalVideoParts = function (userId, itemId) {
  1830. if (!itemId) {
  1831. throw new Error("null itemId");
  1832. }
  1833. var options = {};
  1834. if (userId) {
  1835. options.userId = userId;
  1836. }
  1837. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  1838. return self.ajax({
  1839. type: "GET",
  1840. url: url,
  1841. dataType: "json"
  1842. });
  1843. };
  1844. /**
  1845. * Gets theme songs for an item
  1846. */
  1847. self.getThemeSongs = function (userId, itemId) {
  1848. if (!itemId) {
  1849. throw new Error("null itemId");
  1850. }
  1851. var options = {};
  1852. if (userId) {
  1853. options.userId = userId;
  1854. }
  1855. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  1856. return self.ajax({
  1857. type: "GET",
  1858. url: url,
  1859. dataType: "json"
  1860. });
  1861. };
  1862. self.getThemeVideos = function (userId, itemId) {
  1863. if (!itemId) {
  1864. throw new Error("null itemId");
  1865. }
  1866. var options = {};
  1867. if (userId) {
  1868. options.userId = userId;
  1869. }
  1870. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  1871. return self.ajax({
  1872. type: "GET",
  1873. url: url,
  1874. dataType: "json"
  1875. });
  1876. };
  1877. self.getSearchHints = function (options) {
  1878. var url = self.getUrl("Search/Hints", options);
  1879. return self.ajax({
  1880. type: "GET",
  1881. url: url,
  1882. dataType: "json"
  1883. });
  1884. };
  1885. /**
  1886. * Gets special features for an item
  1887. */
  1888. self.getSpecialFeatures = function (userId, itemId) {
  1889. if (!userId) {
  1890. throw new Error("null userId");
  1891. }
  1892. if (!itemId) {
  1893. throw new Error("null itemId");
  1894. }
  1895. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1896. return self.ajax({
  1897. type: "GET",
  1898. url: url,
  1899. dataType: "json"
  1900. });
  1901. };
  1902. /**
  1903. * Marks an item as played or unplayed
  1904. * This should not be used to update playstate following playback.
  1905. * There are separate playstate check-in methods for that. This should be used for a
  1906. * separate option to reset playstate.
  1907. * @param {String} userId
  1908. * @param {String} itemId
  1909. * @param {Boolean} wasPlayed
  1910. */
  1911. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1912. if (!userId) {
  1913. throw new Error("null userId");
  1914. }
  1915. if (!itemId) {
  1916. throw new Error("null itemId");
  1917. }
  1918. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1919. var method = wasPlayed ? "POST" : "DELETE";
  1920. return self.ajax({
  1921. type: method,
  1922. url: url
  1923. });
  1924. };
  1925. /**
  1926. * Updates a user's favorite status for an item.
  1927. * @param {String} userId
  1928. * @param {String} itemId
  1929. * @param {Boolean} isFavorite
  1930. */
  1931. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1932. if (!userId) {
  1933. throw new Error("null userId");
  1934. }
  1935. if (!itemId) {
  1936. throw new Error("null itemId");
  1937. }
  1938. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1939. var method = isFavorite ? "POST" : "DELETE";
  1940. return self.ajax({
  1941. type: method,
  1942. url: url
  1943. });
  1944. };
  1945. /**
  1946. * Updates a user's personal rating for an item
  1947. * @param {String} userId
  1948. * @param {String} itemId
  1949. * @param {Boolean} likes
  1950. */
  1951. self.updateUserItemRating = function (userId, itemId, likes) {
  1952. if (!userId) {
  1953. throw new Error("null userId");
  1954. }
  1955. if (!itemId) {
  1956. throw new Error("null itemId");
  1957. }
  1958. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1959. likes: likes
  1960. });
  1961. return self.ajax({
  1962. type: "POST",
  1963. url: url
  1964. });
  1965. };
  1966. /**
  1967. * Updates a user's favorite status for an item by name.
  1968. * @param {String} userId
  1969. * @param {String} name
  1970. * @param {Boolean} isFavorite
  1971. */
  1972. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1973. if (!userId) {
  1974. throw new Error("null userId");
  1975. }
  1976. if (!name) {
  1977. throw new Error("null name");
  1978. }
  1979. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1980. var method = isFavorite ? "POST" : "DELETE";
  1981. return self.ajax({
  1982. type: method,
  1983. url: url
  1984. });
  1985. };
  1986. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1987. if (!userId) {
  1988. throw new Error("null userId");
  1989. }
  1990. if (!name) {
  1991. throw new Error("null name");
  1992. }
  1993. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1994. var method = isFavorite ? "POST" : "DELETE";
  1995. return self.ajax({
  1996. type: method,
  1997. url: url
  1998. });
  1999. };
  2000. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2001. if (!userId) {
  2002. throw new Error("null userId");
  2003. }
  2004. if (!name) {
  2005. throw new Error("null name");
  2006. }
  2007. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2008. var method = isFavorite ? "POST" : "DELETE";
  2009. return self.ajax({
  2010. type: method,
  2011. url: url
  2012. });
  2013. };
  2014. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2015. if (!userId) {
  2016. throw new Error("null userId");
  2017. }
  2018. if (!name) {
  2019. throw new Error("null name");
  2020. }
  2021. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2022. var method = isFavorite ? "POST" : "DELETE";
  2023. return self.ajax({
  2024. type: method,
  2025. url: url
  2026. });
  2027. };
  2028. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2029. if (!userId) {
  2030. throw new Error("null userId");
  2031. }
  2032. if (!name) {
  2033. throw new Error("null name");
  2034. }
  2035. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2036. var method = isFavorite ? "POST" : "DELETE";
  2037. return self.ajax({
  2038. type: method,
  2039. url: url
  2040. });
  2041. };
  2042. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2043. if (!userId) {
  2044. throw new Error("null userId");
  2045. }
  2046. if (!name) {
  2047. throw new Error("null name");
  2048. }
  2049. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2050. var method = isFavorite ? "POST" : "DELETE";
  2051. return self.ajax({
  2052. type: method,
  2053. url: url
  2054. });
  2055. };
  2056. /**
  2057. * Updates a user's rating for an item by name.
  2058. * @param {String} userId
  2059. * @param {String} name
  2060. * @param {Boolean} likes
  2061. */
  2062. self.updateArtistRating = function (userId, name, likes) {
  2063. if (!userId) {
  2064. throw new Error("null userId");
  2065. }
  2066. if (!name) {
  2067. throw new Error("null name");
  2068. }
  2069. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2070. likes: likes
  2071. });
  2072. return self.ajax({
  2073. type: "POST",
  2074. url: url
  2075. });
  2076. };
  2077. self.updatePersonRating = function (userId, name, likes) {
  2078. if (!userId) {
  2079. throw new Error("null userId");
  2080. }
  2081. if (!name) {
  2082. throw new Error("null name");
  2083. }
  2084. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2085. likes: likes
  2086. });
  2087. return self.ajax({
  2088. type: "POST",
  2089. url: url
  2090. });
  2091. };
  2092. self.updateStudioRating = function (userId, name, likes) {
  2093. if (!userId) {
  2094. throw new Error("null userId");
  2095. }
  2096. if (!name) {
  2097. throw new Error("null name");
  2098. }
  2099. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2100. likes: likes
  2101. });
  2102. return self.ajax({
  2103. type: "POST",
  2104. url: url
  2105. });
  2106. };
  2107. self.updateGenreRating = function (userId, name, likes) {
  2108. if (!userId) {
  2109. throw new Error("null userId");
  2110. }
  2111. if (!name) {
  2112. throw new Error("null name");
  2113. }
  2114. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2115. likes: likes
  2116. });
  2117. return self.ajax({
  2118. type: "POST",
  2119. url: url
  2120. });
  2121. };
  2122. self.updateMusicGenreRating = function (userId, name, likes) {
  2123. if (!userId) {
  2124. throw new Error("null userId");
  2125. }
  2126. if (!name) {
  2127. throw new Error("null name");
  2128. }
  2129. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2130. likes: likes
  2131. });
  2132. return self.ajax({
  2133. type: "POST",
  2134. url: url
  2135. });
  2136. };
  2137. self.updateGameGenreRating = function (userId, name, likes) {
  2138. if (!userId) {
  2139. throw new Error("null userId");
  2140. }
  2141. if (!name) {
  2142. throw new Error("null name");
  2143. }
  2144. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2145. likes: likes
  2146. });
  2147. return self.ajax({
  2148. type: "POST",
  2149. url: url
  2150. });
  2151. };
  2152. /**
  2153. * Clears a user's rating for an item by name.
  2154. * @param {String} userId
  2155. * @param {String} name
  2156. */
  2157. self.clearArtistRating = function (userId, name) {
  2158. if (!userId) {
  2159. throw new Error("null userId");
  2160. }
  2161. if (!name) {
  2162. throw new Error("null name");
  2163. }
  2164. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2165. return self.ajax({
  2166. type: "DELETE",
  2167. url: url
  2168. });
  2169. };
  2170. self.clearPersonRating = function (userId, name) {
  2171. if (!userId) {
  2172. throw new Error("null userId");
  2173. }
  2174. if (!name) {
  2175. throw new Error("null name");
  2176. }
  2177. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  2178. return self.ajax({
  2179. type: "DELETE",
  2180. url: url
  2181. });
  2182. };
  2183. self.clearStudioRating = function (userId, name) {
  2184. if (!userId) {
  2185. throw new Error("null userId");
  2186. }
  2187. if (!name) {
  2188. throw new Error("null name");
  2189. }
  2190. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2191. return self.ajax({
  2192. type: "DELETE",
  2193. url: url
  2194. });
  2195. };
  2196. self.clearGenreRating = function (userId, name) {
  2197. if (!userId) {
  2198. throw new Error("null userId");
  2199. }
  2200. if (!name) {
  2201. throw new Error("null name");
  2202. }
  2203. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2204. return self.ajax({
  2205. type: "DELETE",
  2206. url: url
  2207. });
  2208. };
  2209. self.clearMusicGenreRating = function (userId, name) {
  2210. if (!userId) {
  2211. throw new Error("null userId");
  2212. }
  2213. if (!name) {
  2214. throw new Error("null name");
  2215. }
  2216. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2217. return self.ajax({
  2218. type: "DELETE",
  2219. url: url
  2220. });
  2221. };
  2222. self.clearGameGenreRating = function (userId, name) {
  2223. if (!userId) {
  2224. throw new Error("null userId");
  2225. }
  2226. if (!name) {
  2227. throw new Error("null name");
  2228. }
  2229. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2230. return self.ajax({
  2231. type: "DELETE",
  2232. url: url
  2233. });
  2234. };
  2235. self.getItemCounts = function (userId) {
  2236. var options = {};
  2237. if (userId) {
  2238. options.userId = userId;
  2239. }
  2240. var url = self.getUrl("Items/Counts", options);
  2241. return self.ajax({
  2242. type: "GET",
  2243. url: url,
  2244. dataType: "json"
  2245. });
  2246. };
  2247. /**
  2248. Gets a variety of item counts that a person appears in
  2249. */
  2250. self.getPersonItemCounts = function (userId, name) {
  2251. if (!userId) {
  2252. throw new Error("null userId");
  2253. }
  2254. if (!name) {
  2255. throw new Error("null name");
  2256. }
  2257. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  2258. userId: userId
  2259. });
  2260. return self.ajax({
  2261. type: "GET",
  2262. url: url,
  2263. dataType: "json"
  2264. });
  2265. };
  2266. /**
  2267. Gets a variety of item counts that a genre appears in
  2268. */
  2269. self.getGenreItemCounts = function (userId, name) {
  2270. if (!userId) {
  2271. throw new Error("null userId");
  2272. }
  2273. if (!name) {
  2274. throw new Error("null name");
  2275. }
  2276. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  2277. userId: userId
  2278. });
  2279. return self.ajax({
  2280. type: "GET",
  2281. url: url,
  2282. dataType: "json"
  2283. });
  2284. };
  2285. self.getMusicGenreItemCounts = function (userId, name) {
  2286. if (!userId) {
  2287. throw new Error("null userId");
  2288. }
  2289. if (!name) {
  2290. throw new Error("null name");
  2291. }
  2292. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Counts", {
  2293. userId: userId
  2294. });
  2295. return self.ajax({
  2296. type: "GET",
  2297. url: url,
  2298. dataType: "json"
  2299. });
  2300. };
  2301. self.getGameGenreItemCounts = function (userId, name) {
  2302. if (!userId) {
  2303. throw new Error("null userId");
  2304. }
  2305. if (!name) {
  2306. throw new Error("null name");
  2307. }
  2308. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Counts", {
  2309. userId: userId
  2310. });
  2311. return self.ajax({
  2312. type: "GET",
  2313. url: url,
  2314. dataType: "json"
  2315. });
  2316. };
  2317. /**
  2318. Gets a variety of item counts that an artist appears in
  2319. */
  2320. self.getArtistItemCounts = function (userId, name) {
  2321. if (!userId) {
  2322. throw new Error("null userId");
  2323. }
  2324. if (!name) {
  2325. throw new Error("null name");
  2326. }
  2327. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  2328. userId: userId
  2329. });
  2330. return self.ajax({
  2331. type: "GET",
  2332. url: url,
  2333. dataType: "json"
  2334. });
  2335. };
  2336. /**
  2337. Gets a variety of item counts that a studio appears in
  2338. */
  2339. self.getStudioItemCounts = function (userId, name) {
  2340. if (!userId) {
  2341. throw new Error("null userId");
  2342. }
  2343. if (!name) {
  2344. throw new Error("null name");
  2345. }
  2346. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  2347. userId: userId
  2348. });
  2349. return self.ajax({
  2350. type: "GET",
  2351. url: url,
  2352. dataType: "json"
  2353. });
  2354. };
  2355. /**
  2356. * Clears a user's personal rating for an item
  2357. * @param {String} userId
  2358. * @param {String} itemId
  2359. */
  2360. self.clearUserItemRating = function (userId, itemId) {
  2361. if (!userId) {
  2362. throw new Error("null userId");
  2363. }
  2364. if (!itemId) {
  2365. throw new Error("null itemId");
  2366. }
  2367. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2368. return self.ajax({
  2369. type: "DELETE",
  2370. url: url
  2371. });
  2372. };
  2373. /**
  2374. * Reports the user has started playing something
  2375. * @param {String} userId
  2376. * @param {String} itemId
  2377. */
  2378. self.reportPlaybackStart = function (userId, itemId) {
  2379. if (!userId) {
  2380. throw new Error("null userId");
  2381. }
  2382. if (!itemId) {
  2383. throw new Error("null itemId");
  2384. }
  2385. if (self.isWebSocketOpen()) {
  2386. var deferred = $.Deferred();
  2387. self.sendWebSocketMessage("PlaybackStart", itemId);
  2388. deferred.resolveWith(null, []);
  2389. return deferred.promise();
  2390. }
  2391. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  2392. return self.ajax({
  2393. type: "POST",
  2394. url: url
  2395. });
  2396. };
  2397. /**
  2398. * Reports progress viewing an item
  2399. * @param {String} userId
  2400. * @param {String} itemId
  2401. */
  2402. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  2403. if (!userId) {
  2404. throw new Error("null userId");
  2405. }
  2406. if (!itemId) {
  2407. throw new Error("null itemId");
  2408. }
  2409. if (self.isWebSocketOpen()) {
  2410. var deferred = $.Deferred();
  2411. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  2412. deferred.resolveWith(null, []);
  2413. return deferred.promise();
  2414. }
  2415. var params = {
  2416. };
  2417. if (positionTicks) {
  2418. params.positionTicks = positionTicks;
  2419. }
  2420. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2421. return self.ajax({
  2422. type: "POST",
  2423. url: url
  2424. });
  2425. };
  2426. /**
  2427. * Reports a user has stopped playing an item
  2428. * @param {String} userId
  2429. * @param {String} itemId
  2430. */
  2431. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2432. if (!userId) {
  2433. throw new Error("null userId");
  2434. }
  2435. if (!itemId) {
  2436. throw new Error("null itemId");
  2437. }
  2438. if (self.isWebSocketOpen()) {
  2439. var deferred = $.Deferred();
  2440. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2441. deferred.resolveWith(null, []);
  2442. return deferred.promise();
  2443. }
  2444. var params = {
  2445. };
  2446. if (positionTicks) {
  2447. params.positionTicks = positionTicks;
  2448. }
  2449. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2450. return self.ajax({
  2451. type: "DELETE",
  2452. url: url
  2453. });
  2454. };
  2455. self.sendBrowseCommand = function (sessionId, options) {
  2456. if (!sessionId) {
  2457. throw new Error("null sessionId");
  2458. }
  2459. if (!options) {
  2460. throw new Error("null options");
  2461. }
  2462. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2463. return self.ajax({
  2464. type: "POST",
  2465. url: url
  2466. });
  2467. };
  2468. self.sendPlayCommand = function (sessionId, options) {
  2469. if (!sessionId) {
  2470. throw new Error("null sessionId");
  2471. }
  2472. if (!options) {
  2473. throw new Error("null options");
  2474. }
  2475. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2476. return self.ajax({
  2477. type: "POST",
  2478. url: url
  2479. });
  2480. };
  2481. self.sendPlayStateCommand = function (sessionId, command, options) {
  2482. if (!sessionId) {
  2483. throw new Error("null sessionId");
  2484. }
  2485. if (!command) {
  2486. throw new Error("null command");
  2487. }
  2488. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2489. return self.ajax({
  2490. type: "POST",
  2491. url: url
  2492. });
  2493. };
  2494. }
  2495. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2496. /**
  2497. * Provides a friendly way to create an api client instance using information from the browser's current url
  2498. */
  2499. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2500. var loc = window.location;
  2501. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2502. };
  2503. /**
  2504. *
  2505. * Secure Hash Algorithm (SHA1)
  2506. * http://www.webtoolkit.info/
  2507. *
  2508. **/
  2509. MediaBrowser.SHA1 = function (msg) {
  2510. function rotate_left(n, s) {
  2511. var t4 = (n << s) | (n >>> (32 - s));
  2512. return t4;
  2513. }
  2514. function lsb_hex(val) {
  2515. var str = "";
  2516. var i;
  2517. var vh;
  2518. var vl;
  2519. for (i = 0; i <= 6; i += 2) {
  2520. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2521. vl = (val >>> (i * 4)) & 0x0f;
  2522. str += vh.toString(16) + vl.toString(16);
  2523. }
  2524. return str;
  2525. }
  2526. function cvt_hex(val) {
  2527. var str = "";
  2528. var i;
  2529. var v;
  2530. for (i = 7; i >= 0; i--) {
  2531. v = (val >>> (i * 4)) & 0x0f;
  2532. str += v.toString(16);
  2533. }
  2534. return str;
  2535. }
  2536. function Utf8Encode(string) {
  2537. string = string.replace(/\r\n/g, "\n");
  2538. var utftext = "";
  2539. for (var n = 0; n < string.length; n++) {
  2540. var c = string.charCodeAt(n);
  2541. if (c < 128) {
  2542. utftext += String.fromCharCode(c);
  2543. }
  2544. else if ((c > 127) && (c < 2048)) {
  2545. utftext += String.fromCharCode((c >> 6) | 192);
  2546. utftext += String.fromCharCode((c & 63) | 128);
  2547. }
  2548. else {
  2549. utftext += String.fromCharCode((c >> 12) | 224);
  2550. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2551. utftext += String.fromCharCode((c & 63) | 128);
  2552. }
  2553. }
  2554. return utftext;
  2555. }
  2556. var blockstart;
  2557. var i, j;
  2558. var W = new Array(80);
  2559. var H0 = 0x67452301;
  2560. var H1 = 0xEFCDAB89;
  2561. var H2 = 0x98BADCFE;
  2562. var H3 = 0x10325476;
  2563. var H4 = 0xC3D2E1F0;
  2564. var A, B, C, D, E;
  2565. var temp;
  2566. msg = Utf8Encode(msg);
  2567. var msg_len = msg.length;
  2568. var word_array = new Array();
  2569. for (i = 0; i < msg_len - 3; i += 4) {
  2570. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2571. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2572. word_array.push(j);
  2573. }
  2574. switch (msg_len % 4) {
  2575. case 0:
  2576. i = 0x080000000;
  2577. break;
  2578. case 1:
  2579. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2580. break;
  2581. case 2:
  2582. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2583. break;
  2584. case 3:
  2585. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2586. break;
  2587. }
  2588. word_array.push(i);
  2589. while ((word_array.length % 16) != 14) word_array.push(0);
  2590. word_array.push(msg_len >>> 29);
  2591. word_array.push((msg_len << 3) & 0x0ffffffff);
  2592. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2593. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2594. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2595. A = H0;
  2596. B = H1;
  2597. C = H2;
  2598. D = H3;
  2599. E = H4;
  2600. for (i = 0; i <= 19; i++) {
  2601. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2602. E = D;
  2603. D = C;
  2604. C = rotate_left(B, 30);
  2605. B = A;
  2606. A = temp;
  2607. }
  2608. for (i = 20; i <= 39; i++) {
  2609. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2610. E = D;
  2611. D = C;
  2612. C = rotate_left(B, 30);
  2613. B = A;
  2614. A = temp;
  2615. }
  2616. for (i = 40; i <= 59; i++) {
  2617. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2618. E = D;
  2619. D = C;
  2620. C = rotate_left(B, 30);
  2621. B = A;
  2622. A = temp;
  2623. }
  2624. for (i = 60; i <= 79; i++) {
  2625. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2626. E = D;
  2627. D = C;
  2628. C = rotate_left(B, 30);
  2629. B = A;
  2630. A = temp;
  2631. }
  2632. H0 = (H0 + A) & 0x0ffffffff;
  2633. H1 = (H1 + B) & 0x0ffffffff;
  2634. H2 = (H2 + C) & 0x0ffffffff;
  2635. H3 = (H3 + D) & 0x0ffffffff;
  2636. H4 = (H4 + E) & 0x0ffffffff;
  2637. }
  2638. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2639. return temp.toLowerCase();
  2640. };
  2641. (function (jQuery, window, undefined) {
  2642. "use strict";
  2643. var matched, browser;
  2644. jQuery.uaMatch = function (ua) {
  2645. ua = ua.toLowerCase();
  2646. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2647. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2648. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2649. /(msie) ([\w.]+)/.exec(ua) ||
  2650. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2651. [];
  2652. var platform_match = /(ipad)/.exec(ua) ||
  2653. /(iphone)/.exec(ua) ||
  2654. /(android)/.exec(ua) ||
  2655. [];
  2656. return {
  2657. browser: match[1] || "",
  2658. version: match[2] || "0",
  2659. platform: platform_match[0] || ""
  2660. };
  2661. };
  2662. matched = jQuery.uaMatch(window.navigator.userAgent);
  2663. browser = {};
  2664. if (matched.browser) {
  2665. browser[matched.browser] = true;
  2666. browser.version = matched.version;
  2667. }
  2668. if (matched.platform) {
  2669. browser[matched.platform] = true
  2670. }
  2671. // Chrome is Webkit, but Webkit is also Safari.
  2672. if (browser.chrome) {
  2673. browser.webkit = true;
  2674. } else if (browser.webkit) {
  2675. browser.safari = true;
  2676. }
  2677. jQuery.browser = browser;
  2678. })(jQuery, window);