ApiClient.js 92 KB

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