ApiClient.js 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  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.encodeName = function (name) {
  84. var val = $.param({ name: name });
  85. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  86. };
  87. /**
  88. * Wraps around jQuery ajax methods to add additional info to the request.
  89. */
  90. self.ajax = function (request) {
  91. if (!request) {
  92. throw new Error("Request cannot be null");
  93. }
  94. if (clientName) {
  95. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  96. if (currentUserId) {
  97. auth += ', UserId="' + currentUserId + '"';
  98. }
  99. request.headers = {
  100. Authorization: auth
  101. };
  102. }
  103. return $.ajax(request);
  104. };
  105. /**
  106. * Creates an api url based on a handler name and query string parameters
  107. * @param {String} name
  108. * @param {Object} params
  109. */
  110. self.getUrl = function (name, params) {
  111. if (!name) {
  112. throw new Error("Url name cannot be empty");
  113. }
  114. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  115. if (params) {
  116. url += "?" + $.param(params);
  117. }
  118. return url;
  119. };
  120. self.openWebSocket = function (port) {
  121. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  122. webSocket = new WebSocket(url);
  123. webSocket.onmessage = function (msg) {
  124. msg = JSON.parse(msg.data);
  125. $(self).trigger("websocketmessage", [msg]);
  126. };
  127. webSocket.onopen = function () {
  128. setTimeout(function () {
  129. $(self).trigger("websocketopen");
  130. }, 500);
  131. };
  132. webSocket.onerror = function () {
  133. setTimeout(function () {
  134. $(self).trigger("websocketerror");
  135. }, 0);
  136. };
  137. webSocket.onclose = function () {
  138. setTimeout(function () {
  139. $(self).trigger("websocketclose");
  140. }, 0);
  141. };
  142. };
  143. self.sendWebSocketMessage = function (name, data) {
  144. var msg = { MessageType: name };
  145. if (data) {
  146. msg.Data = data;
  147. }
  148. msg = JSON.stringify(msg);
  149. webSocket.send(msg);
  150. };
  151. self.isWebSocketOpen = function () {
  152. return webSocket && webSocket.readyState === WebSocket.OPEN;
  153. };
  154. self.isWebSocketOpenOrConnecting = function () {
  155. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  156. };
  157. /**
  158. * Gets an item from the server
  159. * Omit itemId to get the root folder.
  160. */
  161. self.getItem = function (userId, itemId) {
  162. if (!userId) {
  163. throw new Error("null userId");
  164. }
  165. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  166. return self.ajax({
  167. type: "GET",
  168. url: url,
  169. dataType: "json"
  170. });
  171. };
  172. /**
  173. * Gets the root folder from the server
  174. */
  175. self.getRootFolder = function (userId) {
  176. if (!userId) {
  177. throw new Error("null userId");
  178. }
  179. var url = self.getUrl("Users/" + userId + "/Items/Root");
  180. return self.ajax({
  181. type: "GET",
  182. url: url,
  183. dataType: "json"
  184. });
  185. };
  186. /**
  187. * Gets the current server status
  188. */
  189. self.getSystemInfo = function () {
  190. var url = self.getUrl("System/Info");
  191. return self.ajax({
  192. type: "GET",
  193. url: url,
  194. dataType: "json"
  195. });
  196. };
  197. /**
  198. * Gets all cultures known to the server
  199. */
  200. self.getCultures = function () {
  201. var url = self.getUrl("Localization/cultures");
  202. return self.ajax({
  203. type: "GET",
  204. url: url,
  205. dataType: "json"
  206. });
  207. };
  208. /**
  209. * Gets all countries known to the server
  210. */
  211. self.getCountries = function () {
  212. var url = self.getUrl("Localization/countries");
  213. return self.ajax({
  214. type: "GET",
  215. url: url,
  216. dataType: "json"
  217. });
  218. };
  219. /**
  220. * Gets plugin security info
  221. */
  222. self.getPluginSecurityInfo = function () {
  223. var url = self.getUrl("Plugins/SecurityInfo");
  224. return self.ajax({
  225. type: "GET",
  226. url: url,
  227. dataType: "json"
  228. });
  229. };
  230. /**
  231. * Gets the directory contents of a path on the server
  232. */
  233. self.getDirectoryContents = function (path, options) {
  234. if (!path) {
  235. throw new Error("null path");
  236. }
  237. options = options || {};
  238. options.path = path;
  239. var url = self.getUrl("Environment/DirectoryContents", options);
  240. return self.ajax({
  241. type: "GET",
  242. url: url,
  243. dataType: "json"
  244. });
  245. };
  246. /**
  247. * Gets a list of physical drives from the server
  248. */
  249. self.getDrives = function () {
  250. var url = self.getUrl("Environment/Drives");
  251. return self.ajax({
  252. type: "GET",
  253. url: url,
  254. dataType: "json"
  255. });
  256. };
  257. /**
  258. * Gets a list of network devices from the server
  259. */
  260. self.getNetworkDevices = function () {
  261. var url = self.getUrl("Environment/NetworkDevices");
  262. return self.ajax({
  263. type: "GET",
  264. url: url,
  265. dataType: "json"
  266. });
  267. };
  268. /**
  269. * Cancels a package installation
  270. */
  271. self.cancelPackageInstallation = function (installationId) {
  272. if (!installationId) {
  273. throw new Error("null installationId");
  274. }
  275. var url = self.getUrl("Packages/Installing/" + id);
  276. return self.ajax({
  277. type: "DELETE",
  278. url: url
  279. });
  280. };
  281. /**
  282. * Installs or updates a new plugin
  283. */
  284. self.installPlugin = function (name, updateClass, version) {
  285. if (!name) {
  286. throw new Error("null name");
  287. }
  288. if (!updateClass) {
  289. throw new Error("null updateClass");
  290. }
  291. var options = {
  292. updateClass: updateClass
  293. };
  294. if (version) {
  295. options.version = version;
  296. }
  297. var url = self.getUrl("Packages/Installed/" + name, options);
  298. return self.ajax({
  299. type: "POST",
  300. url: url
  301. });
  302. };
  303. /**
  304. * Instructs the server to perform a pending kernel reload or app restart.
  305. * If a restart is not currently required, nothing will happen.
  306. */
  307. self.performPendingRestart = function () {
  308. var url = self.getUrl("System/Restart");
  309. return self.ajax({
  310. type: "POST",
  311. url: url
  312. });
  313. };
  314. /**
  315. * Gets information about an installable package
  316. */
  317. self.getPackageInfo = function (name) {
  318. if (!name) {
  319. throw new Error("null name");
  320. }
  321. var url = self.getUrl("Packages/" + name);
  322. return self.ajax({
  323. type: "GET",
  324. url: url,
  325. dataType: "json"
  326. });
  327. };
  328. /**
  329. * Gets the latest available application update (if any)
  330. */
  331. self.getAvailableApplicationUpdate = function () {
  332. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  333. return self.ajax({
  334. type: "GET",
  335. url: url,
  336. dataType: "json"
  337. });
  338. };
  339. /**
  340. * Gets the latest available plugin updates (if any)
  341. */
  342. self.getAvailablePluginUpdates = function () {
  343. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  344. return self.ajax({
  345. type: "GET",
  346. url: url,
  347. dataType: "json"
  348. });
  349. };
  350. /**
  351. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  352. */
  353. self.getVirtualFolders = function (userId) {
  354. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  355. url = self.getUrl(url);
  356. return self.ajax({
  357. type: "GET",
  358. url: url,
  359. dataType: "json"
  360. });
  361. };
  362. /**
  363. * Gets all the paths of the locations in the physical root.
  364. */
  365. self.getPhysicalPaths = function () {
  366. var url = self.getUrl("Library/PhysicalPaths");
  367. return self.ajax({
  368. type: "GET",
  369. url: url,
  370. dataType: "json"
  371. });
  372. };
  373. /**
  374. * Gets the current server configuration
  375. */
  376. self.getServerConfiguration = function () {
  377. var url = self.getUrl("System/Configuration");
  378. return self.ajax({
  379. type: "GET",
  380. url: url,
  381. dataType: "json"
  382. });
  383. };
  384. /**
  385. * Gets the server's scheduled tasks
  386. */
  387. self.getScheduledTasks = function () {
  388. var url = self.getUrl("ScheduledTasks");
  389. return self.ajax({
  390. type: "GET",
  391. url: url,
  392. dataType: "json"
  393. });
  394. };
  395. /**
  396. * Starts a scheduled task
  397. */
  398. self.startScheduledTask = function (id) {
  399. if (!id) {
  400. throw new Error("null id");
  401. }
  402. var url = self.getUrl("ScheduledTasks/Running/" + id);
  403. return self.ajax({
  404. type: "POST",
  405. url: url
  406. });
  407. };
  408. /**
  409. * Gets a scheduled task
  410. */
  411. self.getScheduledTask = function (id) {
  412. if (!id) {
  413. throw new Error("null id");
  414. }
  415. var url = self.getUrl("ScheduledTasks/" + id);
  416. return self.ajax({
  417. type: "GET",
  418. url: url,
  419. dataType: "json"
  420. });
  421. };
  422. /**
  423. * Stops a scheduled task
  424. */
  425. self.stopScheduledTask = function (id) {
  426. if (!id) {
  427. throw new Error("null id");
  428. }
  429. var url = self.getUrl("ScheduledTasks/Running/" + id);
  430. return self.ajax({
  431. type: "DELETE",
  432. url: url
  433. });
  434. };
  435. /**
  436. * Gets the configuration of a plugin
  437. * @param {String} Id
  438. */
  439. self.getPluginConfiguration = function (id) {
  440. if (!id) {
  441. throw new Error("null Id");
  442. }
  443. var url = self.getUrl("Plugins/" + id + "/Configuration");
  444. return self.ajax({
  445. type: "GET",
  446. url: url,
  447. dataType: "json"
  448. });
  449. };
  450. /**
  451. * Gets a list of plugins that are available to be installed
  452. */
  453. self.getAvailablePlugins = function (options) {
  454. options = $.extend({}, options || {});
  455. options.PackageType = "UserInstalled";
  456. var url = self.getUrl("Packages", options);
  457. return self.ajax({
  458. type: "GET",
  459. url: url,
  460. dataType: "json"
  461. });
  462. };
  463. /**
  464. * Uninstalls a plugin
  465. * @param {String} Id
  466. */
  467. self.uninstallPlugin = function (id) {
  468. if (!id) {
  469. throw new Error("null Id");
  470. }
  471. var url = self.getUrl("Plugins/" + id);
  472. return self.ajax({
  473. type: "DELETE",
  474. url: url
  475. });
  476. };
  477. /**
  478. * Removes a virtual folder from either the default view or a user view
  479. * @param {String} name
  480. */
  481. self.removeVirtualFolder = function (name, userId) {
  482. if (!name) {
  483. throw new Error("null name");
  484. }
  485. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  486. url += "/" + name;
  487. url = self.getUrl(url);
  488. return self.ajax({
  489. type: "DELETE",
  490. url: url
  491. });
  492. };
  493. /**
  494. * Adds a virtual folder to either the default view or a user view
  495. * @param {String} name
  496. */
  497. self.addVirtualFolder = function (name, userId) {
  498. if (!name) {
  499. throw new Error("null name");
  500. }
  501. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  502. url += "/" + name;
  503. url = self.getUrl(url);
  504. return self.ajax({
  505. type: "POST",
  506. url: url
  507. });
  508. };
  509. /**
  510. * Renames a virtual folder, within either the default view or a user view
  511. * @param {String} name
  512. */
  513. self.renameVirtualFolder = function (name, newName, userId) {
  514. if (!name) {
  515. throw new Error("null name");
  516. }
  517. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  518. url += "/" + name + "/Name";
  519. url = self.getUrl(url, { newName: newName });
  520. return self.ajax({
  521. type: "POST",
  522. url: url
  523. });
  524. };
  525. /**
  526. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  527. * @param {String} name
  528. */
  529. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  530. if (!virtualFolderName) {
  531. throw new Error("null virtualFolderName");
  532. }
  533. if (!mediaPath) {
  534. throw new Error("null mediaPath");
  535. }
  536. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  537. url += "/" + virtualFolderName + "/Paths";
  538. url = self.getUrl(url, { path: mediaPath });
  539. return self.ajax({
  540. type: "POST",
  541. url: url
  542. });
  543. };
  544. /**
  545. * Removes a media path from a virtual folder, within either the default view or a user view
  546. * @param {String} name
  547. */
  548. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  549. if (!virtualFolderName) {
  550. throw new Error("null virtualFolderName");
  551. }
  552. if (!mediaPath) {
  553. throw new Error("null mediaPath");
  554. }
  555. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  556. url += "/" + virtualFolderName + "/Paths";
  557. url = self.getUrl(url, { path: mediaPath });
  558. return self.ajax({
  559. type: "DELETE",
  560. url: url
  561. });
  562. };
  563. /**
  564. * Deletes a user
  565. * @param {String} id
  566. */
  567. self.deleteUser = function (id) {
  568. if (!id) {
  569. throw new Error("null id");
  570. }
  571. var url = self.getUrl("Users/" + id);
  572. return self.ajax({
  573. type: "DELETE",
  574. url: url
  575. });
  576. };
  577. /**
  578. * Deletes a user image
  579. * @param {String} userId
  580. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  581. */
  582. self.deleteUserImage = function (userId, imageType) {
  583. if (!userId) {
  584. throw new Error("null userId");
  585. }
  586. if (!imageType) {
  587. throw new Error("null imageType");
  588. }
  589. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  590. return self.ajax({
  591. type: "DELETE",
  592. url: url
  593. });
  594. };
  595. /**
  596. * Uploads a user image
  597. * @param {String} userId
  598. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  599. * @param {Object} file The file from the input element
  600. */
  601. self.uploadUserImage = function (userId, imageType, file) {
  602. if (!userId) {
  603. throw new Error("null userId");
  604. }
  605. if (!imageType) {
  606. throw new Error("null imageType");
  607. }
  608. if (!file) {
  609. throw new Error("File must be an image.");
  610. }
  611. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  612. throw new Error("File must be an image.");
  613. }
  614. var deferred = $.Deferred();
  615. var reader = new FileReader();
  616. reader.onerror = function () {
  617. deferred.reject();
  618. };
  619. reader.onabort = function () {
  620. deferred.reject();
  621. };
  622. // Closure to capture the file information.
  623. reader.onload = function (e) {
  624. var data = window.btoa(e.target.result);
  625. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  626. self.ajax({
  627. type: "POST",
  628. url: url,
  629. data: data,
  630. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  631. }).done(function (result) {
  632. deferred.resolveWith(null, [result]);
  633. }).fail(function () {
  634. deferred.reject();
  635. });
  636. };
  637. // Read in the image file as a data URL.
  638. reader.readAsBinaryString(file);
  639. return deferred.promise();
  640. };
  641. /**
  642. * Gets the list of installed plugins on the server
  643. */
  644. self.getInstalledPlugins = function () {
  645. var url = self.getUrl("Plugins");
  646. return self.ajax({
  647. type: "GET",
  648. url: url,
  649. dataType: "json"
  650. });
  651. };
  652. /**
  653. * Gets a user by id
  654. * @param {String} id
  655. */
  656. self.getUser = function (id) {
  657. if (!id) {
  658. throw new Error("Must supply a userId");
  659. }
  660. var url = self.getUrl("Users/" + id);
  661. return self.ajax({
  662. type: "GET",
  663. url: url,
  664. dataType: "json"
  665. });
  666. };
  667. /**
  668. * Gets a studio
  669. */
  670. self.getStudio = function (name, userId) {
  671. if (!name) {
  672. throw new Error("null name");
  673. }
  674. var options = {};
  675. if (userId) {
  676. options.userId = userId;
  677. }
  678. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  679. return self.ajax({
  680. type: "GET",
  681. url: url,
  682. dataType: "json"
  683. });
  684. };
  685. /**
  686. * Gets a genre
  687. */
  688. self.getGenre = function (name, userId) {
  689. if (!name) {
  690. throw new Error("null name");
  691. }
  692. var options = {};
  693. if (userId) {
  694. options.userId = userId;
  695. }
  696. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  697. return self.ajax({
  698. type: "GET",
  699. url: url,
  700. dataType: "json"
  701. });
  702. };
  703. /**
  704. * Gets an artist
  705. */
  706. self.getArtist = function (name, userId) {
  707. if (!name) {
  708. throw new Error("null name");
  709. }
  710. var options = {};
  711. if (userId) {
  712. options.userId = userId;
  713. }
  714. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  715. return self.ajax({
  716. type: "GET",
  717. url: url,
  718. dataType: "json"
  719. });
  720. };
  721. /**
  722. * Gets a year
  723. */
  724. self.getYear = function (yea, userId) {
  725. if (!name) {
  726. throw new Error("null name");
  727. }
  728. var options = {};
  729. if (userId) {
  730. options.userId = userId;
  731. }
  732. var url = self.getUrl("Years/" + self.encodeName(name), options);
  733. return self.ajax({
  734. type: "GET",
  735. url: url,
  736. dataType: "json"
  737. });
  738. };
  739. /**
  740. * Gets a Person
  741. */
  742. self.getPerson = function (name, userId) {
  743. if (!name) {
  744. throw new Error("null name");
  745. }
  746. var options = {};
  747. if (userId) {
  748. options.userId = userId;
  749. }
  750. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  751. return self.ajax({
  752. type: "GET",
  753. url: url,
  754. dataType: "json"
  755. });
  756. };
  757. /**
  758. * Gets weather info
  759. * @param {String} location - us zip code / city, state, country / city, country
  760. * Omit location to get weather info using stored server configuration value
  761. */
  762. self.getWeatherInfo = function (location) {
  763. var url = self.getUrl("weather", {
  764. location: location
  765. });
  766. return self.ajax({
  767. type: "GET",
  768. url: url,
  769. dataType: "json"
  770. });
  771. };
  772. /**
  773. * Gets all users from the server
  774. */
  775. self.getUsers = function () {
  776. var url = self.getUrl("users");
  777. return self.ajax({
  778. type: "GET",
  779. url: url,
  780. dataType: "json"
  781. });
  782. };
  783. /**
  784. * Gets all available parental ratings from the server
  785. */
  786. self.getParentalRatings = function () {
  787. var url = self.getUrl("Localization/ParentalRatings");
  788. return self.ajax({
  789. type: "GET",
  790. url: url,
  791. dataType: "json"
  792. });
  793. };
  794. /**
  795. * Gets a list of all available conrete BaseItem types from the server
  796. */
  797. self.getItemTypes = function (options) {
  798. var url = self.getUrl("Library/ItemTypes", options);
  799. return self.ajax({
  800. type: "GET",
  801. url: url,
  802. dataType: "json"
  803. });
  804. };
  805. /**
  806. * Constructs a url for a user image
  807. * @param {String} userId
  808. * @param {Object} options
  809. * Options supports the following properties:
  810. * width - download the image at a fixed width
  811. * height - download the image at a fixed height
  812. * maxWidth - download the image at a maxWidth
  813. * maxHeight - download the image at a maxHeight
  814. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  815. * For best results do not specify both width and height together, as aspect ratio might be altered.
  816. */
  817. self.getUserImageUrl = function (userId, options) {
  818. if (!userId) {
  819. throw new Error("null userId");
  820. }
  821. options = options || {
  822. };
  823. var url = "Users/" + userId + "/Images/" + options.type;
  824. if (options.index != null) {
  825. url += "/" + options.index;
  826. }
  827. // Don't put these on the query string
  828. delete options.type;
  829. delete options.index;
  830. return self.getUrl(url, options);
  831. };
  832. /**
  833. * Constructs a url for a person image
  834. * @param {String} name
  835. * @param {Object} options
  836. * Options supports the following properties:
  837. * width - download the image at a fixed width
  838. * height - download the image at a fixed height
  839. * maxWidth - download the image at a maxWidth
  840. * maxHeight - download the image at a maxHeight
  841. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  842. * For best results do not specify both width and height together, as aspect ratio might be altered.
  843. */
  844. self.getPersonImageUrl = function (name, options) {
  845. if (!name) {
  846. throw new Error("null name");
  847. }
  848. options = options || {
  849. };
  850. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  851. if (options.index != null) {
  852. url += "/" + options.index;
  853. }
  854. // Don't put these on the query string
  855. delete options.type;
  856. delete options.index;
  857. return self.getUrl(url, options);
  858. };
  859. /**
  860. * Constructs a url for a year image
  861. * @param {String} year
  862. * @param {Object} options
  863. * Options supports the following properties:
  864. * width - download the image at a fixed width
  865. * height - download the image at a fixed height
  866. * maxWidth - download the image at a maxWidth
  867. * maxHeight - download the image at a maxHeight
  868. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  869. * For best results do not specify both width and height together, as aspect ratio might be altered.
  870. */
  871. self.getYearImageUrl = function (year, options) {
  872. if (!year) {
  873. throw new Error("null year");
  874. }
  875. options = options || {
  876. };
  877. var url = "Years/" + year + "/Images/" + options.type;
  878. if (options.index != null) {
  879. url += "/" + options.index;
  880. }
  881. // Don't put these on the query string
  882. delete options.type;
  883. delete options.index;
  884. return self.getUrl(url, options);
  885. };
  886. /**
  887. * Constructs a url for a genre image
  888. * @param {String} name
  889. * @param {Object} options
  890. * Options supports the following properties:
  891. * width - download the image at a fixed width
  892. * height - download the image at a fixed height
  893. * maxWidth - download the image at a maxWidth
  894. * maxHeight - download the image at a maxHeight
  895. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  896. * For best results do not specify both width and height together, as aspect ratio might be altered.
  897. */
  898. self.getGenreImageUrl = function (name, options) {
  899. if (!name) {
  900. throw new Error("null name");
  901. }
  902. options = options || {
  903. };
  904. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  905. if (options.index != null) {
  906. url += "/" + options.index;
  907. }
  908. // Don't put these on the query string
  909. delete options.type;
  910. delete options.index;
  911. return self.getUrl(url, options);
  912. };
  913. /**
  914. * Constructs a url for a artist image
  915. * @param {String} name
  916. * @param {Object} options
  917. * Options supports the following properties:
  918. * width - download the image at a fixed width
  919. * height - download the image at a fixed height
  920. * maxWidth - download the image at a maxWidth
  921. * maxHeight - download the image at a maxHeight
  922. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  923. * For best results do not specify both width and height together, as aspect ratio might be altered.
  924. */
  925. self.getArtistImageUrl = function (name, options) {
  926. if (!name) {
  927. throw new Error("null name");
  928. }
  929. options = options || {
  930. };
  931. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  932. if (options.index != null) {
  933. url += "/" + options.index;
  934. }
  935. // Don't put these on the query string
  936. delete options.type;
  937. delete options.index;
  938. return self.getUrl(url, options);
  939. };
  940. /**
  941. * Constructs a url for a studio image
  942. * @param {String} name
  943. * @param {Object} options
  944. * Options supports the following properties:
  945. * width - download the image at a fixed width
  946. * height - download the image at a fixed height
  947. * maxWidth - download the image at a maxWidth
  948. * maxHeight - download the image at a maxHeight
  949. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  950. * For best results do not specify both width and height together, as aspect ratio might be altered.
  951. */
  952. self.getStudioImageUrl = function (name, options) {
  953. if (!name) {
  954. throw new Error("null name");
  955. }
  956. options = options || {
  957. };
  958. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  959. if (options.index != null) {
  960. url += "/" + options.index;
  961. }
  962. // Don't put these on the query string
  963. delete options.type;
  964. delete options.index;
  965. return self.getUrl(url, options);
  966. };
  967. /**
  968. * Constructs a url for an item image
  969. * @param {String} itemId
  970. * @param {Object} options
  971. * Options supports the following properties:
  972. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  973. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  974. * width - download the image at a fixed width
  975. * height - download the image at a fixed height
  976. * maxWidth - download the image at a maxWidth
  977. * maxHeight - download the image at a maxHeight
  978. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  979. * For best results do not specify both width and height together, as aspect ratio might be altered.
  980. */
  981. self.getImageUrl = function (itemId, options) {
  982. if (!itemId) {
  983. throw new Error("itemId cannot be empty");
  984. }
  985. options = options || {
  986. };
  987. var url = "Items/" + itemId + "/Images/" + options.type;
  988. if (options.index != null) {
  989. url += "/" + options.index;
  990. }
  991. // Don't put these on the query string
  992. delete options.type;
  993. delete options.index;
  994. return self.getUrl(url, options);
  995. };
  996. /**
  997. * Constructs a url for an item logo image
  998. * If the item doesn't have a logo, it will inherit a logo from a parent
  999. * @param {Object} item A BaseItem
  1000. * @param {Object} options
  1001. * Options supports the following properties:
  1002. * width - download the image at a fixed width
  1003. * height - download the image at a fixed height
  1004. * maxWidth - download the image at a maxWidth
  1005. * maxHeight - download the image at a maxHeight
  1006. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1007. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1008. */
  1009. self.getLogoImageUrl = function (item, options) {
  1010. if (!item) {
  1011. throw new Error("null item");
  1012. }
  1013. options = options || {
  1014. };
  1015. options.imageType = "logo";
  1016. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1017. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1018. };
  1019. /**
  1020. * Constructs an array of backdrop image url's for an item
  1021. * If the item doesn't have any backdrops, it will inherit them from a parent
  1022. * @param {Object} item A BaseItem
  1023. * @param {Object} options
  1024. * Options supports the following properties:
  1025. * width - download the image at a fixed width
  1026. * height - download the image at a fixed height
  1027. * maxWidth - download the image at a maxWidth
  1028. * maxHeight - download the image at a maxHeight
  1029. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1030. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1031. */
  1032. self.getBackdropImageUrl = function (item, options) {
  1033. if (!item) {
  1034. throw new Error("null item");
  1035. }
  1036. options = options || {
  1037. };
  1038. options.imageType = "backdrop";
  1039. var backdropItemId;
  1040. var backdropCount;
  1041. if (!item.BackdropCount) {
  1042. backdropItemId = item.ParentBackdropItemId;
  1043. backdropCount = item.ParentBackdropCount || 0;
  1044. } else {
  1045. backdropItemId = item.Id;
  1046. backdropCount = item.BackdropCount;
  1047. }
  1048. if (!backdropItemId) {
  1049. return [];
  1050. }
  1051. var files = [];
  1052. for (var i = 0; i < backdropCount; i++) {
  1053. options.imageIndex = i;
  1054. files[i] = self.getImageUrl(backdropItemId, options);
  1055. }
  1056. return files;
  1057. };
  1058. /**
  1059. * Authenticates a user
  1060. * @param {String} userId
  1061. * @param {String} password
  1062. */
  1063. self.authenticateUser = function (userId, password) {
  1064. if (!userId) {
  1065. throw new Error("null userId");
  1066. }
  1067. var url = self.getUrl("Users/" + userId + "/authenticate");
  1068. var postData = {
  1069. password: MediaBrowser.SHA1(password || "")
  1070. };
  1071. return self.ajax({
  1072. type: "POST",
  1073. url: url,
  1074. data: JSON.stringify(postData),
  1075. contentType: "application/json"
  1076. });
  1077. };
  1078. /**
  1079. * Updates a user's password
  1080. * @param {String} userId
  1081. * @param {String} currentPassword
  1082. * @param {String} newPassword
  1083. */
  1084. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1085. if (!userId) {
  1086. throw new Error("null userId");
  1087. }
  1088. var url = self.getUrl("Users/" + userId + "/Password");
  1089. var postData = {
  1090. };
  1091. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1092. if (newPassword) {
  1093. postData.newPassword = newPassword;
  1094. }
  1095. return self.ajax({
  1096. type: "POST",
  1097. url: url,
  1098. data: postData
  1099. });
  1100. };
  1101. /**
  1102. * Resets a user's password
  1103. * @param {String} userId
  1104. */
  1105. self.resetUserPassword = function (userId) {
  1106. if (!userId) {
  1107. throw new Error("null userId");
  1108. }
  1109. var url = self.getUrl("Users/" + userId + "/Password");
  1110. var postData = {
  1111. };
  1112. postData.resetPassword = true;
  1113. return self.ajax({
  1114. type: "POST",
  1115. url: url,
  1116. data: postData
  1117. });
  1118. };
  1119. /**
  1120. * Updates the server's configuration
  1121. * @param {Object} configuration
  1122. */
  1123. self.updateServerConfiguration = function (configuration) {
  1124. if (!configuration) {
  1125. throw new Error("null configuration");
  1126. }
  1127. var url = self.getUrl("System/Configuration");
  1128. return self.ajax({
  1129. type: "POST",
  1130. url: url,
  1131. data: JSON.stringify(configuration),
  1132. contentType: "application/json"
  1133. });
  1134. };
  1135. /**
  1136. * Updates plugin security info
  1137. */
  1138. self.updatePluginSecurityInfo = function (info) {
  1139. var url = self.getUrl("Plugins/SecurityInfo");
  1140. return self.ajax({
  1141. type: "POST",
  1142. url: url,
  1143. data: JSON.stringify(info),
  1144. contentType: "application/json"
  1145. });
  1146. };
  1147. /**
  1148. * Creates a user
  1149. * @param {Object} user
  1150. */
  1151. self.createUser = function (user) {
  1152. if (!user) {
  1153. throw new Error("null user");
  1154. }
  1155. var url = self.getUrl("Users");
  1156. return self.ajax({
  1157. type: "POST",
  1158. url: url,
  1159. data: JSON.stringify(user),
  1160. dataType: "json",
  1161. contentType: "application/json"
  1162. });
  1163. };
  1164. /**
  1165. * Updates a user
  1166. * @param {Object} user
  1167. */
  1168. self.updateUser = function (user) {
  1169. if (!user) {
  1170. throw new Error("null user");
  1171. }
  1172. var url = self.getUrl("Users/" + user.Id);
  1173. return self.ajax({
  1174. type: "POST",
  1175. url: url,
  1176. data: JSON.stringify(user),
  1177. contentType: "application/json"
  1178. });
  1179. };
  1180. /**
  1181. * Updates the Triggers for a ScheduledTask
  1182. * @param {String} id
  1183. * @param {Object} triggers
  1184. */
  1185. self.updateScheduledTaskTriggers = function (id, triggers) {
  1186. if (!id) {
  1187. throw new Error("null id");
  1188. }
  1189. if (!triggers) {
  1190. throw new Error("null triggers");
  1191. }
  1192. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1193. return self.ajax({
  1194. type: "POST",
  1195. url: url,
  1196. data: JSON.stringify(triggers),
  1197. contentType: "application/json"
  1198. });
  1199. };
  1200. /**
  1201. * Updates a plugin's configuration
  1202. * @param {String} Id
  1203. * @param {Object} configuration
  1204. */
  1205. self.updatePluginConfiguration = function (id, configuration) {
  1206. if (!id) {
  1207. throw new Error("null Id");
  1208. }
  1209. if (!configuration) {
  1210. throw new Error("null configuration");
  1211. }
  1212. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1213. return self.ajax({
  1214. type: "POST",
  1215. url: url,
  1216. data: JSON.stringify(configuration),
  1217. contentType: "application/json"
  1218. });
  1219. };
  1220. /**
  1221. * Gets items based on a query, typically for children of a folder
  1222. * @param {String} userId
  1223. * @param {Object} options
  1224. * Options accepts the following properties:
  1225. * itemId - Localize the search to a specific folder (root if omitted)
  1226. * startIndex - Use for paging
  1227. * limit - Use to limit results to a certain number of items
  1228. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1229. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1230. * sortOrder - ascending/descending
  1231. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1232. * index - the name of the dynamic, localized index function
  1233. * dynamicSortBy - the name of the dynamic localized sort function
  1234. * recursive - Whether or not the query should be recursive
  1235. * searchTerm - search term to use as a filter
  1236. */
  1237. self.getItems = function (userId, options) {
  1238. if (!userId) {
  1239. throw new Error("null userId");
  1240. }
  1241. var url = self.getUrl("Users/" + userId + "/Items", options);
  1242. return self.ajax({
  1243. type: "GET",
  1244. url: url,
  1245. dataType: "json"
  1246. });
  1247. };
  1248. /**
  1249. Gets artists from an item
  1250. */
  1251. self.getArtists = function (userId, options) {
  1252. if (!userId) {
  1253. throw new Error("null userId");
  1254. }
  1255. options = options || {};
  1256. options.userId = userId;
  1257. var url = self.getUrl("Artists", options);
  1258. return self.ajax({
  1259. type: "GET",
  1260. url: url,
  1261. dataType: "json"
  1262. });
  1263. };
  1264. /**
  1265. Gets genres from an item
  1266. */
  1267. self.getGenres = function (userId, options) {
  1268. if (!userId) {
  1269. throw new Error("null userId");
  1270. }
  1271. options = options || {};
  1272. options.userId = userId;
  1273. var url = self.getUrl("Genres", options);
  1274. return self.ajax({
  1275. type: "GET",
  1276. url: url,
  1277. dataType: "json"
  1278. });
  1279. };
  1280. /**
  1281. Gets people from an item
  1282. */
  1283. self.getPeople = function (userId, options) {
  1284. if (!userId) {
  1285. throw new Error("null userId");
  1286. }
  1287. options = options || {};
  1288. options.userId = userId;
  1289. var url = self.getUrl("Persons", options);
  1290. return self.ajax({
  1291. type: "GET",
  1292. url: url,
  1293. dataType: "json"
  1294. });
  1295. };
  1296. /**
  1297. Gets studios from an item
  1298. */
  1299. self.getStudios = function (userId, options) {
  1300. if (!userId) {
  1301. throw new Error("null userId");
  1302. }
  1303. options = options || {};
  1304. options.userId = userId;
  1305. var url = self.getUrl("Studios", options);
  1306. return self.ajax({
  1307. type: "GET",
  1308. url: url,
  1309. dataType: "json"
  1310. });
  1311. };
  1312. /**
  1313. * Gets local trailers for an item
  1314. */
  1315. self.getLocalTrailers = function (userId, itemId) {
  1316. if (!userId) {
  1317. throw new Error("null userId");
  1318. }
  1319. if (!itemId) {
  1320. throw new Error("null itemId");
  1321. }
  1322. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1323. return self.ajax({
  1324. type: "GET",
  1325. url: url,
  1326. dataType: "json"
  1327. });
  1328. };
  1329. /**
  1330. * Gets theme songs for an item
  1331. */
  1332. self.getThemeSongs = function (userId, itemId) {
  1333. if (!userId) {
  1334. throw new Error("null userId");
  1335. }
  1336. if (!itemId) {
  1337. throw new Error("null itemId");
  1338. }
  1339. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/ThemeSongs");
  1340. return self.ajax({
  1341. type: "GET",
  1342. url: url,
  1343. dataType: "json"
  1344. });
  1345. };
  1346. self.getThemeVideos = function (userId, itemId) {
  1347. if (!userId) {
  1348. throw new Error("null userId");
  1349. }
  1350. if (!itemId) {
  1351. throw new Error("null itemId");
  1352. }
  1353. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/ThemeVideos");
  1354. return self.ajax({
  1355. type: "GET",
  1356. url: url,
  1357. dataType: "json"
  1358. });
  1359. };
  1360. self.getSearchHints = function (options) {
  1361. var url = self.getUrl("Search/Hints", options);
  1362. return self.ajax({
  1363. type: "GET",
  1364. url: url,
  1365. dataType: "json"
  1366. });
  1367. };
  1368. /**
  1369. * Gets special features for an item
  1370. */
  1371. self.getSpecialFeatures = function (userId, itemId) {
  1372. if (!userId) {
  1373. throw new Error("null userId");
  1374. }
  1375. if (!itemId) {
  1376. throw new Error("null itemId");
  1377. }
  1378. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1379. return self.ajax({
  1380. type: "GET",
  1381. url: url,
  1382. dataType: "json"
  1383. });
  1384. };
  1385. /**
  1386. * Marks an item as played or unplayed
  1387. * This should not be used to update playstate following playback.
  1388. * There are separate playstate check-in methods for that. This should be used for a
  1389. * separate option to reset playstate.
  1390. * @param {String} userId
  1391. * @param {String} itemId
  1392. * @param {Boolean} wasPlayed
  1393. */
  1394. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1395. if (!userId) {
  1396. throw new Error("null userId");
  1397. }
  1398. if (!itemId) {
  1399. throw new Error("null itemId");
  1400. }
  1401. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1402. var method = wasPlayed ? "POST" : "DELETE";
  1403. return self.ajax({
  1404. type: method,
  1405. url: url
  1406. });
  1407. };
  1408. /**
  1409. * Updates a user's favorite status for an item.
  1410. * @param {String} userId
  1411. * @param {String} itemId
  1412. * @param {Boolean} isFavorite
  1413. */
  1414. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1415. if (!userId) {
  1416. throw new Error("null userId");
  1417. }
  1418. if (!itemId) {
  1419. throw new Error("null itemId");
  1420. }
  1421. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1422. var method = isFavorite ? "POST" : "DELETE";
  1423. return self.ajax({
  1424. type: method,
  1425. url: url
  1426. });
  1427. };
  1428. /**
  1429. * Updates a user's personal rating for an item
  1430. * @param {String} userId
  1431. * @param {String} itemId
  1432. * @param {Boolean} likes
  1433. */
  1434. self.updateUserItemRating = function (userId, itemId, likes) {
  1435. if (!userId) {
  1436. throw new Error("null userId");
  1437. }
  1438. if (!itemId) {
  1439. throw new Error("null itemId");
  1440. }
  1441. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1442. likes: likes
  1443. });
  1444. return self.ajax({
  1445. type: "POST",
  1446. url: url
  1447. });
  1448. };
  1449. /**
  1450. * Updates a user's favorite status for an item by name.
  1451. * @param {String} userId
  1452. * @param {String} name
  1453. * @param {Boolean} isFavorite
  1454. */
  1455. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1456. if (!userId) {
  1457. throw new Error("null userId");
  1458. }
  1459. if (!name) {
  1460. throw new Error("null name");
  1461. }
  1462. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1463. var method = isFavorite ? "POST" : "DELETE";
  1464. return self.ajax({
  1465. type: method,
  1466. url: url
  1467. });
  1468. };
  1469. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1470. if (!userId) {
  1471. throw new Error("null userId");
  1472. }
  1473. if (!name) {
  1474. throw new Error("null name");
  1475. }
  1476. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1477. var method = isFavorite ? "POST" : "DELETE";
  1478. return self.ajax({
  1479. type: method,
  1480. url: url
  1481. });
  1482. };
  1483. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1484. if (!userId) {
  1485. throw new Error("null userId");
  1486. }
  1487. if (!name) {
  1488. throw new Error("null name");
  1489. }
  1490. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1491. var method = isFavorite ? "POST" : "DELETE";
  1492. return self.ajax({
  1493. type: method,
  1494. url: url
  1495. });
  1496. };
  1497. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1498. if (!userId) {
  1499. throw new Error("null userId");
  1500. }
  1501. if (!name) {
  1502. throw new Error("null name");
  1503. }
  1504. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1505. var method = isFavorite ? "POST" : "DELETE";
  1506. return self.ajax({
  1507. type: method,
  1508. url: url
  1509. });
  1510. };
  1511. /**
  1512. * Updates a user's rating for an item by name.
  1513. * @param {String} userId
  1514. * @param {String} name
  1515. * @param {Boolean} likes
  1516. */
  1517. self.updateArtistRating = function (userId, name, likes) {
  1518. if (!userId) {
  1519. throw new Error("null userId");
  1520. }
  1521. if (!name) {
  1522. throw new Error("null name");
  1523. }
  1524. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  1525. likes: likes
  1526. });
  1527. return self.ajax({
  1528. type: "POST",
  1529. url: url
  1530. });
  1531. };
  1532. self.updatePersonRating = function (userId, name, likes) {
  1533. if (!userId) {
  1534. throw new Error("null userId");
  1535. }
  1536. if (!name) {
  1537. throw new Error("null name");
  1538. }
  1539. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  1540. likes: likes
  1541. });
  1542. return self.ajax({
  1543. type: "POST",
  1544. url: url
  1545. });
  1546. };
  1547. self.updateStudioRating = function (userId, name, likes) {
  1548. if (!userId) {
  1549. throw new Error("null userId");
  1550. }
  1551. if (!name) {
  1552. throw new Error("null name");
  1553. }
  1554. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  1555. likes: likes
  1556. });
  1557. return self.ajax({
  1558. type: "POST",
  1559. url: url
  1560. });
  1561. };
  1562. self.updateGenreRating = function (userId, name, likes) {
  1563. if (!userId) {
  1564. throw new Error("null userId");
  1565. }
  1566. if (!name) {
  1567. throw new Error("null name");
  1568. }
  1569. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  1570. likes: likes
  1571. });
  1572. return self.ajax({
  1573. type: "POST",
  1574. url: url
  1575. });
  1576. };
  1577. /**
  1578. * Clears a user's rating for an item by name.
  1579. * @param {String} userId
  1580. * @param {String} name
  1581. */
  1582. self.clearArtistRating = function (userId, name) {
  1583. if (!userId) {
  1584. throw new Error("null userId");
  1585. }
  1586. if (!name) {
  1587. throw new Error("null name");
  1588. }
  1589. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1590. return self.ajax({
  1591. type: "DELETE",
  1592. url: url
  1593. });
  1594. };
  1595. self.clearPersonRating = function (userId, name) {
  1596. if (!userId) {
  1597. throw new Error("null userId");
  1598. }
  1599. if (!name) {
  1600. throw new Error("null name");
  1601. }
  1602. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1603. return self.ajax({
  1604. type: "DELETE",
  1605. url: url
  1606. });
  1607. };
  1608. self.clearStudioRating = function (userId, name) {
  1609. if (!userId) {
  1610. throw new Error("null userId");
  1611. }
  1612. if (!name) {
  1613. throw new Error("null name");
  1614. }
  1615. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1616. return self.ajax({
  1617. type: "DELETE",
  1618. url: url
  1619. });
  1620. };
  1621. self.clearGenreRating = function (userId, name) {
  1622. if (!userId) {
  1623. throw new Error("null userId");
  1624. }
  1625. if (!name) {
  1626. throw new Error("null name");
  1627. }
  1628. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1629. return self.ajax({
  1630. type: "DELETE",
  1631. url: url
  1632. });
  1633. };
  1634. /**
  1635. Gets a variety of item counts that a person appears in
  1636. */
  1637. self.getPersonItemCounts = function (userId, name) {
  1638. if (!userId) {
  1639. throw new Error("null userId");
  1640. }
  1641. if (!name) {
  1642. throw new Error("null name");
  1643. }
  1644. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1645. userId: userId
  1646. });
  1647. return self.ajax({
  1648. type: "GET",
  1649. url: url,
  1650. dataType: "json"
  1651. });
  1652. };
  1653. /**
  1654. Gets a variety of item counts that a genre appears in
  1655. */
  1656. self.getGenreItemCounts = function (userId, name) {
  1657. if (!userId) {
  1658. throw new Error("null userId");
  1659. }
  1660. if (!name) {
  1661. throw new Error("null name");
  1662. }
  1663. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1664. userId: userId
  1665. });
  1666. return self.ajax({
  1667. type: "GET",
  1668. url: url,
  1669. dataType: "json"
  1670. });
  1671. };
  1672. /**
  1673. Gets a variety of item counts that an artist appears in
  1674. */
  1675. self.getArtistItemCounts = function (userId, name) {
  1676. if (!userId) {
  1677. throw new Error("null userId");
  1678. }
  1679. if (!name) {
  1680. throw new Error("null name");
  1681. }
  1682. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1683. userId: userId
  1684. });
  1685. return self.ajax({
  1686. type: "GET",
  1687. url: url,
  1688. dataType: "json"
  1689. });
  1690. };
  1691. /**
  1692. Gets a variety of item counts that a studio appears in
  1693. */
  1694. self.getStudioItemCounts = function (userId, name) {
  1695. if (!userId) {
  1696. throw new Error("null userId");
  1697. }
  1698. if (!name) {
  1699. throw new Error("null name");
  1700. }
  1701. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1702. userId: userId
  1703. });
  1704. return self.ajax({
  1705. type: "GET",
  1706. url: url,
  1707. dataType: "json"
  1708. });
  1709. };
  1710. /**
  1711. * Clears a user's personal rating for an item
  1712. * @param {String} userId
  1713. * @param {String} itemId
  1714. */
  1715. self.clearUserItemRating = function (userId, itemId) {
  1716. if (!userId) {
  1717. throw new Error("null userId");
  1718. }
  1719. if (!itemId) {
  1720. throw new Error("null itemId");
  1721. }
  1722. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1723. return self.ajax({
  1724. type: "DELETE",
  1725. url: url
  1726. });
  1727. };
  1728. /**
  1729. * Reports the user has started playing something
  1730. * @param {String} userId
  1731. * @param {String} itemId
  1732. */
  1733. self.reportPlaybackStart = function (userId, itemId) {
  1734. if (!userId) {
  1735. throw new Error("null userId");
  1736. }
  1737. if (!itemId) {
  1738. throw new Error("null itemId");
  1739. }
  1740. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1741. return self.ajax({
  1742. type: "POST",
  1743. url: url
  1744. });
  1745. };
  1746. /**
  1747. * Reports progress viewing an item
  1748. * @param {String} userId
  1749. * @param {String} itemId
  1750. */
  1751. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1752. if (!userId) {
  1753. throw new Error("null userId");
  1754. }
  1755. if (!itemId) {
  1756. throw new Error("null itemId");
  1757. }
  1758. var params = {
  1759. };
  1760. if (positionTicks) {
  1761. params.positionTicks = positionTicks;
  1762. }
  1763. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1764. return self.ajax({
  1765. type: "POST",
  1766. url: url
  1767. });
  1768. };
  1769. /**
  1770. * Reports a user has stopped playing an item
  1771. * @param {String} userId
  1772. * @param {String} itemId
  1773. */
  1774. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1775. if (!userId) {
  1776. throw new Error("null userId");
  1777. }
  1778. if (!itemId) {
  1779. throw new Error("null itemId");
  1780. }
  1781. var params = {
  1782. };
  1783. if (positionTicks) {
  1784. params.positionTicks = positionTicks;
  1785. }
  1786. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1787. return self.ajax({
  1788. type: "DELETE",
  1789. url: url
  1790. });
  1791. };
  1792. }
  1793. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1794. /**
  1795. * Provides a friendly way to create an api client instance using information from the browser's current url
  1796. */
  1797. MediaBrowser.ApiClient.create = function (clientName) {
  1798. var loc = window.location;
  1799. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1800. };
  1801. /**
  1802. *
  1803. * Secure Hash Algorithm (SHA1)
  1804. * http://www.webtoolkit.info/
  1805. *
  1806. **/
  1807. MediaBrowser.SHA1 = function (msg) {
  1808. function rotate_left(n, s) {
  1809. var t4 = (n << s) | (n >>> (32 - s));
  1810. return t4;
  1811. }
  1812. function lsb_hex(val) {
  1813. var str = "";
  1814. var i;
  1815. var vh;
  1816. var vl;
  1817. for (i = 0; i <= 6; i += 2) {
  1818. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1819. vl = (val >>> (i * 4)) & 0x0f;
  1820. str += vh.toString(16) + vl.toString(16);
  1821. }
  1822. return str;
  1823. }
  1824. function cvt_hex(val) {
  1825. var str = "";
  1826. var i;
  1827. var v;
  1828. for (i = 7; i >= 0; i--) {
  1829. v = (val >>> (i * 4)) & 0x0f;
  1830. str += v.toString(16);
  1831. }
  1832. return str;
  1833. }
  1834. function Utf8Encode(string) {
  1835. string = string.replace(/\r\n/g, "\n");
  1836. var utftext = "";
  1837. for (var n = 0; n < string.length; n++) {
  1838. var c = string.charCodeAt(n);
  1839. if (c < 128) {
  1840. utftext += String.fromCharCode(c);
  1841. }
  1842. else if ((c > 127) && (c < 2048)) {
  1843. utftext += String.fromCharCode((c >> 6) | 192);
  1844. utftext += String.fromCharCode((c & 63) | 128);
  1845. }
  1846. else {
  1847. utftext += String.fromCharCode((c >> 12) | 224);
  1848. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1849. utftext += String.fromCharCode((c & 63) | 128);
  1850. }
  1851. }
  1852. return utftext;
  1853. }
  1854. var blockstart;
  1855. var i, j;
  1856. var W = new Array(80);
  1857. var H0 = 0x67452301;
  1858. var H1 = 0xEFCDAB89;
  1859. var H2 = 0x98BADCFE;
  1860. var H3 = 0x10325476;
  1861. var H4 = 0xC3D2E1F0;
  1862. var A, B, C, D, E;
  1863. var temp;
  1864. msg = Utf8Encode(msg);
  1865. var msg_len = msg.length;
  1866. var word_array = new Array();
  1867. for (i = 0; i < msg_len - 3; i += 4) {
  1868. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1869. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1870. word_array.push(j);
  1871. }
  1872. switch (msg_len % 4) {
  1873. case 0:
  1874. i = 0x080000000;
  1875. break;
  1876. case 1:
  1877. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1878. break;
  1879. case 2:
  1880. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1881. break;
  1882. case 3:
  1883. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1884. break;
  1885. }
  1886. word_array.push(i);
  1887. while ((word_array.length % 16) != 14) word_array.push(0);
  1888. word_array.push(msg_len >>> 29);
  1889. word_array.push((msg_len << 3) & 0x0ffffffff);
  1890. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1891. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1892. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1893. A = H0;
  1894. B = H1;
  1895. C = H2;
  1896. D = H3;
  1897. E = H4;
  1898. for (i = 0; i <= 19; i++) {
  1899. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1900. E = D;
  1901. D = C;
  1902. C = rotate_left(B, 30);
  1903. B = A;
  1904. A = temp;
  1905. }
  1906. for (i = 20; i <= 39; i++) {
  1907. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1908. E = D;
  1909. D = C;
  1910. C = rotate_left(B, 30);
  1911. B = A;
  1912. A = temp;
  1913. }
  1914. for (i = 40; i <= 59; i++) {
  1915. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1916. E = D;
  1917. D = C;
  1918. C = rotate_left(B, 30);
  1919. B = A;
  1920. A = temp;
  1921. }
  1922. for (i = 60; i <= 79; i++) {
  1923. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1924. E = D;
  1925. D = C;
  1926. C = rotate_left(B, 30);
  1927. B = A;
  1928. A = temp;
  1929. }
  1930. H0 = (H0 + A) & 0x0ffffffff;
  1931. H1 = (H1 + B) & 0x0ffffffff;
  1932. H2 = (H2 + C) & 0x0ffffffff;
  1933. H3 = (H3 + D) & 0x0ffffffff;
  1934. H4 = (H4 + E) & 0x0ffffffff;
  1935. }
  1936. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1937. return temp.toLowerCase();
  1938. };
  1939. (function (jQuery, window, undefined) {
  1940. "use strict";
  1941. var matched, browser;
  1942. jQuery.uaMatch = function (ua) {
  1943. ua = ua.toLowerCase();
  1944. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  1945. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  1946. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  1947. /(msie) ([\w.]+)/.exec(ua) ||
  1948. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  1949. [];
  1950. var platform_match = /(ipad)/.exec(ua) ||
  1951. /(iphone)/.exec(ua) ||
  1952. /(android)/.exec(ua) ||
  1953. [];
  1954. return {
  1955. browser: match[1] || "",
  1956. version: match[2] || "0",
  1957. platform: platform_match[0] || ""
  1958. };
  1959. };
  1960. matched = jQuery.uaMatch(window.navigator.userAgent);
  1961. browser = {};
  1962. if (matched.browser) {
  1963. browser[matched.browser] = true;
  1964. browser.version = matched.version;
  1965. }
  1966. if (matched.platform) {
  1967. browser[matched.platform] = true
  1968. }
  1969. // Chrome is Webkit, but Webkit is also Safari.
  1970. if (browser.chrome) {
  1971. browser.webkit = true;
  1972. } else if (browser.webkit) {
  1973. browser.safari = true;
  1974. }
  1975. jQuery.browser = browser;
  1976. })(jQuery, window);