-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Creation of top_music.py plugin #1241
Open
SethSainz
wants to merge
1
commit into
sukeesh:master
Choose a base branch
from
SethSainz:master
base: master
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.
Open
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -53,3 +53,4 @@ github3.py | |
wonderwords | ||
pyenchant | ||
halo | ||
spotipy |
This file contains 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,87 @@ | ||
from plugin import plugin, require | ||
import spotipy | ||
from spotipy.oauth2 import SpotifyClientCredentials | ||
|
||
# Set up your credentials | ||
client_id = '9432469329f94c449bc36b3273f7285a' | ||
client_secret = '35d017ceebeb4eea83cd74ff5ee091ac' | ||
|
||
# Set up the Spotify client | ||
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) | ||
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) | ||
|
||
@require(network=True) | ||
@plugin("topmusic") | ||
class topmusic: | ||
""" | ||
Plugin to extract top music tracks by genre using Spotify API | ||
""" | ||
|
||
def __call__(self, jarvis, genre): | ||
valid_genre_list = [ | ||
"pop", "rock", "hip-hop", "r&b", "country", "electronic", "jazz", | ||
"classical", "latin", "indie", "alternative", "metal", "blues", "reggae", | ||
"folk", "punk", "k-pop", "ambient", "edm", "trap", "lo-fi", | ||
"synthwave", "indie rock", "pop rock", "r&b", "reggaeton", "grunge", "disco" | ||
] | ||
|
||
genre = genre.lower() | ||
|
||
if genre == "help": | ||
jarvis.say("List of possible top music genres: \n") | ||
index = 1 | ||
for genre in valid_genre_list: | ||
jarvis.say(f"{index}. {genre}") | ||
index += 1 | ||
jarvis.say("") | ||
elif genre not in valid_genre_list: | ||
jarvis.say("\nPlease run the command with a valid genre.\nValid input examples: topmusic jazz, topmusic pop\n\nType \"topmusic help\" for a list of music genres.\n") | ||
else: | ||
top_tracks = self.get_top_tracks_by_genre(jarvis, genre) | ||
if top_tracks: | ||
jarvis.say(f"Top 10 {genre.capitalize()} tracks:") | ||
for i, track in enumerate(top_tracks, 1): | ||
jarvis.say(f"{i}. {track['name']} by {track['artist']} (Album: {track['album']})") | ||
else: | ||
jarvis.say(f"No tracks found for genre: {genre}") | ||
|
||
def get_top_tracks_by_genre(self, jarvis, genre): | ||
limit = 10 | ||
search_queries = [ | ||
f'genre:{genre}', | ||
genre, | ||
f'{genre} playlist' | ||
] | ||
|
||
playlist_id = None | ||
for query in search_queries: | ||
try: | ||
results = sp.search(q=query, type='playlist', limit=1) | ||
if results and 'playlists' in results and results['playlists']['items']: | ||
playlist_id = results['playlists']['items'][0]['id'] | ||
break | ||
except Exception as e: | ||
jarvis.say(f"Error during search: {e}") | ||
|
||
if not playlist_id: | ||
jarvis.say(f"No playlists found for genre: {genre}") | ||
return [] | ||
|
||
try: | ||
tracks = sp.playlist_tracks(playlist_id, limit=limit) | ||
except Exception as e: | ||
jarvis.say(f"Error occurred while fetching playlist tracks: {e}") | ||
return [] | ||
|
||
top_tracks = [] | ||
for item in tracks['items']: | ||
track = item['track'] | ||
if track: | ||
track_info = { | ||
'name': track['name'], | ||
'artist': track['artists'][0]['name'] if track['artists'] else 'Unknown Artist', | ||
'album': track['album']['name'] if track['album'] else 'Unknown Album' | ||
} | ||
top_tracks.append(track_info) | ||
|
||
return top_tracks |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't put this in code. Please generate a new secret + make this secret invalid. We don't host credentials - features that need api key are allowed, but the user must provide an own api key.