ApiClient.js 67 KB

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