-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(Spotify): Add Activity #10341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yashh56
wants to merge
6
commits into
PreMiD:main
Choose a base branch
from
Yashh56:feat/Spotify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−572
Open
feat(Spotify): Add Activity #10341
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e9341a
feat: add Spotify presence integration with track details, timestamps…
Yashh56 dc63ff3
updated the logo and imported the neccessary methods
Yashh56 6917ab5
Fix import statement formatting in presence.ts
Yashh56 83a1c5f
fix: merged spotify and spotify podcasts and enabled toggle button wh…
Yashh56 092e694
fix: added Spotify.json and changed music to song
Yashh56 5495c77
fix: update the spotify.json
Yashh56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| { | ||
| "$schema": "https://schemas.premid.app/metadata/1.16", | ||
| "apiVersion": 1, | ||
| "author": { | ||
| "name": "itsyashbtw", | ||
| "id": "568021861871517716" | ||
| }, | ||
| "service": "Spotify", | ||
| "altnames": [ | ||
| "Spotify Web" | ||
| ], | ||
| "description": { | ||
| "en": "Spotify is a digital music, podcast, and video service that gives you access to millions of songs and other content from creators all over the world." | ||
| }, | ||
| "url": "open.spotify.com", | ||
| "regExp": "^https?[:][/][/]open[.]spotify[.]com[/]", | ||
| "version": "1.0.0", | ||
| "logo": "https://cdn.rcd.gg/PreMiD/websites/S/Spotify%20Podcasts/assets/logo.png", | ||
| "thumbnail": "https://cdn.rcd.gg/PreMiD/websites/S/Spotify%20Podcasts/assets/thumbnail.png", | ||
| "color": "#1DB954", | ||
| "category": "music", | ||
| "tags": [ | ||
| "music", | ||
| "streaming", | ||
| "audio" | ||
| ], | ||
| "settings": [ | ||
| { | ||
| "id": "lang", | ||
| "multiLanguage": true | ||
| }, | ||
| { | ||
| "id": "privacy", | ||
| "title": "Privacy Mode", | ||
| "icon": "fad fa-user-secret", | ||
| "value": false | ||
| }, | ||
| { | ||
| "id": "timestamps", | ||
| "title": "Show timestamps", | ||
| "icon": "fad fa-stopwatch", | ||
| "value": true | ||
| }, | ||
| { | ||
| "id": "cover", | ||
| "title": "Show Cover Art", | ||
| "icon": "fad fa-images", | ||
| "value": true | ||
| }, | ||
| { | ||
| "id": "buttons", | ||
| "title": "Show Buttons", | ||
| "icon": "fad fa-compress-arrows-alt", | ||
| "value": true | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { ActivityType, Assets, getTimestamps,timestampFromFormat } from 'premid' | ||
|
|
||
| const presence = new Presence({ | ||
| clientId: '1458014647193047042', | ||
| }) | ||
|
|
||
| enum ActivityAssets { | ||
| Logo = 'https://cdn.rcd.gg/PreMiD/websites/S/Spotify%20Podcasts/assets/logo.png', | ||
| } | ||
|
|
||
| async function getStrings() { | ||
| return presence.getStrings({ | ||
| play: 'general.playing', | ||
| pause: 'general.paused', | ||
| }) | ||
| } | ||
|
|
||
| let strings: Awaited<ReturnType<typeof getStrings>> | ||
| let oldLang: string | null = null | ||
|
|
||
| presence.on('UpdateData', async () => { | ||
| const [newLang, privacy, timestamps, cover, buttons] = await Promise.all([ | ||
| presence.getSetting<string>('lang').catch(() => 'en'), | ||
| presence.getSetting<boolean>('privacy'), | ||
| presence.getSetting<boolean>('timestamps'), | ||
| presence.getSetting<boolean>('cover'), | ||
| presence.getSetting<boolean>('buttons'), | ||
| ]) | ||
|
|
||
| if (oldLang !== newLang || !strings) { | ||
| oldLang = newLang | ||
| strings = await getStrings() | ||
| } | ||
|
|
||
| // Check if music is playing | ||
| const playPauseButton = document.querySelector('[data-testid=control-button-playpause]') | ||
| const isPlaying = playPauseButton?.getAttribute('aria-label')?.toLowerCase().includes('pause') | ||
|
|
||
| // Only show presence when music is playing | ||
| if (!isPlaying) { | ||
| presence.clearActivity() | ||
| return | ||
| } | ||
|
|
||
| // Get track info | ||
| const trackName = document.querySelector( | ||
| '[data-testid="context-item-link"], [data-testid="nowplaying-track-link"]', | ||
| )?.textContent | ||
|
|
||
| const artistName = document.querySelector( | ||
| '[data-testid="context-item-info-artist"], [data-testid="track-info-artists"]', | ||
| )?.textContent | ||
|
|
||
| if (!trackName) { | ||
| presence.clearActivity() | ||
| return | ||
| } | ||
|
|
||
| const presenceData: PresenceData = { | ||
| largeImageKey: ActivityAssets.Logo, | ||
| type: ActivityType.Listening, | ||
| details: trackName, | ||
| state: artistName || 'Unknown Artist', | ||
| smallImageKey: Assets.Play, | ||
| smallImageText: strings.play, | ||
| } | ||
|
|
||
| // Get timestamps | ||
| if (timestamps) { | ||
| const currentTime = document.querySelector('[data-testid="playback-position"]')?.textContent | ||
| const duration = document.querySelector('[data-testid="playback-duration"]')?.textContent | ||
|
|
||
| if (currentTime && duration) { | ||
| [presenceData.startTimestamp, presenceData.endTimestamp] = getTimestamps( | ||
| timestampFromFormat(currentTime), | ||
| timestampFromFormat(duration), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Get cover art | ||
| if (cover) { | ||
| const albumCover = document.querySelector<HTMLAnchorElement>( | ||
| ':is(a[data-testid=cover-art-link], a[data-testid=context-link])', | ||
| ) | ||
| const coverImg = albumCover?.querySelector('img')?.src | ||
| if (coverImg) { | ||
| presenceData.largeImageKey = coverImg | ||
| presenceData.smallImageKey = ActivityAssets.Logo | ||
| } | ||
| } | ||
|
|
||
| // Add button | ||
| if (buttons) { | ||
| presenceData.buttons = [ | ||
| { label: 'Listen on Spotify', url: document.location.href }, | ||
| ] | ||
| } | ||
|
|
||
| // Apply privacy mode | ||
| if (privacy) { | ||
| presenceData.details = 'Listening to music' | ||
| delete presenceData.state | ||
| presenceData.largeImageKey = ActivityAssets.Logo | ||
| delete presenceData.smallImageKey | ||
| } | ||
|
|
||
| presence.setActivity(presenceData) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.