Added urban dictionary plugin

This commit is contained in:
mrhanky 2017-06-01 15:07:30 +02:00
parent 432a2c9339
commit 100572abe6
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
3 changed files with 53 additions and 0 deletions

View File

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

View File

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

49
nxy/plugins/urban.py Normal file
View File

@ -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>...
"""
query = ' '.join(args.get('<query>')).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'