ApiClient.js 114 KB

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