44 lines
2.3 KiB
Python
44 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
###############################################################################################
|
|
###############################################################################################
|
|
from irc3.plugins.command import command
|
|
import irc3
|
|
###############################################################################################
|
|
###############################################################################################
|
|
from urllib.request import Request, urlopen
|
|
from urllib.error import URLError, HTTPError
|
|
###############################################################################################
|
|
###############################################################################################
|
|
@irc3.plugin
|
|
class Plugin:
|
|
###########################################################################################
|
|
###########################################################################################
|
|
def __init__(self, bot):
|
|
self.bot=bot
|
|
self.site_url="https://icanhazdadjoke.com"
|
|
###########################################################################################
|
|
###########################################################################################
|
|
def urlget(self,url):
|
|
USER_AGENT_CURL="curl/7.78.0"
|
|
ACCEPT_MODES="text/plain"
|
|
STATUS_BAD_CODE=":( - error code: {}"
|
|
STATUS_BAD_REASON=":( - failed reason: {}"
|
|
STATUS_OK=":)"
|
|
r = Request(url,data=None,headers={ 'user-agent': USER_AGENT_CURL, 'accept': ACCEPT_MODES })
|
|
try: response = urlopen(r,timeout=15).read().decode('utf-8')
|
|
except HTTPError as e: return STATUS_BAD_CODE.format(e.code)
|
|
except URLError as e: return STATUS_BAD_REASON.format(e.reason)
|
|
else: return STATUS_OK + response
|
|
###########################################################################################
|
|
###########################################################################################
|
|
@irc3.extend
|
|
@command(permission='view')
|
|
def joke(self, mask, target, args):
|
|
"""joke
|
|
%%joke
|
|
"""
|
|
response=self.urlget(self.site_url)[2:]
|
|
for msg in response.splitlines():
|
|
if len(msg) > 1:
|
|
self.bot.privmsg(target, self.bot.emo(msg))
|