From f28b927bc513da112cc6f98c2887cde9eef567a0 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Wed, 8 Sep 2021 14:53:09 +0200 Subject: [PATCH] Updated joke skill with spiltted providers --- .../skills/entertainement/jokes/__init__.py | 57 ++++++++----------- .../jokes/providers/__init__.py | 0 .../jokes/providers/jokes_english_provider.py | 26 +++++++++ .../jokes/providers/jokes_french_provider.py | 29 ++++++++++ .../jokes/vocab/en-us/Chuck.voc | 2 - .../entertainement/jokes/vocab/en-us/Joke.voc | 5 -- .../jokes/vocab/en-us/Neutral.voc | 3 - .../jokes/vocab/fr-fr/Chuck.voc | 2 - .../entertainement/jokes/vocab/fr-fr/Joke.voc | 5 -- .../jokes/vocab/fr-fr/Neutral.voc | 3 - .../jokes/vocab/fr-fr/tell_a_joke.intent | 3 + 11 files changed, 82 insertions(+), 53 deletions(-) create mode 100644 jarvis/skills/entertainement/jokes/providers/__init__.py create mode 100644 jarvis/skills/entertainement/jokes/providers/jokes_english_provider.py create mode 100644 jarvis/skills/entertainement/jokes/providers/jokes_french_provider.py delete mode 100644 jarvis/skills/entertainement/jokes/vocab/en-us/Chuck.voc delete mode 100644 jarvis/skills/entertainement/jokes/vocab/en-us/Joke.voc delete mode 100644 jarvis/skills/entertainement/jokes/vocab/en-us/Neutral.voc delete mode 100644 jarvis/skills/entertainement/jokes/vocab/fr-fr/Chuck.voc delete mode 100644 jarvis/skills/entertainement/jokes/vocab/fr-fr/Joke.voc delete mode 100644 jarvis/skills/entertainement/jokes/vocab/fr-fr/Neutral.voc create mode 100644 jarvis/skills/entertainement/jokes/vocab/fr-fr/tell_a_joke.intent diff --git a/jarvis/skills/entertainement/jokes/__init__.py b/jarvis/skills/entertainement/jokes/__init__.py index 425b6e8..86aba80 100644 --- a/jarvis/skills/entertainement/jokes/__init__.py +++ b/jarvis/skills/entertainement/jokes/__init__.py @@ -1,44 +1,35 @@ -import requests -from adapt.intent import IntentBuilder - from jarvis.skills import Skill, SkillRegistering -from jarvis.skills.decorators import intent_handler -from jarvis.utils import languages_utils, config_utils - - -def speak_joke(): - # french jokes - if languages_utils.get_language().startswith("fr-"): - # please register on www.blagues-api.fr and set a token in your secret - response = requests.get('https://www.blagues-api.fr/api/random', headers={ - 'Authorization': 'Bearer ' + config_utils.get_in_secret('JOKES_FRENCH_API_TOKEN')}) - - data = response.json() - joke = data['joke'] - answer = data['answer'] - - return joke + " /pause:2s/ " + answer - - # english jokes - elif languages_utils.get_language().startswith("en-"): - response = requests.get('https://v2.jokeapi.dev/joke/Any?type=twopart') - data = response.json() - - joke = data['setup'] - answer = data['delivery'] - - return joke + " /pause:2s/ " + answer - else: - return "I don't know any jokes in your language..." +from jarvis.skills.decorators import intent_file_handler +from jarvis.skills.entertainement.jokes.providers import jokes_french_provider, jokes_english_provider +from jarvis.utils import languages_utils class JokesSkill(Skill, metaclass=SkillRegistering): def __init__(self, data=dict): super().__init__("JokesSkill", data) - @intent_handler(IntentBuilder("JokingIntent").require("Joke")) + @intent_file_handler("tell_a_joke.intent", "TellAJokeIntent") def handle_joke(self, data): - print(speak_joke()) + self.speak(get_joke(False)) + + +def get_joke(nsfw, lang=languages_utils.get_language()): + """ + Returns a joke in the good language + + Args: + lang: use language from config by default, you can specify a custom language here + nsfw: should include nsfw jokes + + Returns: + array + """ + if lang.startswith("fr-"): + return jokes_french_provider.get_joke(nsfw) + elif lang.startswith("en-"): + return jokes_english_provider.get_joke(nsfw) + else: + return ['Error', "I don't know any jokes in your language..."] def create_skill(data): diff --git a/jarvis/skills/entertainement/jokes/providers/__init__.py b/jarvis/skills/entertainement/jokes/providers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarvis/skills/entertainement/jokes/providers/jokes_english_provider.py b/jarvis/skills/entertainement/jokes/providers/jokes_english_provider.py new file mode 100644 index 0000000..046e9c8 --- /dev/null +++ b/jarvis/skills/entertainement/jokes/providers/jokes_english_provider.py @@ -0,0 +1,26 @@ +import requests + + +def get_joke(nsfw=False): + """ + Returns a joke in 2 parts + + Args: + nsfw: include nsfw jokes? + + Returns: array + """ + + url = 'https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=twopart' + + if nsfw: + url = "https://v2.jokeapi.dev/joke/Any?type=twopart" + + # please register on www.blagues-api.fr and set a token in your secrets file + response = requests.get(url) + + data = response.json() + joke = data['setup'] + answer = data['delivery'] + + return [joke, answer] diff --git a/jarvis/skills/entertainement/jokes/providers/jokes_french_provider.py b/jarvis/skills/entertainement/jokes/providers/jokes_french_provider.py new file mode 100644 index 0000000..20acc08 --- /dev/null +++ b/jarvis/skills/entertainement/jokes/providers/jokes_french_provider.py @@ -0,0 +1,29 @@ +import requests + +from jarvis.utils import config_utils + + +def get_joke(nsfw=False): + """ + Returns a joke in 2 parts + + Args: + nsfw: include nsfw jokes? + + Returns: array + """ + + url = 'https://www.blagues-api.fr/api/random' + + if nsfw: + url = url + "?disallow=dark&disallow=limit" + + # please register on www.blagues-api.fr and set a token in your secrets file + response = requests.get(url, headers={ + 'Authorization': 'Bearer ' + config_utils.get_in_secret('JOKES_FRENCH_API_TOKEN')}) + + data = response.json() + joke = data['joke'] + answer = data['answer'] + + return [joke, answer] diff --git a/jarvis/skills/entertainement/jokes/vocab/en-us/Chuck.voc b/jarvis/skills/entertainement/jokes/vocab/en-us/Chuck.voc deleted file mode 100644 index 28b30ed..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/en-us/Chuck.voc +++ /dev/null @@ -1,2 +0,0 @@ -chuck -chuck norris diff --git a/jarvis/skills/entertainement/jokes/vocab/en-us/Joke.voc b/jarvis/skills/entertainement/jokes/vocab/en-us/Joke.voc deleted file mode 100644 index 05e2f3f..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/en-us/Joke.voc +++ /dev/null @@ -1,5 +0,0 @@ -joke -jokes -make me laugh -brighten my day -tell me joke diff --git a/jarvis/skills/entertainement/jokes/vocab/en-us/Neutral.voc b/jarvis/skills/entertainement/jokes/vocab/en-us/Neutral.voc deleted file mode 100644 index 0f8b900..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/en-us/Neutral.voc +++ /dev/null @@ -1,3 +0,0 @@ -neutral -non-offensive -geeky diff --git a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Chuck.voc b/jarvis/skills/entertainement/jokes/vocab/fr-fr/Chuck.voc deleted file mode 100644 index 28b30ed..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Chuck.voc +++ /dev/null @@ -1,2 +0,0 @@ -chuck -chuck norris diff --git a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Joke.voc b/jarvis/skills/entertainement/jokes/vocab/fr-fr/Joke.voc deleted file mode 100644 index 724bcdd..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Joke.voc +++ /dev/null @@ -1,5 +0,0 @@ -blague -blagues -fais moi rire -illumine ma journée -raconte moi une blague diff --git a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Neutral.voc b/jarvis/skills/entertainement/jokes/vocab/fr-fr/Neutral.voc deleted file mode 100644 index b229f44..0000000 --- a/jarvis/skills/entertainement/jokes/vocab/fr-fr/Neutral.voc +++ /dev/null @@ -1,3 +0,0 @@ -neutre -non offensant -geek diff --git a/jarvis/skills/entertainement/jokes/vocab/fr-fr/tell_a_joke.intent b/jarvis/skills/entertainement/jokes/vocab/fr-fr/tell_a_joke.intent new file mode 100644 index 0000000..52b6160 --- /dev/null +++ b/jarvis/skills/entertainement/jokes/vocab/fr-fr/tell_a_joke.intent @@ -0,0 +1,3 @@ +raconte (moi|nous|) (voir|) une blague +fais (moi|nous) rire +illumine (ma|notre) journée