ApiClient.js 114 KB

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