56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
from irc3.plugins.command import command
|
|
from irc3.plugins.cron import cron
|
|
import irc3
|
|
|
|
import sys
|
|
import re
|
|
from urllib.request import Request, urlopen
|
|
from urllib.error import URLError, HTTPError
|
|
|
|
USER_AGENT_CURL="curl/7.29.0"
|
|
STATUS_BAD_CODE=":( - error code: {}"
|
|
STATUS_BAD_REASON=":( - failed reason: {}"
|
|
STATUS_OK=":)"
|
|
|
|
@irc3.plugin
|
|
class Plugin:
|
|
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
|
|
def ratesex_request(self,commands):
|
|
url = f"https://rate.sx/{commands}"
|
|
r = Request(url,data=None,headers={ 'User-Agent': USER_AGENT_CURL })
|
|
try:
|
|
response = urlopen(r,timeout=10).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:
|
|
print(response)
|
|
return STATUS_OK + response
|
|
|
|
def escape_ansi(self,line):
|
|
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
|
|
return ansi_escape.sub('', str(line))
|
|
|
|
|
|
@command(permission='view')
|
|
def ratesex(self, mask, target, args):
|
|
"""ratesex - help/commands: visit 'https://rate.sx/:help' or try '?ratesex :help', to check exchange rates for top 10 ^cripdoeocurrencies try '?ratesex top10'
|
|
%%ratesex <command>
|
|
"""
|
|
command = args.get('<command>')
|
|
if command.lower() == 'top10': command = ''
|
|
response=self.ratesex_request(command)
|
|
s = ''
|
|
for _ in response:
|
|
s = s + ''.join(_)
|
|
B=self.escape_ansi(s).replace('\x1b(B','')
|
|
f = open('/run/irc3/tcpdirect','w')
|
|
for _ in B:
|
|
f.write(_)
|
|
f.close()
|