ApiClient.js 92 KB

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