1 line
6.1 KiB
JavaScript
1 line
6.1 KiB
JavaScript
(function(o,w,h,a,u,L,g,S,A){"use strict";const c={DEFAULT_APP_NAME:"Music",DEFAULT_TIME_INTERVAL:5,APPLICATION_ID:"1054951789318909972",LFM_API_KEY:"615322f0047e12aedbc610d9d71f7430",LFM_DEFAULT_COVER_HASHES:["2a96cbd8b46e442fc41c2b86b821562f","c6f59c1e5e7240a4c0d427abd71f3dbb"]},E=h.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 N(s.image[3]["#text"]),url:s.url,date:s.date?.["#text"]??"now",nowPlaying:Boolean(s["@attr"]?.nowplaying),loved:s.loved==="1"}}async function N(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 P(e,t=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")),p(),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 P(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 p(){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!");p();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..."),p())})},(l.timeInterval||c.DEFAULT_TIME_INTERVAL)*1e3)}const{ScrollView:F,TouchableOpacity:C,Text:D}=a.ReactNative,{FormInput:y,FormDivider:d,FormSwitchRow:m,FormText:U,FormIcon:v}=S.Forms;function k(){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(C,{onPress:e},a.React.createElement(U,{style:{marginRight:12}},"UPDATE"))}function V(){const e=L.useProxy(u.storage),t=a.NavigationNative.useNavigation();return a.React.useEffect(function(){t.setOptions({title:"Last.fm Configuration",headerRight:k})},[]),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,{required:!0,value:e.username||void 0,onChangeText:function(n){return e.username=n.trim()},title:"Last.fm username",helpText:!e.username&&a.React.createElement(D,{style:{color:"#FF0000"}},"This field is required!"),placeholder:"wumpus123",returnKeyType:"done"}),a.React.createElement(d,null),a.React.createElement(y,{value:e.timeInterval,onChangeText:function(n){return e.timeInterval=Number(n)},title:"Update interval (in seconds)",placeholder:c.DEFAULT_TIME_INTERVAL.toString(),keyboardType:"numeric",returnKeyType:"done"}),a.React.createElement(d,null),a.React.createElement(m,{label:"Show time elapsed",subLabel:"Show the time elapsed since the song started playing",leading:a.React.createElement(v,{source:g.getAssetIDByName("clock")}),value:e.showTimestamp,onValueChange:function(n){return e.showTimestamp=n}}),a.React.createElement(d,null),a.React.createElement(m,{label:"Set status as listening",subLabel:'Set your status as "Listening to" instead of "Playing"',leading:a.React.createElement(v,{source:g.getAssetIDByName("ic_headset_neutral")}),value:e.listeningTo,onValueChange:function(n){return e.listeningTo=n}}),a.React.createElement(d,null),a.React.createElement(m,{label:"Verbose logging",subLabel:"Log more information to the console for debugging purposes",leading:a.React.createElement(v,{source:g.getAssetIDByName("pencil")}),value:e.verboseLogging,onValueChange:function(n){return e.verboseLogging=n}}))}const r={},l={...w.plugin.storage},_=h.findByStoreName("UserStore"),M=h.findByProps("SET_ACTIVITY").SET_ACTIVITY,i=function(...e){return l.verboseLogging&&console.log(...e)};var O={onLoad:function(){console.log("Starting last.fm plugin.."),r.pluginStopped=!1,_.getCurrentUser()&&(console.log("User is already logged in, initializing..."),T().catch(console.error)),a.FluxDispatcher.subscribe("CONNECTION_OPEN",function(){T().catch(console.error)})},onUnload:function(){console.log("Stopping last.fm..."),r.pluginStopped=!0,p()},settings:V};return o.SET_ACTIVITY=M,o.UserStore=_,o.currentSettings=l,o.default=O,o.global=r,o.verboseLog=i,Object.defineProperty(o,"__esModule",{value:!0}),o})({},vendetta,vendetta.metro,vendetta.metro.common,vendetta.plugin,vendetta.storage,vendetta.ui.assets,vendetta.ui.components,vendetta.ui.toasts);
|