Added urban dictionary plugin
This commit is contained in:
parent
432a2c9339
commit
100572abe6
|
@ -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..."
|
||||
|
|
|
@ -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
49
nxy/plugins/urban.py
Normal 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'
|
Loading…
Reference in New Issue
Block a user