ApiClient.js 100 KB

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