diff --git a/README.md b/README.md index eec0786..1f090bb 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Missing features - [x] Horoscope - [x] "Journee mondiale" - [x] Mug -- [ ] Music +- [x] Music - [x] Opinions - [x] Translate - [x] Wikipedia: find definition, get random page diff --git a/config.json.example b/config.json.example index e12a6e9..f749b86 100644 --- a/config.json.example +++ b/config.json.example @@ -71,6 +71,9 @@ "calmed_message": "I'm calm now.", "pissed_message": "I'm pissed off now..." }, + "music": { + "commands": ["music", "album"], + }, "notes": { "commands": ["note down", "notes"], "content_regex": "for (?P\\S+) (?P.+)", diff --git a/edmond/plugins/music.py b/edmond/plugins/music.py new file mode 100644 index 0000000..9f861a6 --- /dev/null +++ b/edmond/plugins/music.py @@ -0,0 +1,66 @@ +import random + +try: + from scaruffi.api import ScaruffiApi + DEPENDENCIES_FOUND = True +except ImportError: + DEPENDENCIES_FOUND = False + +from edmond.plugin import Plugin + + +class MusicPlugin(Plugin): + """Get some good music for your friends using the scaruffi library. + + The first command returns a random good album. The second command allows you + to choose the decade: you can say 1960, 60, 60s or 60's, all of these will + return an album from the sixties. + """ + + REQUIRED_CONFIGS = ["commands"] + + DECADES = [60, 70, 80, 90, 00, 10] + + def __init__(self, bot): + super().__init__(bot) + self.api = ScaruffiApi() + + def on_pubmsg(self, event): + if not self.should_handle_command(event.arguments[0]): + return False + + if self.command.ident == self.config["commands"][0]: + decade = random.choice(self.DECADES) + return self.tell_album_from_decade(event.target, decade) + elif self.command.ident == self.config["commands"][1]: + decade = self.parse_decade(self.command.content) + if decade is None: + self.signal_failure(event.target) + return False + return self.tell_album_from_decade(event.target, decade) + return False + + def tell_album_from_decade(self, target, decade): + album = self.get_random_album(decade) + if not album: + self.signal_failure(target) + return False + self.bot.say(target, album) + return True + + def get_random_album(self, decade): + """Return an album as str from this decade from Scaruffi website.""" + ratings = self.api.get_ratings(decade) + if not ratings: + return None + rating = random.choice(list(ratings.keys())) + album = random.choice(ratings[rating]) + return f"{album.artist} - {album.title} ({album.year})" + + def parse_decade(self, text): + """Return decade text as an int if possible, else None. Strip "'s".""" + text = text.lower().rstrip("'s") + try: + return int(text) + except ValueError: + return None diff --git a/edmond/plugins/requirements.txt b/edmond/plugins/requirements.txt index 5daa7ad..50c6209 100644 --- a/edmond/plugins/requirements.txt +++ b/edmond/plugins/requirements.txt @@ -9,3 +9,6 @@ google-api-python-client # Translator translate + +# Music +scaruffi==0.0.2