music: add plugin using scaruffi library

This commit is contained in:
dece 2020-11-05 22:31:24 +01:00
parent 93eddad240
commit 7cbcd5eb85
4 changed files with 73 additions and 1 deletions

View file

@ -35,7 +35,7 @@ Missing features
- [x] Horoscope - [x] Horoscope
- [x] "Journee mondiale" - [x] "Journee mondiale"
- [x] Mug - [x] Mug
- [ ] Music - [x] Music
- [x] Opinions - [x] Opinions
- [x] Translate - [x] Translate
- [x] Wikipedia: find definition, get random page - [x] Wikipedia: find definition, get random page

View file

@ -71,6 +71,9 @@
"calmed_message": "I'm calm now.", "calmed_message": "I'm calm now.",
"pissed_message": "I'm pissed off now..." "pissed_message": "I'm pissed off now..."
}, },
"music": {
"commands": ["music", "album"],
},
"notes": { "notes": {
"commands": ["note down", "notes"], "commands": ["note down", "notes"],
"content_regex": "for (?P<target>\\S+) (?P<note>.+)", "content_regex": "for (?P<target>\\S+) (?P<note>.+)",

66
edmond/plugins/music.py Normal file
View file

@ -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

View file

@ -9,3 +9,6 @@ google-api-python-client
# Translator # Translator
translate translate
# Music
scaruffi==0.0.2