vd-plugins/Last.fm/index.js

2 lines
6.3 KiB
JavaScript
Raw Normal View History

(function(o,_,p,a,u,L,g,S,A){"use strict";function N(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}const c={DEFAULT_APP_NAME:"Music",DEFAULT_TIME_INTERVAL:5,APPLICATION_ID:"1054951789318909972",LFM_API_KEY:"615322f0047e12aedbc610d9d71f7430",LFM_DEFAULT_COVER_HASHES:["2a96cbd8b46e442fc41c2b86b821562f","c6f59c1e5e7240a4c0d427abd71f3dbb"]},E=p.findByProps("getAssetIds");async function R(){const e=new URLSearchParams({method:"user.getrecenttracks",user:l.username,api_key:c.LFM_API_KEY,format:"json",limit:"1",extended:"1"}).toString(),t=await fetch(`https://ws.audioscrobbler.com/2.0/?${e}`);if(!t.ok)throw new Error(`Failed to fetch the latest scrobble: ${t.statusText}`);const n=await t.json(),s=n?.recenttracks?.track?.[0];if(!s)throw n;return{name:s.name,artist:s.artist.name,album:s.album["#text"],albumArt:await P(s.image[3]["#text"]),url:s.url,date:s.date?.["#text"]??"now",nowPlaying:!!s["@attr"]?.nowplaying,loved:s.loved==="1"}}async function P(e){return c.LFM_DEFAULT_COVER_HASHES.some(function(t){return e.includes(t)})?null:e}function f(){return r.lastActivity&&i("--> Clearing activity..."),I(null)}function I(e){r.pluginStopped&&(console.log("--> Plugin is unloaded, aborting..."),r.updateInterval&&clearInterval(r.updateInterval),e=null),r.lastActivity=e,a.FluxDispatcher.dispatch({type:"LOCAL_ACTIVITY_UPDATE",activity:e})}async function C(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:c.APPLICATION_ID;return e?typeof e=="string"?(await E.getAssetIds(t,[e]))[0]:await E.getAssetIds(t,e):null}async function b(){if(i("--> Fetching last track..."),!l.username)throw A.showToast("Last.fm username is not set!",g.getAssetIDByName("Small")),h(),new Error("Username is not set");const e=await R().catch(async function(n){throw i("--> An error occurred while fetching the last track, aborting..."),f(),n});if(!e.nowPlaying){i("--> Last track is not currently playing, aborting..."),f();return}if(i("--> Track fetched!"),r.lastTrackUrl===e.url){i("--> Last track is the same as the previous one, aborting...");return}const t={name:l.appName||c.DEFAULT_APP_NAME,flags:0,type:l.listeningTo?2:0,details:e.name,state:`by ${e.artist}`,application_id:c.APPLICATION_ID};if(r.lastTrackUrl=e.url,t.name.includes("{{"))for(const n in e)t.name=t.name.replace(`{{${n}}}`,e[n]);l.showTimestamp&&(t.timestamps={start:Date.now()/1e3|0}),e.album&&(t.assets={large_image:await C(e.albumArt),large_text:`on ${e.album}`}),i("--> Setting activity..."),i(t);try{I(t)}catch(n){throw i("--> An error occurred while setting the activity"),f(),n}i("--> Successfully set activity!")}function h(){console.log("--> Flushing..."),r.lastActivity=null,r.lastTrackUrl=null,r.updateInterval&&clearInterval(r.updateInterval),f()}async function T(){if(console.log("--> Initializing..."),r.pluginStopped)throw new Error("Plugin is already stopped!");h();let e=0;await b().catch(function(t){console.error(t),e++}),r.updateInterval=setInterval(function(){return b().then(function(){e=0}).catch(function(t){console.error(t),++e>3&&(console.error("Failed to fetch/set activity 3 times, aborting..."),h())})},(l.timeInterval||c.DEFAULT_TIME_INTERVAL)*1e3)}const{ScrollView:F,TouchableOpacity:D,Text:U}=a.ReactNative,{FormInput:y,FormDivider:d,FormSwitchRow:m,FormText:k,FormIcon:v}=S.Forms;function V(){async function e(){for(const t in u.storage)(u.storage[t]===!1||u.storage[t])&&(l[t]=u.storage[t]);console.log("Applying settings..."),await T(),A.showToast("Settings updated!",g.getAssetIDByName("Check"))}return a.React.createElement(D,{onPress:e},a.React.createElement(k,{style:{marginRight:12}},"UPDATE"))}function M(){const e=L.useProxy(u.storage),t=a.NavigationNative.useNavigation();return a.React.useEffect(function(){t.setOptions({title:"Last.fm Configuration",headerRight:V})},[]),a.React.createElement(F,null,a.React.createElement(y,{value:e.appName||void 0,onChangeText:function(n){return e.appName=n.trim()},title:"Discord Application Name",placeholder:c.DEFAULT_APP_NAME,returnKeyType:"done"}),a.React.createElement(d,null),a.React.createElement(y,{requi