ApiClient.js 110 KB

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