ApiClient.js 112 KB

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