ApiClient.js 113 KB

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