ApiClient.js 67 KB

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