music: add plugin using scaruffi library

master
dece 4 years ago
parent 93eddad240
commit 7cbcd5eb85

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

@ -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<target>\\S+) (?P<note>.+)",

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

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

Loading…
Cancel
Save