ApiClient.js 106 KB

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