Update Bollycao.
This commit is contained in:
parent
addc7fef6b
commit
ece6e78f48
|
@ -0,0 +1 @@
|
|||
Bollycao
|
|
@ -0,0 +1,37 @@
|
|||
import sys
|
||||
import supybot
|
||||
from supybot import world
|
||||
|
||||
# Use this for the version of this plugin.
|
||||
__version__ = "0.0.1"
|
||||
|
||||
# XXX Replace this with an appropriate author or supybot.Author instance.
|
||||
__author__ = supybot.authors.unknown
|
||||
|
||||
# This is a dictionary mapping supybot.Author instances to lists of
|
||||
# contributions.
|
||||
__contributors__ = {}
|
||||
|
||||
# This is a url where the most recent plugin package can be downloaded.
|
||||
__url__ = ''
|
||||
|
||||
from . import config
|
||||
from . import plugin
|
||||
if sys.version_info >= (3, 4):
|
||||
from importlib import reload
|
||||
else:
|
||||
from imp import reload
|
||||
# In case we're being reloaded.
|
||||
reload(config)
|
||||
reload(plugin)
|
||||
# Add more reloads here if you add third-party modules and want them to be
|
||||
# reloaded when this plugin is reloaded. Don't forget to import them as well!
|
||||
|
||||
if world.testing:
|
||||
from . import test
|
||||
|
||||
Class = plugin.Class
|
||||
configure = config.configure
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1,19 @@
|
|||
import json
|
||||
import re
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
class Bollycao():
|
||||
def __init__(self, url, r = None):
|
||||
self.requests = r or requests
|
||||
self.url = url
|
||||
|
||||
def get_price(self):
|
||||
content = self.requests.get(self.url).content
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
data_layer = soup.find('script', text=re.compile('dataLayer')).text
|
||||
|
||||
return float(json.loads(data_layer.split('=', 2)[-1][:-1])[0]['product']['price']['final'])
|
||||
|
||||
def get_bollycaos(self, euros):
|
||||
return euros/self.get_price()
|
|
@ -0,0 +1,30 @@
|
|||
from supybot import conf, registry
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('Bollycao')
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x: x
|
||||
|
||||
|
||||
def configure(advanced):
|
||||
# This will be called by supybot to configure this module. advanced is
|
||||
# a bool that specifies whether the user identified themself as an advanced
|
||||
# user or not. You should effect your configuration by manipulating the
|
||||
# registry as appropriate.
|
||||
from supybot.questions import expect, anything, something, yn
|
||||
conf.registerPlugin('Bollycao', True)
|
||||
|
||||
|
||||
Bollycao = conf.registerPlugin('Bollycao')
|
||||
|
||||
conf.registerGlobalValue(Bollycao, 'url',
|
||||
registry.String('https://www.elcorteingles.pt/supermercado/0105220657200778-bollycao-bolos-recheados-com-chocolate-embalagem-57-g/',
|
||||
'elcorte ingles url to use for price'))
|
||||
# This is where your configuration variables (if any) should go. For example:
|
||||
# conf.registerGlobalValue(Bollycao, 'someConfigVariableName',
|
||||
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1 @@
|
|||
# Stub so local is a module, used for third-party modules
|
|
@ -0,0 +1,43 @@
|
|||
from supybot import utils, plugins, ircutils, callbacks
|
||||
from supybot.commands import *
|
||||
from .bollycao import Bollycao as BollycaoUtils
|
||||
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('Bollycao')
|
||||
except ImportError:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x: x
|
||||
|
||||
|
||||
class Bollycao(callbacks.Plugin):
|
||||
"""Bollycao"""
|
||||
threaded = True
|
||||
|
||||
def __init__(self, irc):
|
||||
self.__parent = super(Bollycao, self)
|
||||
self.__parent.__init__(irc)
|
||||
self.bollycao_utils = BollycaoUtils(self.registryValue('url'))
|
||||
|
||||
def bollycao(self, irc, msg, args):
|
||||
"""takes no arguments
|
||||
|
||||
Returns the price of one bollycao in euros
|
||||
"""
|
||||
irc.reply('1 bollycao = €' + "{:.2f}".format(self.bollycao_utils.get_price()))
|
||||
bollycao = wrap(bollycao)
|
||||
|
||||
def bollycaos(self, irc, msg, args, euros):
|
||||
"""<euros>
|
||||
|
||||
Returns the number of bollycaos you could buy with <euros>
|
||||
"""
|
||||
irc.reply('€' + "{:.2f}".format(euros) + ' = ' +
|
||||
str(self.bollycao_utils.get_bollycaos(euros)) + ' bollycaos')
|
||||
bollycaos = wrap(bollycaos, ['float'])
|
||||
|
||||
Class = Bollycao
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1,2 @@
|
|||
beautifulsoup4
|
||||
requests
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,22 @@
|
|||
from supybot.test import *
|
||||
|
||||
|
||||
class BollycaoTestCase(PluginTestCase):
|
||||
plugins = ('Bollycao',)
|
||||
|
||||
def test_bollycao_no_error(self):
|
||||
self.assertNotError('bollycao')
|
||||
|
||||
def test_bollycaos_no_error(self):
|
||||
self.assertNotError('bollycaos 2')
|
||||
|
||||
def test_bollycao(self):
|
||||
self.assertRegexp('bollycao', '1 bollycao = €[-+]?[0-9]*\.?[0-9]+')
|
||||
|
||||
def test_bollycaos(self):
|
||||
self.assertRegexp('bollycaos 2.40', '€2.40 = [-+]?[0-9]*\.?[0-9]+ bollycaos')
|
||||
|
||||
def test_bollycaos_with_no_decimal_places(self):
|
||||
self.assertRegexp('bollycaos 2', '€2.00 = [-+]?[0-9]*\.?[0-9]+ bollycaos')
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1,37 @@
|
|||
from unittest.mock import MagicMock
|
||||
import unittest
|
||||
import requests
|
||||
from bollycao import Bollycao
|
||||
|
||||
class BollycaoTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.test_html = get_text('./test.html')
|
||||
self.requests = requests
|
||||
self.bollycao = Bollycao('url', self.requests)
|
||||
|
||||
def test_get_price_parses_correctly(self):
|
||||
self.requests.get = MagicMock(return_value=MockResponse(self.test_html))
|
||||
price = self.bollycao.get_price()
|
||||
self.assertAlmostEqual(price, 0.90)
|
||||
|
||||
def test_get_bollycaos_returns_correct_values(self):
|
||||
self.bollycao.get_price = MagicMock(return_value=0.5)
|
||||
bollycao_number = self.bollycao.get_bollycaos(100)
|
||||
self.assertAlmostEqual(bollycao_number, 200)
|
||||
|
||||
|
||||
class MockResponse():
|
||||
def __init__(self, content):
|
||||
self._content = content
|
||||
|
||||
@property
|
||||
def content(self):
|
||||
return self._content
|
||||
|
||||
def get_text(filename):
|
||||
text = ''
|
||||
|
||||
with open(filename, 'r') as content_file:
|
||||
text = content_file.read()
|
||||
|
||||
return text
|
Loading…
Reference in New Issue