vd-plugins/Last.fm/index.js

2 lines
12 KiB
JavaScript
Raw Normal View History

2025-09-19 20:45:44 +02:00
(function(d,S,O,f,p,a,h,g,V,x){"use strict";const s={DEFAULT_APP_NAME:"Music",DEFAULT_TIME_INTERVAL:5,APPLICATION_ID:"1054951789318909972",MIN_UPDATE_INTERVAL:3,MAX_RETRY_ATTEMPTS:3,RETRY_DELAY:5e3,LFM_API_BASE_URL:"https://ws.audioscrobbler.com/2.0",LFM_DEFAULT_COVER_HASHES:["2a96cbd8b46e442fc41c2b86b821562f","c6f59c1e5e7240a4c0d427abd71f3dbb"],DEFAULT_SETTINGS:{username:"",apiKey:"",appName:"Music",timeInterval:5,showTimestamp:!0,listeningTo:!0,ignoreSpotify:!0,verboseLogging:!1},GITHUB_URL:"https://github.com/kmmiio99o/letup",GITHUB_COMMITS_URL:"https://github.com/kmmiio99o/letup/commits/main/"};function b(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _(e,t){for(var n=0;n<t.length;n++){var c=t[n];c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(e,c.key,c)}}function N(e,t,n){return t&&_(e.prototype,t),n&&_(e,n),e}g.findByProps("SET_ACTIVITY");const B=g.findByProps("getAssetIds"),$=g.findByStoreName("SelfPresenceStore"),P=g.findByStoreName("UserStore");function v(){return k(null)}function k(e){o.pluginStopped&&(F(),e=null),o.lastActivity=e,a.FluxDispatcher.dispatch({type:"LOCAL_ACTIVITY_UPDATE",activity:e,pid:2312,socketId:"Last.fm@Vendetta"})}async function Y(e,t=s.APPLICATION_ID){if(!e?.length)return[];try{return await B.fetchAssetIds(t,e)}catch(n){return console.error("[Last.fm] Failed to fetch assets:",n),[]}}const A={};A.componentMountErrors=[],A.componentMountCount=0,A.settingsLoadAttempts=0;function T(e,t){A[e]=t}const U=function(){function e(){b(this,e)}return N(e,[{key:"validateSettings",value:function(){if(!i.username)throw new Error("Last.fm username is not set");if(!i.apiKey)throw new Error("Last.fm API key is not set")}},{key:"handleError",value:async function(t){this.lastError=t.error;const n={2:"Invalid API key",6:"User not found",11:"Service temporarily unavailable",16:"Service temporarily unavailable",29:"Rate limit exceeded"}[t.error]||t.message||"Unknown error";throw p.showToast(`Last.fm Error: ${n}`,f.getAssetIDByName("Small")),new Error(`Last.fm API Error ${t.error}: ${n}`)}},{key:"makeRequest",value:async function(t){const n=new URLSearchParams({...t,api_key:i.apiKey,format:"json"}).toString(),c=await fetch(`${s.LFM_API_BASE_URL}?${n}`),r=await c.json();return(!c.ok||r.error)&&await this.handleError(r),r}},{key:"fetchLatestScrobble",value:async function(){try{this.validateSettings();const t=(await this.makeRequest({method:"user.getrecenttracks",user:i.username,limit:"1",extended:"1"}))?.recenttracks?.track?.[0];if(T("lastAPIResponse",t),!t)throw new Error("No tracks found");return this.retryCount=0,{name:t.name,artist:t.artist.name,album:t.album["#text"],albumArt:await this.handleAlbumCover(t.image?.find(function(n){return n.size==="large"})?.["#text"]),url:t.url,date:t.date?.["#text"]??"now",nowPlaying:!!t["@attr"]?.nowplaying,loved:t.loved==="1"}}catch(t){if(this.retryCount++,this.retryCount>s.MAX_RETRY_ATTEMPTS)throw this.retryCount=0,t;return await new Promise(function(n){return setTimeout(n,s.RETRY_DELAY)}),this.fetchLatestScrobble()}}},{key:"handleAlbumCover",value:async function(t){return!t||s.LFM_DEFAULT_COVER_HASHES.some(function(n){return t.includes(n)})?null:t}},{key:"getLastError",value:function(){return this.lastError}},{key:"resetRetryCount",value:function(){this.retryCount=0}}],[{key:"getInstance",value:function(){return e.instance||(e.instance=new e),e.instance}}]),e}().getInstance();var K=function(e){return e[e.PLAYING=0]="PLAYING",e[e.STREAMING=1]="STREAMING",e[e.LISTENING=2]="LISTENING",e[e.COMPETING=5]="COMPETING",e}(K||{});const l=function(...e){return i.verboseLogging&&console.log("[Last.fm]",...e)},C=function(){function e(){b(this,e)}return N(e,[{key:"updateActivity",value:async function(){if(o.pluginStopped){l("Plugin is stopped, skipping update"),this.stopUpdates();return}l("Fetching last track...");try{if(!i.username||!i.apiKey)throw new Error("Username or API key not set");if(i.ignoreSpotify){if($.findActivity(function(c){return c.sync_id})){l("Spotify is currently playing, cl