ApiClient.js 103 KB

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