1 line
7.5 KiB
JavaScript
1 line
7.5 KiB
JavaScript
(function(c,b,y,a,f,P,u,R,_,C,I){"use strict";function F(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}function w(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function D(e,n,t){return n&&w(e.prototype,n),t&&w(e,t),e}function U(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}const o={DEFAULT_APP_NAME:"Music",DEFAULT_TIME_INTERVAL:5,APPLICATION_ID:"1054951789318909972",LFM_API_KEY:"615322f0047e12aedbc610d9d71f7430",LFM_DEFAULT_COVER_HASHES:["2a96cbd8b46e442fc41c2b86b821562f","c6f59c1e5e7240a4c0d427abd71f3dbb"]},L=y.findByProps("getAssetIds"),k=y.findByStoreName("PresenceStore");var T;(function(e){e[e.PLAYING=0]="PLAYING",e[e.STREAMING=1]="STREAMING",e[e.LISTENING=2]="LISTENING",e[e.COMPETING=5]="COMPETING"})(T||(T={}));async function O(){const e=new URLSearchParams({method:"user.getrecenttracks",user:s.username,api_key:o.LFM_API_KEY,format:"json",limit:"1",extended:"1"}).toString(),n=await fetch(`https://ws.audioscrobbler.com/2.0/?${e}`);if(!n.ok)throw new Error(`Failed to fetch the latest scrobble: ${n.statusText}`);const t=await n.json(),r=t?.recenttracks?.track?.[0];if(!r)throw t;return{name:r.name,artist:r.artist.name,album:r.album["#text"],albumArt:await M(r.image[3]["#text"]),url:r.url,date:r.date?.["#text"]??"now",nowPlaying:!!r["@attr"]?.nowplaying,loved:r.loved==="1"}}async function M(e){return o.LFM_DEFAULT_COVER_HASHES.some(function(n){return e.includes(n)})?null:e}function d(){return i.lastActivity&&l("--> Clearing activity..."),S(null)}function S(e){i.pluginStopped&&(console.log("--> Plugin is unloaded, aborting..."),i.updateInterval&&clearInterval(i.updateInterval),e=null),i.lastActivity=e,a.FluxDispatcher.dispatch({type:"LOCAL_ACTIVITY_UPDATE",activity:e,pid:2312,socketId:"Last.fm@Vendetta"})}async function V(e){let n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:o.APPLICATION_ID;if(!e)return[];const t=L.getAssetIds(n,e);return t.length>0?t:await L.fetchAssetIds(n,e)}async function N(){if(l("--> Fetching last track..."),!s.username)throw _.showToast("Last.fm username is not set!",u.getAssetIDByName("Small")),h(),new Error("Username is not set");if(s.ignoreSpotify){for(const t of k.getActivities(A.getCurrentUser().id))if(t?.type===T.LISTENING&&t.application_id!==o.APPLICATION_ID){l("--> Spotify is currently playing, aborting..."),d();return}}const e=await O().catch(async function(t){throw l("--> An error occurred while fetching the last track, aborting..."),d(),t});if(!e.nowPlaying){l("--> Last track is not currently playing, aborting..."),d();return}if(l("--> Track fetched!"),i.lastTrackUrl===e.url){l("--> Last track is the same as the previous one, aborting...");return}const n={name:s.appName||o.DEFAULT_APP_NAME,flags:0,type:s.listeningTo?2:0,details:e.name,state:`by ${e.artist}`,application_id:o.APPLICATION_ID};if(i.lastTrackUrl=e.url,n.name.includes("{{"))for(const t in e)n.name=n.name.replace(`{{${t}}}`,e[t]);s.showTimestamp&&(n.timestamps={start:Date.now()/1e3|0}),e.album&&(n.assets={large_image:(await V([e.albumArt]))[0],large_text:`on ${e.album}`}),l("--> Setting activity..."),l(n);try{S(n)}catch(t){throw l("--> An error occurred while setting the activity"),d(),t}l("--> Successfully set activity!")}function h(){console.log("--> Flushing..."),i.lastActivity=null,i.lastTrackUrl=null,i.updateInterval&&clearInterval(i.updateInterval),d()}async function E(){if(console.log("--> Initializing..."),i.pluginStopped)throw new Error("Plugin is already stopped!");h();let e=0;await N().catch(function(n){console.error(n),e++}),i.updateInterval=setInterval(function(){return N().then(function(){e=0}).catch(function(n){console.error(n),++e>3&&(console.error("Failed to fetch/set activity 3 times, aborting..."),h())})},(Number(s.timeInterval)||o.DEFAULT_TIME_INTERVAL)*1e3)}const{FormInput:v,FormDivider:g,FormSwitchRow:p,FormText:B,FormIcon:m}=R.Forms;function x(){async function e(){for(const n in f.storage)(f.storage[n]===!1||f.storage[n])&&(s[n]=f.storage[n]);console.log("Applying settings..."),await E(),_.showToast("Settings updated!",u.getAssetIDByName("Check"))}return a.React.createElement(I.TouchableOpacity,{onPress:e},a.React.createElement(B,{style:{marginRight:12}},"UPDATE"))}function G(){const e=P.useProxy(f.storage),n=a.NavigationNative.useNavigation();return C.useEffect(function(){n.setOptions({title:"Last.fm Configuration",headerRight:x})},[]),a.React.createElement(I.ScrollView,null,a.React.createElement(v,{value:e.appName||void 0,onChangeText:function(t){return e.appName=t.trim()},title:"Discord Application Name",placeholder:o.DEFAULT_APP_NAME,returnKeyType:"done"}),a.React.createElement(g,null),a.React.createElement(v,{required:!0,value:e.username||void 0,onChangeText:function(t){return e.username=t.trim()},title:"Last.fm username",helpText:!e.username&&a.React.createElement(I.Text,{style:{color:"#FF0000"}},"This field is required!"),placeholder:"wumpus123",returnKeyType:"done"}),a.React.createElement(g,null),a.React.createElement(v,{value:e.timeInterval,onChangeText:function(t){return e.timeInterval=t},title:"Update interval (in seconds)",placeholder:o.DEFAULT_TIME_INTERVAL.toString(),keyboardType:"numeric",returnKeyType:"done"}),a.React.createElement(g,null),a.React.createElement(p,{label:"Show time elapsed",subLabel:"Show the time elapsed since the song started playing",leading:a.React.createElement(m,{source:u.getAssetIDByName("clock")}),value:e.showTimestamp,onValueChange:function(t){return e.showTimestamp=t}}),a.React.createElement(g,null),a.React.createElement(p,{label:"Set status as listening",subLabel:'Set your status as "Listening to" instead of "Playing"',leading:a.React.createElement(m,{source:u.getAssetIDByName("ic_headset_neutral")}),value:e.listeningTo,onValueChange:function(t){return e.listeningTo=t}}),a.React.createElement(g,null),a.React.createElement(p,{label:"Hide when Spotify is running",subLabel:"Hide the status when a Spotify activity is detected",leading:a.React.createElement(m,{source:u.getAssetIDByName("img_account_sync_spotify_light_and_dark")}),value:e.ignoreSpotify,onValueChange:function(t){return e.ignoreSpotify=t}}),a.React.createElement(g,null),a.React.createElement(p,{label:"Verbose logging",subLabel:"Log more information to the console for debugging purposes",leading:a.React.createElement(m,{source:u.getAssetIDByName("pencil")}),value:e.verboseLogging,onValueChange:function(t){return e.verboseLogging=t}}))}const i={};b.plugin.storage.ignoreSpotify??=!0;const s={...b.plugin.storage},A=y.findByStoreName("UserStore"),{SET_ACTIVITY:Y}=y.findByProps("SET_ACTIVITY"),l=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return s.verboseLogging&&console.log(...n)};var $=new(function(){function e(){F(this,e),U(this,"settings",G)}return D(e,[{key:"onLoad",value:function(){console.log("Starting last.fm plugin.."),i.pluginStopped=!1,A.getCurrentUser()&&(console.log("User is already logged in, initializing..."),E().catch(console.error)),a.FluxDispatcher.subscribe("CONNECTION_OPEN",function(){E().catch(console.error)})}},{key:"onUnload",value:function(){console.log("Stopping last.fm..."),i.pluginStopped=!0,h()}}]),e}());return c.SET_ACTIVITY=Y,c.UserStore=A,c.currentSettings=s,c.default=$,c.global=i,c.verboseLog=l,Object.defineProperty(c,"__esModule",{value:!0}),c})({},vendetta,vendetta.metro,vendetta.metro.common,vendetta.plugin,vendetta.storage,vendetta.ui.assets,vendetta.ui.components,vendetta.ui.toasts,window.React,vendetta.metro.common.ReactNative);
|