ApiClient.js 91 KB

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