Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions installer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ github3.py
wonderwords
pyenchant
halo
spotipy
87 changes: 87 additions & 0 deletions jarviscli/plugins/top_music.py
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'
Comment on lines +5 to +7
Copy link
Collaborator

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.


# 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