ApiClient.js 69 KB

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