ApiClient.js 100 KB

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