2017-06-01 13:07:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import irc3
|
|
|
|
import requests
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-06-01 13:07:30 +00:00
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
|
|
|
|
|
|
|
from . import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
@irc3.plugin
|
|
|
|
class Urban(Plugin):
|
|
|
|
requires = ['irc3.plugins.command']
|
|
|
|
|
|
|
|
URL = 'https://api.urbandictionary.com/v0/define'
|
|
|
|
|
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
def ud(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-08-22 14:15:52 +00:00
|
|
|
"""Searches for a term on YouTube and returns first result
|
2017-06-01 13:07:30 +00:00
|
|
|
|
2017-06-01 15:38:34 +00:00
|
|
|
%%ud <term>...
|
2017-06-01 13:07:30 +00:00
|
|
|
"""
|
2017-06-30 11:25:56 +00:00
|
|
|
# Clean, strip and split term by whitespace
|
2017-06-01 15:38:34 +00:00
|
|
|
term = ' '.join(args.get('<term>')).lower().strip().split()
|
2017-06-01 13:07:30 +00:00
|
|
|
|
2017-06-01 15:38:34 +00:00
|
|
|
if term[-1].isdigit():
|
2017-06-30 11:25:56 +00:00
|
|
|
# Parse number at end as index - 1 and slice term
|
|
|
|
index = int(term[-1]) - 1
|
2017-06-01 15:38:34 +00:00
|
|
|
term = ' '.join(term[:-1])
|
2017-06-01 13:07:30 +00:00
|
|
|
else:
|
2017-06-30 11:25:56 +00:00
|
|
|
# Set index to 0
|
|
|
|
index = 0
|
2017-06-01 13:07:30 +00:00
|
|
|
|
2017-06-30 11:25:56 +00:00
|
|
|
# Fetch data for term from urban dictionary as json
|
2017-06-01 15:38:34 +00:00
|
|
|
data = requests.get(self.URL, params=dict(term=term)).json()
|
2017-06-30 11:25:56 +00:00
|
|
|
|
|
|
|
# No results
|
2017-06-01 13:07:30 +00:00
|
|
|
if data['result_type'] == 'no_results':
|
2017-06-01 15:38:34 +00:00
|
|
|
return 'Term not found'
|
2017-06-01 13:07:30 +00:00
|
|
|
|
2017-06-30 11:25:56 +00:00
|
|
|
# Get result by index
|
|
|
|
res = data['list'][index]
|
|
|
|
|
|
|
|
# Format and return result
|
2017-06-01 15:38:34 +00:00
|
|
|
return '[{idx}/{len}] \x02{word}\x02: {definition} - {example}'.format(
|
2017-06-30 11:25:56 +00:00
|
|
|
idx=index + 1,
|
2017-06-01 15:38:34 +00:00
|
|
|
len=len(data['list']),
|
|
|
|
word=res['word'],
|
|
|
|
definition=res['definition'].replace('\r\n', ' '),
|
|
|
|
example=res['example'].replace('\r\n', ' '),
|
|
|
|
)
|