Example #1
0
    def __init__(self, *args, **kwargs):
        super(ServiceBase, self).__init__(*args, **kwargs)

        self.url = self.init_kwarg('url')
        self.service_name = self.init_kwarg('service_name')
        self.total_units = self.init_kwarg('total_units', default=1)
        self.page = ""
        self.soup = ""
        self._rate_morning = 0.00
        self._rate_evening = 0.00
        self._prices_text = ""
        self.db = RatesDB(conn_string=cfg.db['conn_string'])
        self.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
        self.headers = {
            'User-Agent': self.user_agent,
        }
Example #2
0
def db(request):

    test_db = "ratesdb_test"

    conn_string = cfg.db['conn_string'].replace(cfg.db['dbname'],test_db)

    print(clr.red("\nConn String: {}\n".format(conn_string)))
        
    db = RatesDB(conn_string=conn_string)
    
    #Function for teardown operations
    def fin():
        
        print(clr.red("\n===Tearing down===\n"))
        db.session.close()
        db.session.remove()
        db.base.metadata.drop_all(db.engine)
        
        print(clr.red("\n===Executed drop_all on the DB===\n"))
        
    request.addfinalizer(fin)
    
    return db

    
    
Example #3
0
def get_rates(for_date, to_date):

    for_date = for_date.strftime('%Y-%m-%d')
    to_date = to_date + timedelta(days=1)
    to_date = to_date.strftime('%Y-%m-%d')

    db = RatesDB(conn_string=cfg.db['conn_string'])

    services = {}

    for service in ServiceBase.__subclasses__():

        srv = service()
        rates = db.get_rates_between_dates(srv, for_date, to_date)
        services[srv.service_name] = rates

    db.session.close()
    db.session.remove()

    return services
Example #4
0
class ServiceBase(Logger, Kwargs):
    '''Common base class for all services that provide rates'''
    def __init__(self, *args, **kwargs):
        super(ServiceBase, self).__init__(*args, **kwargs)

        self.url = self.init_kwarg('url')
        self.service_name = self.init_kwarg('service_name')
        self.total_units = self.init_kwarg('total_units', default=1)
        self.page = ""
        self.soup = ""
        self._rate_morning = 0.00
        self._rate_evening = 0.00
        self._prices_text = ""
        self.db = RatesDB(conn_string=cfg.db['conn_string'])
        self.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
        self.headers = {
            'User-Agent': self.user_agent,
        }

    @property
    def rate_morning(self):
        return self._rate_morning

    @rate_morning.setter
    def rate_morning(self, value):
        self._rate_morning = value

    @property
    def rate_evening(self):
        return self._rate_evening

    @rate_evening.setter
    def rate_evening(self, value):
        self._rate_evening = value

    @property
    def current_value_morning(self):

        return self.total_units * self._rate_morning

    @property
    def current_value_evening(self):
        return self.total_units * self._rate_evening

    @property
    def value_text_morning(self):
        return "\n\nTotal Value ({} units @ {}) : {}".format(
            self.total_units, self._rate_morning, self.current_value_morning)

    @property
    def value_text_evening(self):
        return "\n\nTotal Value ({} units @ {}) : {}".format(
            self.total_units, self._rate_evening, self.current_value_evening)

    @property
    def value_text(self):
        return self.value_text_morning + self.value_text_evening

    @property
    def prices_text(self):
        return self._prices_text + self.value_text

    def __read_url(self):

        self.logger.info("Reading website: {}".format(self.url))
        req = urllib.request.Request(self.url, headers=self.headers)
        response = urllib.request.urlopen(req)
        self.page = response.read()

    def __parse_html(self):

        self.logger.info("Parsing HTML: {}".format(self.url))

        self.soup = BeautifulSoup(self.page, features="html.parser")

    def db_save(self):
        '''Save rates to the database'''

        self.db.insert_rate(self)
        self.db.session.close()
        self.db.session.remove()
        self.logger.info("Rates saved to Database")

    def db_last_rates(self):
        '''Get the last rates inserted in the DB for the service'''

        last_rates = self.db.get_last_rates(self)
        self.db.session.close()
        self.db.session.remove()
        self.logger.debug("Last rates from DB: {}".format(str(last_rates)))

        return last_rates

    def get_rates(self):
        '''
        Retreive rates from a provider.
        This method should be implemented for each of the derived classes that will be implemented for each of the service.
        It should populate the fields - _prices_text, _rate_morning and _rate_evening after scraping the service and return prices_text
        '''
        self.__read_url()
        self.__parse_html()

    def rates_changed(self):
        '''Check latest rates from a service and compare to saved rates from last time the rates were checked.'''

        self.logger.debug("Current rates from {} : {}".format(
            self.service_name, "[{}, {}]".format(self.rate_morning,
                                                 self.rate_evening)))

        if [self.rate_morning, self.rate_evening] == self.db_last_rates():
            return False
        else:
            self.db_save()

            return True

    def format_rate(self, rate):
        return 0.00 if (rate == "" or rate == "-") else float(rate.split()[0])