ApiClient.js 114 KB

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