Module marketools.stqscraper.fundamentals

Expand source code
from . import get_storage_status, get_storage_dir
from datetime import datetime
import os
import csv
from .scrapers import scrap_summary_table


class Fundamentals(dict):
    """
    Class Fundamentals
    """

    def __init__(self, ticker):
        super().__init__()
        self.ticker = ticker

    def get_fundamentals(self):
        """
        Scraps from Stooq.com fundamental information for given ticker.
        """

        update_required = True  # assuming that update will be required
        file_path = os.path.join(get_dwl_storage_dir(), f'{self.ticker}_indicators.csv')

        if get_dwl_storage_status() and os.path.exists(file_path):
            timestamp_now = datetime.timestamp(datetime.now())
            timestamp_up = os.path.getatime(file_path)  # CSV file modification time

            if timestamp_now - timestamp_up < 24 * 3600:
                # do not update more often than once in 24 hours
                with open(file_path, 'r') as f:
                    reader = csv.DictReader(f)
                    try:
                        self.update(next(reader))
                    except StopIteration:
                        pass
                update_required = False
                # make sure that values are float
                for k in self.keys():
                    if self[k]:
                        self[k] = float(self[k])

        if update_required:
            # data older than 24 hours - update
            self.update(scrap_summary_table(self.ticker))
            if get_dwl_storage_status():
                with open(file_path, 'w') as f:
                    writer = csv.DictWriter(f, self.keys())
                    writer.writeheader()
                    writer.writerow(self)

        if bool(self) is False:
            # no fundamental data (None or empty dict)
            self.update()

    
if __name__ == '__main__':
    pass

Classes

class Fundamentals (ticker)

Class Fundamentals

Expand source code
class Fundamentals(dict):
    """
    Class Fundamentals
    """

    def __init__(self, ticker):
        super().__init__()
        self.ticker = ticker

    def get_fundamentals(self):
        """
        Scraps from Stooq.com fundamental information for given ticker.
        """

        update_required = True  # assuming that update will be required
        file_path = os.path.join(get_dwl_storage_dir(), f'{self.ticker}_indicators.csv')

        if get_dwl_storage_status() and os.path.exists(file_path):
            timestamp_now = datetime.timestamp(datetime.now())
            timestamp_up = os.path.getatime(file_path)  # CSV file modification time

            if timestamp_now - timestamp_up < 24 * 3600:
                # do not update more often than once in 24 hours
                with open(file_path, 'r') as f:
                    reader = csv.DictReader(f)
                    try:
                        self.update(next(reader))
                    except StopIteration:
                        pass
                update_required = False
                # make sure that values are float
                for k in self.keys():
                    if self[k]:
                        self[k] = float(self[k])

        if update_required:
            # data older than 24 hours - update
            self.update(scrap_summary_table(self.ticker))
            if get_dwl_storage_status():
                with open(file_path, 'w') as f:
                    writer = csv.DictWriter(f, self.keys())
                    writer.writeheader()
                    writer.writerow(self)

        if bool(self) is False:
            # no fundamental data (None or empty dict)
            self.update()

Ancestors

  • builtins.dict

Methods

def get_fundamentals(self)

Scraps from Stooq.com fundamental information for given ticker.

Expand source code
def get_fundamentals(self):
    """
    Scraps from Stooq.com fundamental information for given ticker.
    """

    update_required = True  # assuming that update will be required
    file_path = os.path.join(get_dwl_storage_dir(), f'{self.ticker}_indicators.csv')

    if get_dwl_storage_status() and os.path.exists(file_path):
        timestamp_now = datetime.timestamp(datetime.now())
        timestamp_up = os.path.getatime(file_path)  # CSV file modification time

        if timestamp_now - timestamp_up < 24 * 3600:
            # do not update more often than once in 24 hours
            with open(file_path, 'r') as f:
                reader = csv.DictReader(f)
                try:
                    self.update(next(reader))
                except StopIteration:
                    pass
            update_required = False
            # make sure that values are float
            for k in self.keys():
                if self[k]:
                    self[k] = float(self[k])

    if update_required:
        # data older than 24 hours - update
        self.update(scrap_summary_table(self.ticker))
        if get_dwl_storage_status():
            with open(file_path, 'w') as f:
                writer = csv.DictWriter(f, self.keys())
                writer.writeheader()
                writer.writerow(self)

    if bool(self) is False:
        # no fundamental data (None or empty dict)
        self.update()