From 100572abe64e4bdea27a624b9991174591cbf5cb Mon Sep 17 00:00:00 2001 From: mrhanky Date: Thu, 1 Jun 2017 15:07:30 +0200 Subject: [PATCH] Added urban dictionary plugin --- README.md | 3 +++ config.json | 1 + nxy/plugins/urban.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 nxy/plugins/urban.py diff --git a/README.md b/README.md index b80e997..2e5b39e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,9 @@ ## plugins/timer * [x] .timer - sets a timer for a specified amount of time and a name +## plugins/urban +* [x] .ud - gets a definition from urban dictionary + ## plugins/useless * [x] .gay - colors text in rainbow colors * [x] .hack - prints "hacking..." diff --git a/config.json b/config.json index 3cf19d4..475ecfa 100644 --- a/config.json +++ b/config.json @@ -21,6 +21,7 @@ "nxy.plugins.seen", "nxy.plugins.tell", "nxy.plugins.timer", + "nxy.plugins.urban", "nxy.plugins.useless", "nxy.plugins.weather", "nxy.plugins.youtube" diff --git a/nxy/plugins/urban.py b/nxy/plugins/urban.py new file mode 100644 index 0000000..6026f9d --- /dev/null +++ b/nxy/plugins/urban.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +import irc3 +import os +import re +import requests + +from docopt import Dict as DocOptDict +from irc3.plugins.command import command +from irc3.utils import IrcString + +from . import Plugin +from ..utils import date_from_iso + + +@irc3.plugin +class Urban(Plugin): + requires = ['irc3.plugins.command'] + + URL = 'https://api.urbandictionary.com/v0/define' + + @command + def ud(self, mask: IrcString, channel: IrcString, args: DocOptDict): + """Searches for query on YouTube and returns first result. + + %%ud ... + """ + query = ' '.join(args.get('')).lower().strip().split() + + if query[-1].isdigit(): + idx = int(query[-1]) - 1 + query = ' '.join(query[:-1]) + else: + idx = 0 + + data = requests.get(self.URL, params=dict(term=query)).json() + + if data['result_type'] == 'no_results': + return '[UrbanDictionary] Query not found' + res = data['list'][idx] + + try: + return '[{idx}/{len}] \x02{word}\x02: {definition} - {example}' \ + .format(idx=idx + 1, + len=len(data['list']), + word=res['word'], + definition=res['definition'].replace('\r\n', ' '), + example=res['example'].replace('\r\n', ' ')) + except IndexError: + return '[UrbanDictionary] Error getting result'