ApiClient.js 84 KB

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