Huge cleanup and refactoring :>
This commit is contained in:
47
bot/urban.py
Normal file
47
bot/urban.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import requests
|
||||
from docopt import Dict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
|
||||
from . import Plugin
|
||||
|
||||
|
||||
class Urban(Plugin):
|
||||
URL = 'https://api.urbandictionary.com/v0/define'
|
||||
|
||||
@command
|
||||
def ud(self, mask: IrcString, target: IrcString, args: Dict):
|
||||
"""Searches for a term on YouTube and returns first result
|
||||
|
||||
%%ud <term>...
|
||||
"""
|
||||
# Clean, strip and split term by whitespace
|
||||
term = ' '.join(args.get('<term>')).lower().strip().split()
|
||||
|
||||
if term[-1].isdigit():
|
||||
# Parse number at end as index - 1 and slice term
|
||||
index = int(term[-1]) - 1
|
||||
term = ' '.join(term[:-1])
|
||||
else:
|
||||
# Set index to 0
|
||||
index = 0
|
||||
|
||||
# Fetch data for term from urban dictionary as json
|
||||
data = requests.get(self.URL, params=dict(term=term)).json()
|
||||
|
||||
# No results
|
||||
if data['result_type'] == 'no_results':
|
||||
return 'Term not found'
|
||||
|
||||
# Get result by index
|
||||
res = data['list'][index]
|
||||
|
||||
# Format and return result
|
||||
return '[{idx}/{len}] \x02{word}\x02: {definition} - {example}'.format(
|
||||
idx=index + 1,
|
||||
len=len(data['list']),
|
||||
word=res['word'],
|
||||
definition=res['definition'].replace('\r\n', ' '),
|
||||
example=res['example'].replace('\r\n', ' '),
|
||||
)
|
||||
Reference in New Issue
Block a user