vd-plugins/Last.fm/index.js

2 lines
12 KiB
JavaScript
Raw Normal View History

2025-09-27 15:13:04 +02:00
(function(I,A,a,g,h,ee,T,k){"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:"http://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 P(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function U(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function D(t,e,n){return e&&U(t.prototype,e),n&&U(t,n),t}T.findByProps("SET_ACTIVITY");const O=T.findByProps("getAssetIds"),x=T.findByStoreName("SelfPresenceStore"),C=T.findByStoreName("UserStore");function L(){return M(null)}function M(t){c.pluginStopped&&(V(),t=null),c.lastActivity=t,a.FluxDispatcher.dispatch({type:"LOCAL_ACTIVITY_UPDATE",activity:t,pid:2312,socketId:"Last.fm@Vendetta"})}async function Y(t,e=s.APPLICATION_ID){if(!t?.length)return[];try{return await O.fetchAssetIds(e,t)}catch(n){return console.error("[Last.fm] Failed to fetch assets:",n),[]}}const b={};b.componentMountErrors=[],b.componentMountCount=0,b.settingsLoadAttempts=0;function m(t,e){b[t]=e}const F=function(){function t(){P(this,t)}return D(t,[{key:"validateSettings",value:function(){if(!o.username)throw new Error("Last.fm username is not set");if(!o.apiKey)throw new Error("Last.fm API key is not set")}},{key:"handleError",value:async function(e){this.lastError=e.error;const n={2:"Invalid API key",6:"User not found",11:"Service temporarily unavailable",16:"Service temporarily unavailable",29:"Rate limit exceeded"}[e.error]||e.message||"Unknown error";throw h.showToast(`Last.fm Error: ${n}`,g.getAssetIDByName("Small")),new Error(`Last.fm API Error ${e.error}: ${n}`)}},{key:"makeRequest",value:async function(e){const n=new URLSearchParams({...e,api_key:o.apiKey,format:"json"}).toString(),r=await fetch(`${s.LFM_API_BASE_URL}?${n}`),p=await r.json();return(!r.ok||p.error)&&await this.handleError(p),p}},{key:"fetchLatestScrobble",value:async function(){try{this.validateSettings();const e=(await this.makeRequest({method:"user.getrecenttracks",user:o.username,limit:"1",extended:"1"}))?.recenttracks?.track?.[0];if(m("lastAPIResponse",e),!e)throw new Error("No tracks found");this.retryCount=0;const n=!!e["@attr"]?.nowplaying,r=n?Math.floor(Date.now()/1e3):parseInt(e?.date?.uts);let p=null;try{const y=await this.makeRequest({method:"track.getInfo",track:e.name,artist:e.artist.name,username:o.username}),i=parseInt(y?.track?.duration);i>0&&(p=r+Math.floor(i/1e3))}catch(y){console.warn("Failed to fetch track duration",y)}return{name:e.name,artist:e.artist.name,album:e.album["#text"],albumArt:await this.handleAlbumCover(e.image?.find(function(y){return y.size==="large"})?.["#text"]),url:e.url,date:e.date?.["#text"]??"now",nowPlaying:n,loved:e.loved==="1",from:r,to:p}}catch(e){if(this.retryCount++,this.retryCount>s.MAX_RETRY_ATTEMPTS)throw this.retryCount=0,e;return await new Promise(function(n){return setTimeout(n,s.RETRY_DELAY)}),this.fetchLatestScrobble()}}},{key:"handleAlbumCover",value:async function(e){return!e||s.LFM_DEFAULT_COVER_HASHES.some(function(n){return e.includes(n)})?null:e}},{key:"getLastError",value:function(){return this.lastError}},{key:"resetRetryCount",value:function(){this.retryCount=0}}],[{key:"getInstance",value:function(){return t.instance||(t.instance=new t),t.instance}}]),t}().getInstance();function K(t){const e=Math.floor(t/60),n=Math.floor(t%60);return`${e}:${n.toString().padStart(2,"0")}`}var H=function(t){return t[t.PLAYING=0]="PLAYING",t[t.STREAMING=1]="STREAMING",t[t.LISTENING=2]="LISTENING",t[t.COMPETING=5]="COMPETING",t}(H||{});const l=function(...t){return o.verboseLogging&&co