├── cumplo_spotter ├── py.typed ├── models │ ├── __init__.py │ ├── cumplo │ │ ├── __init__.py │ │ ├── request_duration.py │ │ ├── portfolio.py │ │ ├── simulation.py │ │ ├── debtor.py │ │ ├── borrower.py │ │ └── funding_request.py │ └── filter.py ├── utils │ ├── __init__.py │ └── constants.py ├── business │ ├── __init__.py │ └── funding_requests.py ├── routers │ ├── __init__.py │ └── funding_requests │ │ ├── __init__.py │ │ ├── private.py │ │ └── public.py ├── integrations │ ├── __init__.py │ └── cumplo │ │ ├── __init__.py │ │ ├── exceptions.py │ │ ├── api_html.py │ │ ├── controller.py │ │ ├── api_graphql.py │ │ └── api_global.py ├── __init__.py └── main.py ├── docker-compose.yml ├── .dockerignore ├── .vscode ├── extensions.json └── settings.json ├── .devcontainer ├── devcontainer.json └── Dockerfile ├── README.md ├── Makefile ├── Dockerfile ├── Dockerfile.development ├── pyproject.toml ├── .gitignore └── LICENSE /cumplo_spotter/py.typed: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/models/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/business/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/routers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cumplo_spotter/routers/funding_requests/__init__.py: -------------------------------------------------------------------------------- 1 | from cumplo_spotter.routers.funding_requests import private, public 2 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/__init__.py: -------------------------------------------------------------------------------- 1 | from cumplo_spotter.integrations.cumplo.controller import cache, get_available_funding_requests 2 | -------------------------------------------------------------------------------- /cumplo_spotter/__init__.py: -------------------------------------------------------------------------------- 1 | """A simple yet powerful API for spotting secure and high-return Cumplo investment opportunities.""" 2 | 3 | __version__ = "1.6.2" 4 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/exceptions.py: -------------------------------------------------------------------------------- 1 | class NoResultFoundError(Exception): 2 | """Exception raised when no result is found in a Cumplo API.""" 3 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | cumplo-spotter: 3 | image: cumplo-spotter 4 | env_file: .env 5 | ports: 6 | - 8000:8080 7 | build: 8 | context: . 9 | dockerfile: Dockerfile.development 10 | args: 11 | - CUMPLO_PYPI_BASE64_KEY 12 | volumes: 13 | - ./cumplo_spotter:/app/cumplo_spotter 14 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # Exclude: everything 2 | * 3 | 4 | # Include: application files 5 | !cumplo_spotter 6 | 7 | # Include: build files 8 | !pyproject.toml 9 | !poetry.toml 10 | !poetry.lock 11 | 12 | # Include: miscelaneous files 13 | !entrypoint.sh 14 | 15 | # Include: credentials 16 | !cumplo-spotter-credentials.json 17 | !cumplo-pypi-credentials.json 18 | -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/__init__.py: -------------------------------------------------------------------------------- 1 | from cumplo_spotter.models.cumplo.borrower import Borrower as CumploBorrower 2 | from cumplo_spotter.models.cumplo.debtor import Debtor as CumploDebtor 3 | from cumplo_spotter.models.cumplo.funding_request import CumploFundingRequest 4 | from cumplo_spotter.models.cumplo.request_duration import CumploFundingRequestDuration 5 | from cumplo_spotter.models.cumplo.simulation import CumploFundingRequestSimulation 6 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | // Python 4 | "ms-python.python", 5 | "matangover.mypy", 6 | "charliermarsh.ruff", 7 | // Others 8 | "tamasfe.even-better-toml", // TOML 9 | "ms-vscode.makefile-tools", // Makefile 10 | "eamodio.gitlens", 11 | "usernamehw.errorlens", 12 | "Gruntfuggly.todo-tree", 13 | "GitHub.copilot", 14 | "ms-azuretools.vscode-docker", 15 | "emeraldwalk.runonsave", 16 | ], 17 | "unwantedRecommendations": [ 18 | "ms-python.autopep8" 19 | ] 20 | } -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/request_duration.py: -------------------------------------------------------------------------------- 1 | from cumplo_common.models import DurationUnit 2 | from pydantic import BaseModel, Field, field_validator 3 | 4 | 5 | class CumploFundingRequestDuration(BaseModel): 6 | unit: DurationUnit = Field(..., alias="type") 7 | value: int = Field(...) 8 | 9 | def __str__(self) -> str: 10 | return f"{self.value} {self.unit}" 11 | 12 | @field_validator("unit", mode="before") 13 | @classmethod 14 | def unit_formatter(cls, value: str) -> DurationUnit: 15 | """Format the unit value.""" 16 | return DurationUnit(value.strip().upper()) 17 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "${localWorkspaceFolderBasename}", 3 | "remoteUser": "root", 4 | "runArgs": [ 5 | "--privileged" 6 | ], 7 | "build": { 8 | "context": "..", 9 | "dockerfile": "Dockerfile", 10 | "args": { 11 | "WORKSPACE_FOLDER": "${localWorkspaceFolderBasename}", 12 | "POETRY_VERSION": "2.1.2" 13 | } 14 | }, 15 | "customizations": { 16 | "vscode": { 17 | "extensions": [ 18 | "ms-python.python", 19 | "charliermarsh.ruff", 20 | "matangover.mypy", 21 | "tamasfe.even-better-toml", 22 | "ms-vscode.makefile-tools", 23 | "eamodio.gitlens", 24 | "usernamehw.errorlens", 25 | "Gruntfuggly.todo-tree", 26 | "GitHub.copilot", 27 | "ms-azuretools.vscode-docker", 28 | "emeraldwalk.runonsave" 29 | ] 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /cumplo_spotter/main.py: -------------------------------------------------------------------------------- 1 | from logging import CRITICAL, DEBUG, INFO, basicConfig, getLogger 2 | 3 | import google.cloud.logging 4 | from cumplo_common.dependencies import authenticate, is_admin 5 | from cumplo_common.middlewares import PubSubMiddleware 6 | from fastapi import Depends, FastAPI 7 | 8 | from cumplo_spotter.routers import funding_requests 9 | from cumplo_spotter.utils.constants import IS_TESTING, LOG_FORMAT 10 | 11 | # NOTE: Mute noisy third-party loggers 12 | for module in ("google", "urllib3", "werkzeug"): 13 | getLogger(module).setLevel(CRITICAL) 14 | 15 | getLogger("cumplo_common").setLevel(DEBUG) 16 | 17 | if IS_TESTING: 18 | basicConfig(level=INFO, format=LOG_FORMAT) 19 | else: 20 | client = google.cloud.logging.Client() 21 | client.setup_logging(log_level=DEBUG) 22 | 23 | 24 | app = FastAPI(dependencies=[Depends(authenticate)]) 25 | app.add_middleware(PubSubMiddleware) 26 | 27 | app.include_router(funding_requests.public.router) 28 | app.include_router(funding_requests.private.router, dependencies=[Depends(is_admin)]) 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 | 6 |
7 |

8 | 9 | A simple yet powerful API for spotting secure and high-return
Cumplo investment opportunities 10 |
11 |

12 |
13 |
14 | 15 | # License 16 | This project is licensed under the [CC BY-NC 4.0 DEED](https://creativecommons.org/licenses/by-nc/4.0/deed.en) (Attribution-NonCommercial 4.0 International), which essentially means you are free to share and adapt the material in this repository, provided you give appropriate credit, indicate if changes were made, and, most importantly, **do not use it for commercial purposes**. 17 | 18 |
19 | 20 | # Contact 21 | 22 | Feel free to reach out through one of the following channels: 23 | 24 | - 📧 hello@cristobalsfeir.com 25 | - 📮 [Project Issues](https://github.com/cnsfeir/cumplo-spotter/issues) 26 | - 💼 [LinkedIn](https://www.linkedin.com/in/cnsfeir/) 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include .env 2 | export 3 | 4 | PYTHON_VERSION := $(shell python -c "print(open('.python-version').read().strip())") 5 | INSTALLED_VERSION := $(shell python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')") 6 | 7 | # Activates the project configuration and logs in to gcloud 8 | .PHONY: login 9 | login: 10 | @gcloud config configurations activate $(PROJECT_ID) 11 | @gcloud auth application-default login 12 | 13 | # Runs linters 14 | .PHONY: lint 15 | lint: 16 | @ruff check --fix 17 | @ruff format 18 | @mypy --config-file pyproject.toml . 19 | 20 | # Builds the Docker image 21 | .PHONY: build 22 | build: 23 | @docker compose build cumplo-spotter --build-arg CUMPLO_PYPI_BASE64_KEY=`base64 -i cumplo-pypi-credentials.json` 24 | 25 | # Starts the Docker container 26 | .PHONY: start 27 | start: 28 | @docker compose up -d cumplo-spotter 29 | 30 | # Stops the Docker container 31 | .PHONY: down 32 | down: 33 | @docker compose down 34 | 35 | # Updates the common library 36 | .PHONY: update-common 37 | update-common: 38 | @rm -rf .venv 39 | @poetry cache clear --no-interaction --all cumplo-pypi 40 | @poetry update 41 | -------------------------------------------------------------------------------- /cumplo_spotter/routers/funding_requests/private.py: -------------------------------------------------------------------------------- 1 | from http import HTTPStatus 2 | from logging import getLogger 3 | from typing import cast 4 | 5 | from cumplo_common.integrations.cloud_pubsub import CloudPubSub 6 | from cumplo_common.models import PrivateEvent, User 7 | from fastapi import APIRouter 8 | from fastapi.requests import Request 9 | 10 | from cumplo_spotter.business import funding_requests 11 | from cumplo_spotter.integrations import cumplo 12 | 13 | logger = getLogger(__name__) 14 | 15 | router = APIRouter(prefix="/funding-requests") 16 | 17 | 18 | @router.post(path="/fetch", status_code=HTTPStatus.NO_CONTENT) 19 | def _fetch_funding_requests(request: Request) -> None: 20 | """Fetch a list of funding requests and emits an event containing them.""" 21 | user = cast(User, request.state.user) 22 | 23 | cumplo.cache.clear() 24 | available_funding_requests = funding_requests.get_available() 25 | logger.info(f"Found {len(available_funding_requests)} available funding requests") 26 | 27 | if content := [funding_request.json() for funding_request in available_funding_requests]: 28 | CloudPubSub.publish(content=content, topic=PrivateEvent.FUNDING_REQUEST_AVAILABLE, id_user=str(user.id)) 29 | -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/vscode/devcontainers/python:3.12-bookworm 2 | 3 | # Set up Python environment variables 4 | ENV LANG=C.UTF-8 \ 5 | PYTHONDONTWRITEBYTECODE=1 \ 6 | PYTHONHASHSEED=random \ 7 | PYTHONFAULTHANDLER=1 \ 8 | PYTHONUNBUFFERED=1 9 | 10 | # Set up base work directory dynamically 11 | ARG WORKSPACE_FOLDER 12 | WORKDIR /workspaces/${WORKSPACE_FOLDER} 13 | 14 | # Set Poetry and pip environment variables 15 | ARG POETRY_VERSION 16 | ENV POETRY_VERSION=${POETRY_VERSION} \ 17 | POETRY_NO_INTERACTION=1 \ 18 | POETRY_VIRTUALENVS_CREATE=false \ 19 | PIP_DISABLE_PIP_VERSION_CHECK=on \ 20 | PIP_DEFAULT_TIMEOUT=100 \ 21 | PIP_NO_CACHE_DIR=off 22 | 23 | # Install OS package dependencies 24 | RUN apt-get update && \ 25 | apt-get install -y libpq-dev gcc && \ 26 | apt-get clean && \ 27 | rm -rf /var/lib/apt/lists/* 28 | 29 | # Install Poetry 30 | RUN pip install "poetry==$POETRY_VERSION" 31 | 32 | # Install Keyrings support for Google Artifact Registry 33 | RUN poetry self add keyrings.google-artifactregistry-auth 34 | 35 | # Copy only the dependencies related files 36 | COPY pyproject.toml poetry.lock ./ 37 | COPY cumplo_spotter ./cumplo_spotter 38 | 39 | # Add the Google Cloud credentials 40 | COPY cumplo-pypi-credentials.json /tmp/cumplo-pypi-credentials.json 41 | ENV GOOGLE_APPLICATION_CREDENTIALS=/tmp/cumplo-pypi-credentials.json 42 | 43 | # Install all dependencies 44 | RUN poetry install 45 | -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/portfolio.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | from typing import ClassVar 3 | 4 | from pydantic import BaseModel, Field, model_validator 5 | 6 | 7 | class PortfolioUnit(BaseModel): 8 | amount: Decimal = Field(...) 9 | count: int = Field(...) 10 | 11 | 12 | class Portfolio(BaseModel): 13 | cured: PortfolioUnit = Field(...) 14 | active: PortfolioUnit = Field(...) 15 | overdue: PortfolioUnit = Field(...) 16 | on_time: PortfolioUnit = Field(...) 17 | delinquent: PortfolioUnit = Field(...) 18 | 19 | PORTFOLIO_STATUS_MAPPING: ClassVar[dict] = {} 20 | 21 | @model_validator(mode="before") 22 | @classmethod 23 | def _format_portfolio_data(cls, value: list[dict]) -> dict: 24 | """Transform portfolio data from list of dicts to structured format.""" 25 | if not isinstance(value, list): 26 | return value 27 | 28 | portfolio = { 29 | "cured": {"amount": Decimal(0), "count": 0}, 30 | "active": {"amount": Decimal(0), "count": 0}, 31 | "overdue": {"amount": Decimal(0), "count": 0}, 32 | "on_time": {"amount": Decimal(0), "count": 0}, 33 | "delinquent": {"amount": Decimal(0), "count": 0}, 34 | } 35 | 36 | for item in value: 37 | if not (mapping := cls.PORTFOLIO_STATUS_MAPPING.get(item["tipo"])): 38 | continue 39 | 40 | if (status := mapping.get("status")) not in portfolio: 41 | continue 42 | 43 | if (value_type := mapping.get("type")) not in portfolio[status]: 44 | continue 45 | 46 | portfolio[status][value_type] = item.get("cantidad", 0) 47 | 48 | return portfolio 49 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/api_html.py: -------------------------------------------------------------------------------- 1 | from http import HTTPMethod 2 | from logging import getLogger 3 | 4 | import requests 5 | from bs4 import BeautifulSoup 6 | from cumplo_common.utils.text import clean_text 7 | 8 | from cumplo_spotter.integrations.cumplo.exceptions import NoResultFoundError 9 | from cumplo_spotter.utils.constants import CREDIT_DETAIL_TITLE, CUMPLO_HTML_API 10 | 11 | logger = getLogger(__name__) 12 | 13 | 14 | class CumploHTMLAPI: 15 | """Class to interact with Cumplo's HTML API.""" 16 | 17 | url = CUMPLO_HTML_API 18 | 19 | @classmethod 20 | def _request(cls, method: HTTPMethod, endpoint: str, payload: dict | None = None) -> requests.Response: 21 | """ 22 | Make a request to Cumplo's HTML API. 23 | 24 | Args: 25 | method (HTTPMethod): HTTP method to use 26 | endpoint (str): Endpoint to call 27 | payload (dict): Payload to send 28 | 29 | Returns: 30 | requests.Response: Response from the API 31 | 32 | """ 33 | return requests.request(method=method, url=f"{cls.url}{endpoint}", json=payload) 34 | 35 | @classmethod 36 | def get_funding_requests(cls, id_funding_request: int) -> BeautifulSoup: 37 | """ 38 | Query the Cumplo's HTML API for the given funding request information. 39 | 40 | Args: 41 | id_funding_request (int): The ID of the funding request 42 | 43 | Raises: 44 | NoResultFoundError: If the funding request information is not available 45 | 46 | Returns: 47 | BeautifulSoup: The parsed HTML of the funding request 48 | 49 | """ 50 | logger.debug(f"Getting funding request {id_funding_request} from Cumplo's HTML API") 51 | response = cls._request(HTTPMethod.GET, f"/{id_funding_request}") 52 | soup = BeautifulSoup(response.text, "html.parser") 53 | 54 | if CREDIT_DETAIL_TITLE not in clean_text(soup.get_text()): 55 | raise NoResultFoundError 56 | 57 | return soup 58 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Base container with Python 3.12 official image 2 | FROM python:3.12-slim-bookworm AS base 3 | 4 | # Set up Python environment variables 5 | ENV LANG=C.UTF-8 \ 6 | PYTHONDONTWRITEBYTECODE=1 \ 7 | PYTHONHASHSEED=random \ 8 | PYTHONFAULTHANDLER=1 \ 9 | PYTHONUNBUFFERED=1 10 | 11 | # Set up base work directory 12 | WORKDIR /app 13 | 14 | # ================================================================= 15 | 16 | # Build container with Poetry 17 | FROM base AS builder 18 | 19 | # Set Poetry and pip environment variables 20 | ENV POETRY_VERSION=2.1.2 \ 21 | POETRY_NO_INTERACTION=1 \ 22 | POETRY_VIRTUALENVS_CREATE=false \ 23 | PIP_DISABLE_PIP_VERSION_CHECK=on \ 24 | PIP_DEFAULT_TIMEOUT=100 \ 25 | PIP_NO_CACHE_DIR=off 26 | 27 | # Install OS package dependencies 28 | RUN apt-get update && \ 29 | apt-get install -y libpq-dev gcc && \ 30 | apt-get clean && \ 31 | rm -rf /var/lib/apt/lists/* 32 | 33 | # Install Poetry 34 | RUN pip install "poetry==$POETRY_VERSION" 35 | 36 | # Install Keyrings support for Google Artifact Registry 37 | RUN poetry self add keyrings.google-artifactregistry-auth 38 | 39 | # Copy only the dependencies related files 40 | COPY pyproject.toml poetry.lock ./ 41 | COPY cumplo_spotter ./cumplo_spotter 42 | 43 | # Define the service account key as a build argument. 44 | ARG CUMPLO_PYPI_BASE64_KEY 45 | 46 | # Set the service account key location to a temporary file. 47 | ENV GOOGLE_APPLICATION_CREDENTIALS=/tmp/service-account-credentials.json 48 | 49 | # Save the service account key contents from the build argument to the temporary file. 50 | RUN echo "$CUMPLO_PYPI_BASE64_KEY" | base64 -d> "$GOOGLE_APPLICATION_CREDENTIALS" 51 | 52 | # Install dependencies and the project globally 53 | RUN poetry install --without dev && \ 54 | rm -rf /root/.cache/pypoetry && \ 55 | rm -rf /tmp/poetry_cache && \ 56 | rm -rf "$GOOGLE_APPLICATION_CREDENTIALS" 57 | 58 | # ================================================================= 59 | 60 | # Final container with the app 61 | FROM base AS final 62 | 63 | # Copy global site-packages from builder 64 | COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages 65 | COPY --from=builder /usr/local/bin /usr/local/bin 66 | 67 | # Copy the rest of the code 68 | COPY . ./ 69 | 70 | # Run the app 71 | CMD exec uvicorn --workers 8 --host 0.0.0.0 --port 8080 cumplo_spotter.main:app 72 | -------------------------------------------------------------------------------- /Dockerfile.development: -------------------------------------------------------------------------------- 1 | # Base container with Python 3.12 official image 2 | FROM python:3.12-slim-bookworm AS base 3 | 4 | # Set up Python environment variables 5 | ENV LANG=C.UTF-8 \ 6 | PYTHONDONTWRITEBYTECODE=1 \ 7 | PYTHONHASHSEED=random \ 8 | PYTHONFAULTHANDLER=1 \ 9 | PYTHONUNBUFFERED=1 10 | 11 | # Set up base work directory 12 | WORKDIR /app 13 | 14 | # ================================================================= 15 | 16 | # Build container with Poetry 17 | FROM base AS builder 18 | 19 | # Set Poetry and pip environment variables 20 | ENV POETRY_VERSION=2.1.2 \ 21 | POETRY_NO_INTERACTION=1 \ 22 | POETRY_VIRTUALENVS_CREATE=false \ 23 | PIP_DISABLE_PIP_VERSION_CHECK=on \ 24 | PIP_DEFAULT_TIMEOUT=100 \ 25 | PIP_NO_CACHE_DIR=off 26 | 27 | # Install OS package dependencies 28 | RUN apt-get update && \ 29 | apt-get install -y libpq-dev gcc && \ 30 | apt-get clean && \ 31 | rm -rf /var/lib/apt/lists/* 32 | 33 | # Install Poetry 34 | RUN pip install "poetry==$POETRY_VERSION" 35 | 36 | # Install Keyrings support for Google Artifact Registry 37 | RUN poetry self add keyrings.google-artifactregistry-auth 38 | 39 | # Copy only the dependencies related files 40 | COPY pyproject.toml poetry.lock ./ 41 | COPY cumplo_spotter ./cumplo_spotter 42 | 43 | # Define the service account key as a build argument. 44 | ARG CUMPLO_PYPI_BASE64_KEY 45 | 46 | # Set the service account key location to a temporary file. 47 | ENV GOOGLE_APPLICATION_CREDENTIALS=/tmp/service-account-credentials.json 48 | 49 | # Save the service account key contents from the build argument to the temporary file. 50 | RUN echo "$CUMPLO_PYPI_BASE64_KEY" | base64 -d> "$GOOGLE_APPLICATION_CREDENTIALS" 51 | 52 | # Install dependencies and the project globally 53 | RUN poetry install --without dev && \ 54 | rm -rf /root/.cache/pypoetry && \ 55 | rm -rf /tmp/poetry_cache && \ 56 | rm -rf "$GOOGLE_APPLICATION_CREDENTIALS" 57 | 58 | # ================================================================= 59 | 60 | # Final container with the app 61 | FROM base AS final 62 | 63 | # Copy global site-packages from builder 64 | COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages 65 | COPY --from=builder /usr/local/bin /usr/local/bin 66 | 67 | # Copy the rest of the code 68 | COPY . ./ 69 | 70 | # Run the app 71 | CMD exec uvicorn --reload --host 0.0.0.0 --port 8080 cumplo_spotter.main:app 72 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/controller.py: -------------------------------------------------------------------------------- 1 | from concurrent.futures import ThreadPoolExecutor, as_completed 2 | from logging import getLogger 3 | 4 | from cachetools import TTLCache, cached 5 | from cumplo_common.models import FundingRequest 6 | 7 | from cumplo_spotter.integrations.cumplo.api_global import CumploGlobalAPI, GlobalFundingRequest 8 | from cumplo_spotter.models.cumplo import CumploFundingRequest 9 | from cumplo_spotter.utils.constants import CACHE_MAXSIZE, CUMPLO_CACHE_TTL 10 | 11 | logger = getLogger(__name__) 12 | cache = TTLCache(maxsize=CACHE_MAXSIZE, ttl=CUMPLO_CACHE_TTL) 13 | 14 | 15 | @cached(cache=cache) 16 | def get_available_funding_requests() -> list[FundingRequest]: 17 | """ 18 | Query the Cumplo's GraphQL API and returns a list of available funding requests. 19 | 20 | Returns: 21 | list[FundingRequest]: List of available funding requests 22 | 23 | """ 24 | logger.info("Getting funding requests from Cumplo API") 25 | 26 | funding_requests = [] 27 | global_funding_requests = CumploGlobalAPI.get_funding_requests(ignore_completed=True) 28 | logger.info(f"Found {len(global_funding_requests)} existing funding requests") 29 | 30 | with ThreadPoolExecutor(max_workers=25) as executor: 31 | funding_request_by_future = { 32 | executor.submit(_get_funding_request_details, global_funding_request): global_funding_request 33 | for global_funding_request in global_funding_requests 34 | } 35 | for future in as_completed(funding_request_by_future): 36 | global_funding_request = funding_request_by_future[future] 37 | details, simulation = future.result() 38 | 39 | data = {**details, **global_funding_request.model_dump(), "simulation": simulation} 40 | funding_request = CumploFundingRequest.model_validate(data) 41 | funding_request.borrower.id = global_funding_request.id_borrower 42 | 43 | if not funding_request.is_completed and funding_request.maximum_investment: 44 | funding_requests.append(funding_request.export()) 45 | 46 | logger.info(f"Got {len(funding_requests)} funding requests") 47 | return funding_requests 48 | 49 | 50 | def _get_funding_request_details(funding_request: GlobalFundingRequest) -> tuple[dict, dict]: 51 | """Request the details of a given funding request.""" 52 | details = CumploGlobalAPI.get_funding_request(funding_request.id) 53 | simulation = CumploGlobalAPI.simulate_funding_request(funding_request, details["fecha_vencimiento"]) 54 | return details, simulation 55 | -------------------------------------------------------------------------------- /cumplo_spotter/utils/constants.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dataclasses import dataclass 3 | 4 | from dotenv import load_dotenv 5 | 6 | load_dotenv() 7 | 8 | # Basics 9 | LOCATION = os.getenv("LOCATION", "us-central1") 10 | PROJECT_ID = os.getenv("PROJECT_ID") 11 | LOG_FORMAT = "\n%(levelname)s: %(message)s" 12 | IS_TESTING = bool(os.getenv("IS_TESTING")) 13 | 14 | # Selectors 15 | SUPPORTING_DOCUMENTS_XPATH = "//div[@class='loan-view-documents-section']//img/parent::span/following-sibling::span" 16 | AVERAGE_DAYS_DELINQUENT_SELECTOR = "div.loan-view-item span:nth-of-type(3)" 17 | IRS_SECTOR_SELECTOR = "strong.loan-view-primary-color + span" 18 | PAID_FUNDING_REQUESTS_COUNT_SELECTOR = "div.loan-view-item span:nth-of-type(1)" 19 | PAID_IN_TIME_PERCENTAGE_SELECTOR = "div.loan-view-item span:nth-of-type(5)" 20 | TOTAL_AMOUNT_REQUESTED_SELECTOR = "div.loan-view-page-subtitle + p" 21 | 22 | # Markers 23 | GOVERNMENT_TREASURY_DEBT_MARKER = [ 24 | "NO TIENE DEUDAS CON LA TGR", 25 | "NO PRESENTA DEUDAS CON LA TGR", 26 | "NO PRESENTA DEUDA CON LA TESORERIA", 27 | "NO PRESENTA DEUDAS CON LA TESORERIA", 28 | ] 29 | 30 | 31 | @dataclass 32 | class DicomMarker: 33 | BOTH_TRUE = "DEUDOR Y CLIENTE CON DICOM" 34 | BOTH_FALSE = "DEUDOR Y CLIENTE SIN DICOM" 35 | DEBTOR_TRUE = "DEUDOR CON DICOM" 36 | BORROWER_TRUE = ("CLIENTE CON DICOM", "CIENTE CON DICOM", "SOLICITANTE CON DICOM") 37 | BORROWER_FALSE = "SOLICITANTE SIN DICOM" 38 | SINGLE_FALSE = ("SIN DICOM", "TAMPOCO PRESENTA DICOM", "NO TIENE DICOM", "DICOM NO") 39 | SINGLE_TRUE = ("CON DICOM", "CONDICOM", "PRESENTA DICOM") 40 | 41 | 42 | # Firestore Collections 43 | USERS_COLLECTION = os.getenv("USERS_COLLECTION", "users") 44 | 45 | # Cumplo 46 | CREDIT_DETAIL_TITLE = os.getenv("CREDIT_DETAIL_TITLE", "INFORMACION DEL CREDITO") 47 | CUMPLO_BASE_URL = os.getenv("CUMPLO_BASE_URL") 48 | CUMPLO_HTML_API = os.getenv("CUMPLO_HTML_API", "") 49 | CUMPLO_GRAPHQL_API = os.getenv("CUMPLO_GRAPHQL_API", "") 50 | CUMPLO_GRAPHQL_HEADERS = {"Accept-Language": "es-CL"} 51 | CUMPLO_GLOBAL_API = os.getenv("CUMPLO_GLOBAL_API", "") 52 | CUMPLO_GLOBAL_API_FUNDING_REQUESTS = os.getenv("CUMPLO_GLOBAL_API_FUNDING_REQUESTS", "") 53 | CUMPLO_GLOBAL_API_SIMULATION = os.getenv("CUMPLO_GLOBAL_API_SIMULATION", "") 54 | CUMPLO_GLOBAL_API_DETAILS = os.getenv("CUMPLO_GLOBAL_API_DETAILS", "") 55 | UPFRONT_FEE_KEY = os.getenv("UPFRONT_FEE_KEY", "COMISION ENTRADA") 56 | EXIT_FEE_KEY = os.getenv("EXIT_FEE_KEY", "COMISION SALIDA") 57 | SIMULATION_AMOUNT = int(os.getenv("SIMULATION_AMOUNT", "1000000")) 58 | 59 | # Defaults 60 | DEFAULT_FILTER_NOTIFIED = bool(os.getenv("DEFAULT_FILTER_NOTIFIED")) 61 | DEFAULT_EXPIRATION_MINUTES = int(os.getenv("DEFAULT_EXPIRATION_MINUTES", "30")) 62 | 63 | # Cache 64 | CACHE_MAXSIZE = int(os.getenv("CACHE_MAXSIZE", "1000")) 65 | CUMPLO_CACHE_TTL = int(os.getenv("CUMPLO_CACHE_TTL", "120")) 66 | -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/simulation.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from typing import Self 3 | 4 | from cumplo_common.utils.text import clean_text 5 | from pydantic import BaseModel, Field, model_validator 6 | 7 | from cumplo_spotter.utils.constants import EXIT_FEE_KEY, SIMULATION_AMOUNT, UPFRONT_FEE_KEY 8 | 9 | 10 | class CumploSimulationInstallment(BaseModel): 11 | capital: int = Field(..., alias="capital") 12 | interest: int = Field(..., alias="interes") 13 | amount: int = Field(..., alias="montoPagar") 14 | exit_fee: int = Field(..., alias="feeSalida") 15 | date: datetime = Field(..., alias="fechaPago") 16 | 17 | @model_validator(mode="before") 18 | @classmethod 19 | def round_values(cls, values: dict) -> dict: 20 | """Round the amount and interest values.""" 21 | for key in ["montoPagar", "interes", "capital", "feeSalida"]: 22 | if key in values: 23 | values[key] = round(values[key]) 24 | return values 25 | 26 | @model_validator(mode="after") 27 | def adjust_amount(self) -> Self: 28 | """Subtract exit fee from amount after model instantiation.""" 29 | self.amount -= self.exit_fee # NOTE: The exit fee has to be subtracted from the received amount 30 | return self 31 | 32 | 33 | class CumploFundingRequestSimulation(BaseModel): 34 | exit_fee: int = Field(...) 35 | upfront_fee: int = Field(...) 36 | net_returns: int = Field(...) 37 | installments: list[CumploSimulationInstallment] = Field(default_factory=list) 38 | 39 | @model_validator(mode="before") 40 | @classmethod 41 | def format_values(cls, values: dict) -> dict: 42 | """Format the values to the expected format.""" 43 | return cls._unpack_simulation(values) 44 | 45 | @staticmethod 46 | def _unpack_simulation(values: dict) -> dict: 47 | """Unpack the simulation values.""" 48 | result = {"net_returns": round(values["ganancia_liquida"])} 49 | 50 | for cost in values["costos"]["valores"]: 51 | if UPFRONT_FEE_KEY in clean_text(cost["nombre"]): 52 | result["upfront_fee"] = round(cost["valor"]) 53 | elif EXIT_FEE_KEY in clean_text(cost["nombre"]): 54 | result["exit_fee"] = round(cost["valor"]) 55 | 56 | if values.get("cuotas"): 57 | result["installments"] = values["cuotas"] 58 | else: 59 | installment = values["forma_pago"][0] 60 | result["installments"] = [ 61 | { 62 | "capital": SIMULATION_AMOUNT, 63 | "feeSalida": result["exit_fee"], 64 | "interes": installment["interes"], 65 | "montoPagar": installment["monto_cuota"], 66 | "fechaPago": installment["fecha_vencimiento"], 67 | } 68 | ] 69 | 70 | return result 71 | -------------------------------------------------------------------------------- /cumplo_spotter/routers/funding_requests/public.py: -------------------------------------------------------------------------------- 1 | from http import HTTPStatus 2 | from logging import getLogger 3 | from typing import cast 4 | 5 | from cumplo_common.integrations.cloud_pubsub import CloudPubSub 6 | from cumplo_common.models import FundingRequest, PrivateEvent, User 7 | from fastapi import APIRouter, HTTPException 8 | from fastapi.requests import Request 9 | 10 | from cumplo_spotter.business import funding_requests 11 | 12 | logger = getLogger(__name__) 13 | 14 | 15 | router = APIRouter(prefix="/funding-requests") 16 | 17 | 18 | @router.get("", status_code=HTTPStatus.OK) 19 | def _get_funding_requests(_request: Request) -> list[dict]: 20 | """Get a list of available funding requests.""" 21 | available_funding_requests = funding_requests.get_available() 22 | return [funding_request.json() for funding_request in available_funding_requests] 23 | 24 | 25 | @router.get("/promising", status_code=HTTPStatus.OK) 26 | def _get_promising_funding_requests(request: Request) -> list[dict]: 27 | """Get a list of promising funding requests based on the user's configuration.""" 28 | user = cast(User, request.state.user) 29 | promising_funding_requests = funding_requests.get_promising(user) 30 | return [request.json() for request in promising_funding_requests] 31 | 32 | 33 | @router.get("/{id_funding_request}", status_code=HTTPStatus.OK) 34 | def _get_funding_request(id_funding_request: int) -> dict: 35 | """ 36 | Get a funding request by its ID. 37 | 38 | Raises: 39 | HTTPException: If the funding request is not found. 40 | 41 | """ 42 | available_funding_requests = funding_requests.get_available() 43 | for funding_request in available_funding_requests: 44 | if funding_request.id == id_funding_request: 45 | return funding_request.json() 46 | 47 | raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=f"Funding request {id_funding_request} not found") 48 | 49 | 50 | @router.post(path="/filter", status_code=HTTPStatus.NO_CONTENT) 51 | def _filter_funding_requests(request: Request, payload: list[FundingRequest]) -> None: 52 | """Filter a list of funding requests based on the user's filters.""" 53 | user = cast(User, request.state.user) 54 | promising_funding_requests = set() if user.filters else set(payload) 55 | 56 | for filter_ in user.filters.values(): 57 | promising_funding_requests.update(funding_requests.filter_(list(payload), filter_)) 58 | 59 | if not promising_funding_requests: 60 | logger.info(f"No promising funding requests for user {user.id}") 61 | return 62 | 63 | logger.info(f"Found {len(promising_funding_requests)} promising funding requests for user {user.id}") 64 | 65 | for funding_request in promising_funding_requests: 66 | logger.info(f"Notifying about funding request {funding_request.id} to user {user.id}") 67 | CloudPubSub.publish(funding_request.json(), PrivateEvent.FUNDING_REQUEST_PROMISING, id_user=str(user.id)) 68 | -------------------------------------------------------------------------------- /cumplo_spotter/business/funding_requests.py: -------------------------------------------------------------------------------- 1 | from logging import getLogger 2 | 3 | from cumplo_common.models import FilterConfiguration, FundingRequest, User 4 | 5 | from cumplo_spotter.integrations import cumplo 6 | from cumplo_spotter.models.filter import ( 7 | CreditTypeFilter, 8 | DicomFilter, 9 | MaximumDurationFilter, 10 | MinimumAmountFilter, 11 | MinimumDurationFilter, 12 | MinimumInvestmentFilter, 13 | MinimumIRRFilter, 14 | MinimumMonthlyProfitFilter, 15 | MinimumScoreFilter, 16 | PortfolioFilter, 17 | ) 18 | 19 | logger = getLogger(__name__) 20 | 21 | 22 | def get_available() -> list[FundingRequest]: 23 | """ 24 | Get a list of available funding requests sorted by monthly profit rate. 25 | 26 | Returns: 27 | list[dict]: List of available funding requests 28 | 29 | """ 30 | funding_requests = cumplo.get_available_funding_requests() 31 | funding_requests.sort(key=lambda x: x.monthly_profit_rate, reverse=True) 32 | return funding_requests 33 | 34 | 35 | def get_promising(user: User) -> list[FundingRequest]: 36 | """ 37 | Get a list of promising funding requests based on the user's configuration sorted by monthly profit rate. 38 | 39 | Args: 40 | user (User): User to get the configuration from 41 | 42 | Returns: 43 | list[FundingRequest]: List of promising funding requests 44 | 45 | """ 46 | funding_requests = cumplo.get_available_funding_requests() 47 | 48 | promising_requests = set() 49 | for configuration in user.filters.values(): 50 | promising_requests.update(filter_(funding_requests, configuration)) 51 | 52 | return sorted(promising_requests, key=lambda x: x.monthly_profit_rate, reverse=True) 53 | 54 | 55 | def filter_(funding_requests: list[FundingRequest], configuration: FilterConfiguration) -> list[FundingRequest]: 56 | """ 57 | Filter a list of funding requests based on the user's filter. 58 | 59 | Args: 60 | funding_requests (list[FundingRequest]): List of funding requests 61 | configuration (FilterConfiguration): User's filter 62 | 63 | Returns: 64 | list[FundingRequest]: Filtered funding requests 65 | 66 | """ 67 | filters = [ 68 | MinimumAmountFilter(configuration), 69 | CreditTypeFilter(configuration), 70 | MinimumInvestmentFilter(configuration), 71 | MinimumScoreFilter(configuration), 72 | MinimumIRRFilter(configuration), 73 | MinimumMonthlyProfitFilter(configuration), 74 | DicomFilter(configuration), 75 | MinimumDurationFilter(configuration), 76 | MaximumDurationFilter(configuration), 77 | PortfolioFilter(configuration), 78 | ] 79 | 80 | logger.info(f"Applying {len(filters)} filters to {len(funding_requests)} funding requests") 81 | funding_requests = list(filter(lambda x: all(f.apply(x) for f in filters), funding_requests)) 82 | 83 | logger.info(f"Got {len(funding_requests)} funding requests after applying filter {configuration.name}") 84 | return funding_requests 85 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/api_graphql.py: -------------------------------------------------------------------------------- 1 | from http import HTTPMethod 2 | from logging import getLogger 3 | 4 | import requests 5 | from retry import retry 6 | 7 | from cumplo_spotter.utils.constants import CUMPLO_GRAPHQL_API, CUMPLO_GRAPHQL_HEADERS 8 | 9 | logger = getLogger(__name__) 10 | 11 | 12 | class CumploGraphQLAPI: 13 | """Class to interact with Cumplo's GraphQL API.""" 14 | 15 | url = CUMPLO_GRAPHQL_API 16 | headers = CUMPLO_GRAPHQL_HEADERS 17 | 18 | @classmethod 19 | def _request(cls, method: HTTPMethod, payload: dict | None = None) -> requests.Response: 20 | """ 21 | Make a request to Cumplo's GraphQL API. 22 | 23 | Args: 24 | method (HTTPMethod): HTTP method to use 25 | payload (dict): Payload to send 26 | 27 | Returns: 28 | requests.Response: Response from the API 29 | 30 | """ 31 | return requests.request(method=method, url=cls.url, json=payload, headers=cls.headers) 32 | 33 | @classmethod 34 | @retry((KeyError, requests.exceptions.JSONDecodeError), tries=5, delay=1) 35 | def get_funding_requests(cls, *, ignore_completed: bool = False) -> list[dict]: 36 | """ 37 | Query the Cumplo's GraphQL API for the existing funding requests. 38 | 39 | Returns: 40 | list[dict]: List of the existing funding requests 41 | 42 | """ 43 | logger.debug("Getting funding requests from Cumplo's GraphQL API") 44 | payload = cls._build_funding_requests_query() 45 | response = cls._request(HTTPMethod.POST, payload) 46 | data = response.json()["data"]["fundingRequests"] 47 | 48 | if data["allCompleted"] and ignore_completed: 49 | logger.info("All funding requests are completed. Ignoring them") 50 | return [] 51 | 52 | # NOTE: This is a compatibility fix to not overwrite a field with the same name from the Global API 53 | for element in data["results"]: 54 | element["operacion"]["tipo_credito"] = element["operacion"].pop("tipo_respaldo") 55 | 56 | return data["results"] 57 | 58 | @staticmethod 59 | def _build_funding_requests_query(limit: int = 50, page: int = 1) -> dict: 60 | """Build the GraphQL query to fetch funding requests.""" 61 | return { 62 | "operationName": "FundingRequests", 63 | "variables": {"page": page, "limit": limit, "ordering": "-operacion.id", "state": 1}, 64 | "query": """ 65 | query FundingRequests($page: Int!, $limit: Int!, $state: Int, $ordering: String) { 66 | fundingRequests(page: $page, limit: $limit, state: $state, ordering: $ordering) { 67 | count allCompleted results { 68 | empresa { 69 | id 70 | } 71 | operacion { 72 | id 73 | score 74 | tipo_respaldo 75 | } 76 | } 77 | } 78 | } 79 | """, 80 | } 81 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "cumplo-spotter" 3 | version = "1.6.2" 4 | description = "A simple yet powerful API for spotting secure and high-return Cumplo investment opportunities" 5 | authors = ["Cristobal Sfeir "] 6 | packages = [{ include = "cumplo_spotter" }] 7 | 8 | [tool.poetry.dependencies] 9 | python = "^3.12" 10 | requests = "^2.28.1" 11 | arrow = "^1.2.3" 12 | pydantic = "^2.1.1" 13 | fastapi = "^0.109.1" 14 | uvicorn = "^0.23.1" 15 | gunicorn = "^21.2.0" 16 | python-dotenv = "^1.0.0" 17 | bs4 = "^0.0.1" 18 | retry = "^0.9.2" 19 | lxml = "^4.9.2" 20 | google-cloud-logging = "^3.5.0" 21 | httpx = "^0.26.0" 22 | cumplo-common = { version = "^1.12.7", source = "cumplo-pypi" } 23 | cachetools = "^5.5.0" 24 | 25 | [tool.poetry.group.dev.dependencies] 26 | mypy = "^1.13.0" 27 | ruff = "^0.7.1" 28 | docformatter = "^1.7.5" 29 | 30 | [[tool.poetry.source]] 31 | name = "cumplo-pypi" 32 | url = "https://us-central1-python.pkg.dev/cumplo-scraper/cumplo-pypi/simple/" 33 | priority = "supplemental" 34 | 35 | [build-system] 36 | requires = ["poetry-core>=1.0.0"] 37 | build-backend = "poetry.core.masonry.api" 38 | 39 | [tool.mypy] 40 | python_version = "3.12" 41 | disallow_untyped_defs = true 42 | exclude = ".venv" 43 | 44 | [[tool.mypy.overrides]] 45 | module = [ 46 | "requests.*", 47 | "pydantic.*", 48 | "psycopg2.*", 49 | "lxml.*", 50 | "babel.*", 51 | "bs4.*", 52 | "functions_framework.*", 53 | "firebase_admin.*", 54 | "retry.*", 55 | "starlette.*", 56 | "cachetools.*", 57 | ] 58 | ignore_missing_imports = true 59 | 60 | [tool.ruff] 61 | line-length = 120 62 | target-version = "py312" 63 | preview = true 64 | 65 | [tool.ruff.lint] 66 | select = ["ALL"] 67 | ignore = [ 68 | "ANN101", # Missing type annotation for self in method 69 | "ANN102", # Missing type annotation for cls in method 70 | "ANN401", # Dynamically typed expressions (typing.Any) are disallowed 71 | "D100", # Missing docstring in public module 72 | "D107", # Missing docstring in __init__ 73 | "D105", # Missing docstring in magic method 74 | "D212", # Multi-line docstring summary should start at the second line 75 | "D203", # One blank line required before class docstring 76 | "D101", # Missing docstring in public class 77 | "D104", # Missing docstring in public package 78 | "G004", # Logging statement uses string formatting 79 | "S113", # Use of requests call without timeout 80 | "DOC201", # Missing documentation for `return` in docstring 81 | "COM812", # Missing trailing comma in a dictionary 82 | "ISC001", # Implicit string concatenation 83 | "CPY001", # Copying notice 84 | ] 85 | 86 | [tool.ruff.lint.per-file-ignores] 87 | "__init__.py" = ["F401"] 88 | 89 | [tool.ruff.format] 90 | docstring-code-format = false 91 | docstring-code-line-length = 120 92 | 93 | [tool.docformatter] 94 | pre-summary-newline = true # Ensures that multiline docstrings start on a new line. 95 | wrap-descriptions = 120 # Wraps descriptions at 114 characters, ensuring consistent line width. 96 | wrap-summaries = 120 # Wraps summary lines only if they exceed 114 characters. 97 | recursive = true # Recursively formats all Python files in the specified directories. 98 | blank = true # Adds a blank line before the end of multiline docstrings. 99 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | // General 3 | "editor.suggestSelection": "first", 4 | "editor.defaultFormatter": null, 5 | "editor.formatOnSave": true, 6 | "editor.formatOnPaste": true, 7 | "editor.rulers": [ 8 | 80, 9 | 120 10 | ], 11 | "editor.tabSize": 4, 12 | "editor.codeActionsOnSave": { 13 | "source.organizeImports": "explicit" 14 | }, 15 | // Python 16 | "[python]": { 17 | "editor.defaultFormatter": "charliermarsh.ruff", 18 | "files.trimTrailingWhitespace": true, 19 | "files.insertFinalNewline": true, 20 | "files.trimFinalNewlines": true, 21 | "editor.formatOnSave": true, 22 | "editor.formatOnType": true 23 | }, 24 | // Native type hints 25 | "python.analysis.typeCheckingMode": "off", 26 | "python.terminal.activateEnvInCurrentTerminal": true, 27 | "python.analysis.completeFunctionParens": true, 28 | "python.analysis.useLibraryCodeForTypes": true, 29 | "python.analysis.autoImportCompletions": true, 30 | "python.analysis.inlayHints.variableTypes": true, 31 | "python.analysis.inlayHints.functionReturnTypes": true, 32 | "python.analysis.inlayHints.callArgumentNames": "all", 33 | "python.analysis.gotoDefinitionInStringLiteral": true, 34 | // MyPy 35 | "mypy.runUsingActiveInterpreter": true, 36 | "mypy.configFile": "pyproject.toml", 37 | // Run On Save 38 | "emeraldwalk.runonsave": { 39 | "commands": [ 40 | { 41 | "match": "\\.py$", 42 | "cmd": "docformatter -i ${file}" 43 | } 44 | ] 45 | }, 46 | // Todo Tree 47 | "todo-tree.general.tags": [ 48 | "HACK", 49 | "FIXME", 50 | "TODO", 51 | "REVIEW", 52 | "OPTIMIZE", 53 | "NOTE" 54 | ], 55 | "todo-tree.highlights.defaultHighlight": { 56 | "gutterIcon": true, 57 | "type": "tag-and-comment" 58 | }, 59 | "todo-tree.highlights.customHighlight": { 60 | "HACK": { 61 | "icon": "tools", 62 | "rulerColour": "#F9992C", 63 | "iconColour": "#F9992C", 64 | "foreground": "#F9992C", 65 | "background": "#FFFFFF00", 66 | }, 67 | "FIXME": { 68 | "icon": "alert", 69 | "rulerColour": "#FF5B5B", 70 | "iconColour": "#FF5B5B", 71 | "foreground": "#FF5B5B", 72 | "background": "#FFFFFF00", 73 | }, 74 | "TODO": { 75 | "icon": "checkbox", 76 | "rulerColour": "#1A9EFD", 77 | "iconColour": "#1A9EFD", 78 | "foreground": "#1A9EFD", 79 | "background": "#FFFFFF00", 80 | }, 81 | "REVIEW": { 82 | "icon": "comment-discussion", 83 | "rulerColour": "#08C45D", 84 | "iconColour": "#08C45D", 85 | "foreground": "#08C45D", 86 | "background": "#FFFFFF00", 87 | }, 88 | "OPTIMIZE": { 89 | "icon": "zap", 90 | "rulerColour": "#FFDE0D", 91 | "iconColour": "#FFDE0D", 92 | "foreground": "#FFDE0D", 93 | "background": "#FFFFFF00", 94 | }, 95 | "NOTE": { 96 | "icon": "bookmark", 97 | "rulerColour": "#D876FF", 98 | "iconColour": "#D876FF", 99 | "foreground": "#D876FF", 100 | "background": "#FFFFFF00", 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/debtor.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from decimal import Decimal 3 | from typing import Any, ClassVar 4 | 5 | from cumplo_common.models import PortfolioCategory 6 | from cumplo_common.utils.text import clean_text 7 | from pydantic import BaseModel, Field, field_validator 8 | 9 | from .portfolio import Portfolio 10 | 11 | 12 | class DebtorPortfolio(Portfolio): 13 | PORTFOLIO_STATUS_MAPPING: ClassVar[dict] = { 14 | # ON TIME 15 | "cantidad_pagadas_plazo_normal_pagador": {"status": PortfolioCategory.ON_TIME, "type": "count"}, 16 | "monto_pagadas_plazo_normal_pagador": {"status": PortfolioCategory.ON_TIME, "type": "amount"}, 17 | # CURED 18 | "cantidad_pagadas_en_mora_pagador": {"status": PortfolioCategory.CURED, "type": "count"}, 19 | "monto_pagadas_en_mora_pagador": {"status": PortfolioCategory.CURED, "type": "amount"}, 20 | # ACTIVE 21 | "cantidad_operaciones_activas_pagador": {"status": PortfolioCategory.ACTIVE, "type": "count"}, 22 | "monto_operaciones_activas_pagador": {"status": PortfolioCategory.ACTIVE, "type": "amount"}, 23 | # OVERDUE 24 | "cantidad_operaciones_mora_menor_30_pagador": {"status": PortfolioCategory.OVERDUE, "type": "count"}, 25 | "monto_operaciones_mora_menor_30_pagador": {"status": PortfolioCategory.OVERDUE, "type": "amount"}, 26 | # DELINQUENT 27 | "cantidad_operaciones_mora_mayor_30_pagador": {"status": PortfolioCategory.DELINQUENT, "type": "count"}, 28 | "monto_operaciones_mora_mayor_30_pagador": {"status": PortfolioCategory.DELINQUENT, "type": "amount"}, 29 | # PAID 30 | "cantidad_pagadas_pagador": {"status": PortfolioCategory.PAID, "type": "count"}, 31 | "monto_pagadas_pagador": {"status": PortfolioCategory.PAID, "type": "amount"}, 32 | # TOTAL 33 | "cantidad_total_pagador": {"status": PortfolioCategory.TOTAL, "type": "count"}, 34 | "monto_total_pagador": {"status": PortfolioCategory.TOTAL, "type": "amount"}, 35 | # OUTSTANDING 36 | "cantidad_vigentes_pagador": {"status": PortfolioCategory.OUTSTANDING, "type": "count"}, 37 | "monto_vigentes_pagador": {"status": PortfolioCategory.OUTSTANDING, "type": "amount"}, 38 | } 39 | 40 | 41 | class Debtor(BaseModel): 42 | share: Decimal = Field(..., alias="participacion") 43 | name: str | None = Field(None, alias="nombre_pagador") 44 | economic_sector: str | None = Field(None, alias="giro_detalle") 45 | portfolio: DebtorPortfolio = Field(..., alias="historial") 46 | description: str | None = Field(..., alias="descripcion") 47 | first_appearance: datetime | None = Field(None, alias="fecha_primera_operacion") 48 | dicom: bool | None = Field(None) 49 | 50 | @field_validator("name", mode="before") 51 | @classmethod 52 | def _format_name(cls, value: Any) -> str | None: 53 | """Clean the value and checks if the name is empty and returns None.""" 54 | return clean_text(value) or None 55 | 56 | @field_validator("description", mode="before") 57 | @classmethod 58 | def _format_description(cls, value: Any) -> str | None: 59 | """Clean the value and checks if the description is empty and return None.""" 60 | return clean_text(value) or None 61 | 62 | @field_validator("economic_sector", mode="before") 63 | @classmethod 64 | def _format_economic_sector(cls, value: Any) -> str | None: 65 | """Clean the value and checks if the economic sector is 'null' and return None.""" 66 | clean_value = clean_text(value) 67 | return None if clean_value == "NULL" else clean_value 68 | -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/borrower.py: -------------------------------------------------------------------------------- 1 | # mypy: disable-error-code="call-overload" 2 | 3 | from datetime import datetime 4 | from typing import Any, ClassVar 5 | 6 | from cumplo_common.models import PortfolioCategory 7 | from cumplo_common.utils.text import clean_text 8 | from pydantic import BaseModel, Field, field_validator 9 | 10 | from .portfolio import Portfolio 11 | 12 | 13 | class BorrowerPortfolio(Portfolio): 14 | PORTFOLIO_STATUS_MAPPING: ClassVar[dict] = { 15 | # ON TIME 16 | "cantidad_pagadas_plazo_normal_solicitante": {"status": PortfolioCategory.ON_TIME, "type": "count"}, 17 | "monto_pagadas_plazo_normal_solicitante": {"status": PortfolioCategory.ON_TIME, "type": "amount"}, 18 | "porcentaje_pagado_plazo_normal": {"status": PortfolioCategory.ON_TIME, "type": "percentage"}, 19 | # CURED 20 | "cantidad_pagadas_en_mora_solicitante": {"status": PortfolioCategory.CURED, "type": "count"}, 21 | "monto_pagadas_en_mora_solicitante": {"status": PortfolioCategory.CURED, "type": "amount"}, 22 | "porcentaje_pagado_mora": {"status": PortfolioCategory.CURED, "type": "percentage"}, 23 | # ACTIVE 24 | "cantidad_operaciones_activas_solicitante": {"status": PortfolioCategory.ACTIVE, "type": "count"}, 25 | "monto_operaciones_activas_solicitante": {"status": PortfolioCategory.ACTIVE, "type": "amount"}, 26 | "porcentaje_monto_activo": {"status": PortfolioCategory.ACTIVE, "type": "percentage"}, 27 | # OVERDUE 28 | "cantidad_operaciones_mora_menor_30_solicitante": {"status": PortfolioCategory.OVERDUE, "type": "count"}, 29 | "monto_operaciones_mora_menor_30_solicitante": {"status": PortfolioCategory.OVERDUE, "type": "amount"}, 30 | "porcentaje_en_mora_menor_30": {"status": PortfolioCategory.OVERDUE, "type": "percentage"}, 31 | # DELINQUENT 32 | "cantidad_operaciones_mora_mayor_30_solicitante": {"status": PortfolioCategory.DELINQUENT, "type": "count"}, 33 | "monto_operaciones_mora_mayor_30_solicitante": {"status": PortfolioCategory.DELINQUENT, "type": "amount"}, 34 | "porcentaje_en_mora_mayor_30": {"status": PortfolioCategory.DELINQUENT, "type": "percentage"}, 35 | # PAID 36 | "cantidad_pagadas_solicitante": {"status": PortfolioCategory.PAID, "type": "count"}, 37 | "monto_pagadas_solicitante": {"status": PortfolioCategory.PAID, "type": "amount"}, 38 | # TOTAL 39 | "cantidad_total_solicitante": {"status": PortfolioCategory.TOTAL, "type": "count"}, 40 | "monto_total_solicitante": {"status": PortfolioCategory.TOTAL, "type": "amount"}, 41 | # OUTSTANDING 42 | "cantidad_vigentes_solicitante": {"status": PortfolioCategory.OUTSTANDING, "type": "count"}, 43 | "monto_vigentes_solicitante": {"status": PortfolioCategory.OUTSTANDING, "type": "amount"}, 44 | } 45 | 46 | 47 | class Borrower(BaseModel): 48 | id: int | None = Field(None) 49 | name: str | None = Field(None, alias="nombre_solicitante") 50 | average_days_delinquent: int | None = Field(None) 51 | economic_sector: str | None = Field(None, alias="giro_detalle") 52 | description: str | None = Field(..., alias="descripcion") 53 | portfolio: BorrowerPortfolio = Field(..., alias="historial") 54 | first_appearance: datetime | None = Field(None, alias="fecha_primera_operacion") 55 | dicom: bool | None = Field(None) 56 | 57 | @field_validator("description", "name", mode="before") 58 | @classmethod 59 | def _format_text_field(cls, value: Any) -> str | None: 60 | """Clean the text value and return None if empty.""" 61 | return clean_text(value) or None 62 | 63 | @field_validator("economic_sector", mode="before") 64 | @classmethod 65 | def _format_economic_sector(cls, value: Any) -> str | None: 66 | """Clean the value and checks if the economic sector is 'null' and return None.""" 67 | clean_value = clean_text(value) 68 | return None if clean_value == "NULL" else clean_value 69 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode 2 | 3 | ### Linux ### 4 | *~ 5 | 6 | # temporary files which can be created if a process still has a handle open of a deleted file 7 | .fuse_hidden* 8 | 9 | # KDE directory preferences 10 | .directory 11 | 12 | # Linux trash folder which might appear on any partition or disk 13 | .Trash-* 14 | 15 | # .nfs files are created when an open file is removed but is still being accessed 16 | .nfs* 17 | 18 | ### OSX ### 19 | *.DS_Store 20 | .AppleDouble 21 | .LSOverride 22 | 23 | # Icon must end with two \r 24 | Icon 25 | 26 | # Thumbnails 27 | ._* 28 | 29 | # Files that might appear in the root of a volume 30 | .DocumentRevisions-V100 31 | .fseventsd 32 | .Spotlight-V100 33 | .TemporaryItems 34 | .Trashes 35 | .VolumeIcon.icns 36 | .com.apple.timemachine.donotpresent 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | 45 | ### PyCharm ### 46 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 47 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 48 | 49 | # User-specific stuff: 50 | .idea/**/workspace.xml 51 | .idea/**/tasks.xml 52 | .idea/dictionaries 53 | 54 | # Sensitive or high-churn files: 55 | .idea/**/dataSources/ 56 | .idea/**/dataSources.ids 57 | .idea/**/dataSources.xml 58 | .idea/**/dataSources.local.xml 59 | .idea/**/sqlDataSources.xml 60 | .idea/**/dynamic.xml 61 | .idea/**/uiDesigner.xml 62 | 63 | # Gradle: 64 | .idea/**/gradle.xml 65 | .idea/**/libraries 66 | 67 | # CMake 68 | cmake-build-debug/ 69 | 70 | # Mongo Explorer plugin: 71 | .idea/**/mongoSettings.xml 72 | 73 | ## File-based project format: 74 | *.iws 75 | 76 | ## Plugin-specific files: 77 | 78 | # IntelliJ 79 | /out/ 80 | 81 | # mpeltonen/sbt-idea plugin 82 | .idea_modules/ 83 | 84 | # JIRA plugin 85 | atlassian-ide-plugin.xml 86 | 87 | # Cursive Clojure plugin 88 | .idea/replstate.xml 89 | 90 | # Ruby plugin and RubyMine 91 | /.rakeTasks 92 | 93 | # Crashlytics plugin (for Android Studio and IntelliJ) 94 | com_crashlytics_export_strings.xml 95 | crashlytics.properties 96 | crashlytics-build.properties 97 | fabric.properties 98 | 99 | ### PyCharm Patch ### 100 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 101 | 102 | # *.iml 103 | # modules.xml 104 | # .idea/misc.xml 105 | # *.ipr 106 | 107 | ### Python ### 108 | # Byte-compiled / optimized / DLL files 109 | __pycache__/ 110 | *.py[cod] 111 | *$py.class 112 | 113 | # C extensions 114 | *.so 115 | 116 | # Distribution / packaging 117 | .Python 118 | build/ 119 | develop-eggs/ 120 | dist/ 121 | downloads/ 122 | eggs/ 123 | .eggs/ 124 | lib/ 125 | lib64/ 126 | parts/ 127 | sdist/ 128 | var/ 129 | wheels/ 130 | *.egg-info/ 131 | .installed.cfg 132 | *.egg 133 | 134 | # PyInstaller 135 | # Usually these files are written by a python script from a template 136 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 137 | *.manifest 138 | *.spec 139 | 140 | # Installer logs 141 | pip-log.txt 142 | pip-delete-this-directory.txt 143 | 144 | # Unit test / coverage reports 145 | htmlcov/ 146 | .tox/ 147 | .coverage 148 | .coverage.* 149 | .cache 150 | .pytest_cache/ 151 | nosetests.xml 152 | coverage.xml 153 | *.cover 154 | .hypothesis/ 155 | /tests/conftest.py 156 | 157 | # Translations 158 | *.mo 159 | *.pot 160 | 161 | # Flask stuff: 162 | instance/ 163 | .webassets-cache 164 | 165 | # Scrapy stuff: 166 | .scrapy 167 | 168 | # Sphinx documentation 169 | docs/_build/ 170 | 171 | # PyBuilder 172 | target/ 173 | 174 | # Jupyter Notebook 175 | .ipynb_checkpoints 176 | 177 | # pyenv 178 | .python-version 179 | 180 | # celery beat schedule file 181 | celerybeat-schedule.* 182 | 183 | # SageMath parsed files 184 | *.sage.py 185 | 186 | # Environments 187 | .env 188 | .venv 189 | env/ 190 | venv/ 191 | ENV/ 192 | env.bak/ 193 | venv.bak/ 194 | clayenv/ 195 | # Spyder project settings 196 | .spyderproject 197 | .spyproject 198 | 199 | # Rope project settings 200 | .ropeproject 201 | 202 | # mkdocs documentation 203 | /site 204 | 205 | # mypy 206 | .mypy_cache/ 207 | 208 | ### VisualStudioCode ### 209 | .vscode/* 210 | !.vscode/settings.json 211 | !.vscode/tasks.json 212 | !.vscode/launch.json 213 | !.vscode/extensions.json 214 | .history 215 | 216 | ### Windows ### 217 | # Windows thumbnail cache files 218 | Thumbs.db 219 | ehthumbs.db 220 | ehthumbs_vista.db 221 | 222 | # Folder config file 223 | Desktop.ini 224 | 225 | # Recycle Bin used on file shares 226 | $RECYCLE.BIN/ 227 | 228 | # Windows Installer files 229 | *.cab 230 | *.msi 231 | *.msm 232 | *.msp 233 | 234 | # Windows shortcuts 235 | *.lnk 236 | 237 | # Build folder 238 | 239 | */build/* 240 | /webdriver 241 | ghostdriver.log 242 | 243 | # Webdriver Manager 244 | .wdm 245 | 246 | # Google Credentials 247 | credentials.json 248 | 249 | 250 | # End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocodeß 251 | 252 | cumplo-spotter-credentials.json 253 | cumplo-pypi-credentials.json 254 | -------------------------------------------------------------------------------- /cumplo_spotter/integrations/cumplo/api_global.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | from functools import cached_property 3 | from http import HTTPMethod 4 | from logging import getLogger 5 | from typing import Any 6 | 7 | import requests 8 | from cumplo_common.models import Currency 9 | from pydantic import BaseModel, Field, field_validator 10 | from retry import retry 11 | 12 | from cumplo_spotter.models.cumplo.funding_request import CumploCreditType 13 | from cumplo_spotter.models.cumplo.request_duration import CumploFundingRequestDuration 14 | from cumplo_spotter.utils.constants import ( 15 | CUMPLO_GLOBAL_API, 16 | CUMPLO_GLOBAL_API_DETAILS, 17 | CUMPLO_GLOBAL_API_FUNDING_REQUESTS, 18 | CUMPLO_GLOBAL_API_SIMULATION, 19 | SIMULATION_AMOUNT, 20 | ) 21 | 22 | logger = getLogger(__name__) 23 | 24 | 25 | class GlobalFundingRequest(BaseModel): 26 | id: int = Field(...) 27 | score: Decimal = Field(...) 28 | irr: Decimal = Field(..., alias="tir") 29 | currency: Currency = Field(..., alias="moneda") 30 | duration: CumploFundingRequestDuration = Field(..., alias="plazo") 31 | raised_percentage: Decimal = Field(..., alias="porcentaje_inversion") 32 | credit_type: CumploCreditType = Field(...) 33 | id_borrower: int | None = Field(None) 34 | 35 | @field_validator("raised_percentage", mode="before") 36 | @classmethod 37 | def raised_percentage_validator(cls, value: Any) -> Decimal: 38 | """Validate that the raised percentage is a valid decimal number.""" 39 | return round(Decimal(int(value) / 100), 2) 40 | 41 | @cached_property 42 | def is_completed(self) -> bool: 43 | """Check if the funding request is fully funded.""" 44 | return self.raised_percentage == Decimal(1) 45 | 46 | 47 | class CumploGlobalAPI: 48 | """Class to interact with Cumplo's Global API.""" 49 | 50 | url = CUMPLO_GLOBAL_API 51 | 52 | @classmethod 53 | def _request(cls, method: HTTPMethod, endpoint: str, payload: dict | None = None) -> requests.Response: 54 | """ 55 | Make a request to Cumplo's Global API. 56 | 57 | Args: 58 | method (HTTPMethod): HTTP method to use 59 | endpoint (str): Endpoint to call 60 | payload (dict): Payload to send 61 | 62 | Returns: 63 | requests.Response: Response from the API 64 | 65 | """ 66 | return requests.request(method=method, url=f"{cls.url}{endpoint}", json=payload) 67 | 68 | @classmethod 69 | @retry(requests.exceptions.JSONDecodeError, tries=5, delay=1) 70 | def get_funding_request(cls, id_funding_request: int) -> dict: 71 | """ 72 | Query the Cumplo's Global API for the given funding request information. 73 | 74 | Args: 75 | id_funding_request (int): The ID of the funding request 76 | 77 | Returns: 78 | dict: The funding request information 79 | 80 | """ 81 | logger.debug(f"Getting funding request {id_funding_request} from Cumplo's Global API") 82 | endpoint = CUMPLO_GLOBAL_API_DETAILS.format(id_funding_request=id_funding_request) 83 | response = cls._request(HTTPMethod.GET, endpoint) 84 | return response.json()["data"]["attributes"] 85 | 86 | @classmethod 87 | @retry(requests.exceptions.JSONDecodeError, tries=5, delay=1) 88 | def simulate_funding_request(cls, funding_request: GlobalFundingRequest, due_date: str) -> dict: 89 | """ 90 | Request the Cumplo's Global API to simulate the funding request. 91 | 92 | Args: 93 | funding_request (GlobalFundingRequest): The funding request information 94 | due_date (str): The due date of the funding request 95 | Returns: 96 | dict: The funding request information 97 | 98 | """ 99 | logger.debug(f"Simulating funding request {funding_request.id} from Cumplo's Global API") 100 | # NOTE: The payload is hardcoded because the simulation only depends on the funding request ID and the amount 101 | payload = { 102 | "data": { 103 | "cuotas": 1, 104 | "id_operacion": funding_request.id, 105 | "monto_simulacion": SIMULATION_AMOUNT, 106 | "plazo": funding_request.duration.value, 107 | "tasa_anual": float(funding_request.irr), 108 | "fecha_vencimiento": due_date, 109 | }, 110 | } 111 | endpoint = CUMPLO_GLOBAL_API_SIMULATION.format( 112 | credit_type=funding_request.credit_type.value, 113 | currency=funding_request.currency.value, 114 | ) 115 | response = cls._request(HTTPMethod.POST, endpoint, payload=payload) 116 | return response.json()["data"]["attributes"] 117 | 118 | @classmethod 119 | @retry((KeyError, requests.exceptions.JSONDecodeError), tries=5, delay=1) 120 | def get_funding_requests(cls, *, ignore_completed: bool = False) -> list[GlobalFundingRequest]: 121 | """ 122 | Query the Cumplo's Global API for the existing funding requests. 123 | 124 | Returns: 125 | list[GlobalFundingRequest]: List of the existing funding requests 126 | 127 | """ 128 | logger.debug("Getting funding requests from Cumplo's Global API") 129 | response = cls._request(HTTPMethod.GET, CUMPLO_GLOBAL_API_FUNDING_REQUESTS) 130 | 131 | data = [x["attributes"] for x in response.json()["data"]] 132 | 133 | funding_requests = [ 134 | GlobalFundingRequest.model_validate({ 135 | **element["operacion"], 136 | "credit_type": element["operacion"]["producto"]["codigo"], 137 | "id_borrower": element["empresa"]["id"], 138 | }) 139 | for element in data 140 | ] 141 | 142 | if ignore_completed: 143 | funding_requests = [x for x in funding_requests if not x.is_completed] 144 | 145 | return funding_requests 146 | -------------------------------------------------------------------------------- /cumplo_spotter/models/filter.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | from logging import getLogger 3 | from typing import final 4 | 5 | from cumplo_common.models import FilterConfiguration, FundingRequest 6 | 7 | from cumplo_spotter.models.cumplo.request_duration import DurationUnit 8 | 9 | logger = getLogger(__name__) 10 | 11 | 12 | class Filter(ABC): 13 | def __init__(self, configuration: FilterConfiguration) -> None: 14 | self.configuration = configuration 15 | 16 | @abstractmethod 17 | def _apply(self, funding_request: FundingRequest) -> bool: ... 18 | 19 | @final 20 | def apply(self, funding_request: FundingRequest) -> bool: 21 | """Apply the filter to the funding request.""" 22 | if not (result := self._apply(funding_request)): 23 | filter_name = f"'{self.configuration.name}' {self.__class__.__name__}" 24 | logger.info(f"Funding request {funding_request.id} filtered out by {filter_name}") 25 | return result 26 | 27 | 28 | class CreditTypeFilter(Filter): 29 | def _apply(self, funding_request: FundingRequest) -> bool: 30 | """Filter out the funding requests that don't have the target credit types.""" 31 | if self.configuration.target_credit_types is None: 32 | return True 33 | 34 | return funding_request.credit_type in self.configuration.target_credit_types 35 | 36 | 37 | class MinimumInvestmentFilter(Filter): 38 | def _apply(self, funding_request: FundingRequest) -> bool: 39 | """Filter out the funding requests that have a available investment lower than the minimum.""" 40 | if self.configuration.minimum_investment_amount is None: 41 | return True 42 | 43 | return funding_request.maximum_investment >= self.configuration.minimum_investment_amount 44 | 45 | 46 | class MinimumAmountFilter(Filter): 47 | def _apply(self, funding_request: FundingRequest) -> bool: 48 | """Filter out the funding requests that have a available investment lower than the minimum.""" 49 | if self.configuration.minimum_amount is None: 50 | return True 51 | 52 | return funding_request.amount >= self.configuration.minimum_amount 53 | 54 | 55 | class MinimumScoreFilter(Filter): 56 | def _apply(self, funding_request: FundingRequest) -> bool: 57 | """Filter out the funding requests that have a score lower than the minimum.""" 58 | if self.configuration.minimum_score is None: 59 | return True 60 | 61 | return funding_request.score >= self.configuration.minimum_score 62 | 63 | 64 | class MinimumMonthlyProfitFilter(Filter): 65 | def _apply(self, funding_request: FundingRequest) -> bool: 66 | """Filter out the funding requests that have a monthly profit lower than the minimum.""" 67 | if self.configuration.minimum_monthly_profit_rate is None: 68 | return True 69 | 70 | return funding_request.monthly_profit_rate >= self.configuration.minimum_monthly_profit_rate 71 | 72 | 73 | class MinimumIRRFilter(Filter): 74 | def _apply(self, funding_request: FundingRequest) -> bool: 75 | """Filter out the funding requests that have an IRR lower than the minimum.""" 76 | if self.configuration.minimum_irr is None: 77 | return True 78 | 79 | return funding_request.irr >= self.configuration.minimum_irr 80 | 81 | 82 | class MinimumDurationFilter(Filter): 83 | def _apply(self, funding_request: FundingRequest) -> bool: 84 | """Filter out the funding requests that have a duration lower than the minimum.""" 85 | if self.configuration.minimum_duration is None: 86 | return True 87 | 88 | duration = ( 89 | funding_request.duration.value 90 | if funding_request.duration.unit == DurationUnit.DAY 91 | else funding_request.duration.value * 30 92 | ) 93 | return duration >= self.configuration.minimum_duration 94 | 95 | 96 | class MaximumDurationFilter(Filter): 97 | def _apply(self, funding_request: FundingRequest) -> bool: 98 | """Filter out the funding requests that have a duration greater than the maximum.""" 99 | if self.configuration.maximum_duration is None: 100 | return True 101 | 102 | duration = ( 103 | funding_request.duration.value 104 | if funding_request.duration.unit == DurationUnit.DAY 105 | else funding_request.duration.value * 30 106 | ) 107 | return duration <= self.configuration.maximum_duration 108 | 109 | 110 | class DicomFilter(Filter): 111 | def _apply(self, funding_request: FundingRequest) -> bool: 112 | """Filter out the funding requests whose debtor has DICOM.""" 113 | if self.configuration.ignore_dicom: 114 | return True 115 | 116 | dicoms = [debtor.dicom for debtor in funding_request.debtors] or [funding_request.borrower.dicom] 117 | return not any(dicoms) 118 | 119 | 120 | class PortfolioFilter(Filter): 121 | def _apply(self, funding_request: FundingRequest) -> bool: 122 | if not self.configuration.portfolio: 123 | return True 124 | 125 | portfolios = [debtor.portfolio for debtor in funding_request.debtors] + [funding_request.borrower.portfolio] 126 | 127 | for portfolio in portfolios: 128 | for filter_ in self.configuration.portfolio: 129 | value = portfolio.get( 130 | unit=filter_.unit, 131 | category=filter_.category, 132 | percentage_unit=filter_.percentage_unit, 133 | percentage_base=filter_.percentage_base, 134 | ) 135 | 136 | if filter_.minimum is not None and value < filter_.minimum: 137 | logger.info(f"Funding request {funding_request.id} filtered out by {filter_}") 138 | return False 139 | 140 | if filter_.maximum is not None and value > filter_.maximum: 141 | logger.info(f"Funding request {funding_request.id} filtered out by {filter_}") 142 | return False 143 | 144 | return True 145 | -------------------------------------------------------------------------------- /cumplo_spotter/models/cumplo/funding_request.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | from enum import StrEnum 3 | from functools import cached_property 4 | from typing import Any 5 | 6 | from cumplo_common.models import CreditType, Currency, FundingRequest 7 | from cumplo_common.utils.text import clean_text 8 | from pydantic import BaseModel, Field, field_validator, model_validator 9 | 10 | from cumplo_spotter.models.cumplo.borrower import Borrower 11 | from cumplo_spotter.models.cumplo.debtor import Debtor 12 | from cumplo_spotter.models.cumplo.request_duration import CumploFundingRequestDuration 13 | from cumplo_spotter.models.cumplo.simulation import CumploFundingRequestSimulation 14 | from cumplo_spotter.utils.constants import DicomMarker 15 | 16 | 17 | class CumploCreditType(StrEnum): 18 | ONE_SHOT = "ONE_SHOT" 19 | ANTICIPO_RIEGO = "ANTICIPO_RIEGO" 20 | FACTURA_FUTURA = "FACTURA_FUTURA" 21 | ANTICIPO_SERVIU = "ANTICIPO_SERVIU" 22 | CAPITAL_TRABAJO = "CAPITAL_TRABAJO" 23 | ANTICIPO_FACTURA = "ANTICIPO_FACTURA" 24 | EXTENSION_PLAZO_PAGO = "EXTENSION_PLAZO_PAGO" 25 | ANTICIPO_FACTURA_USD = "ANTICIPO_FACTURA_USD" 26 | CREDITO_CONTRATO = "CREDITO_CONTRATO" 27 | CREDITO_ORDEN_COMPRA = "CREDITO_ORDEN_COMPRA" 28 | SHORT_TERM_CAPITAL = "short_term_capital" 29 | IRRIGATION = "irrigation" 30 | BALLOON = "balloon" 31 | INVOICE = "invoice" 32 | BULLET = "bullet" 33 | SIMPLE = "simple" 34 | 35 | 36 | CREDIT_TYPE_TRANSLATIONS = { 37 | CumploCreditType.SIMPLE: CreditType.WORKING_CAPITAL, 38 | CumploCreditType.BULLET: CreditType.WORKING_CAPITAL, 39 | CumploCreditType.BALLOON: CreditType.WORKING_CAPITAL, 40 | CumploCreditType.ONE_SHOT: CreditType.WORKING_CAPITAL, 41 | CumploCreditType.CAPITAL_TRABAJO: CreditType.WORKING_CAPITAL, 42 | CumploCreditType.CREDITO_CONTRATO: CreditType.WORKING_CAPITAL, 43 | CumploCreditType.SHORT_TERM_CAPITAL: CreditType.WORKING_CAPITAL, 44 | CumploCreditType.CREDITO_ORDEN_COMPRA: CreditType.WORKING_CAPITAL, 45 | # TODO: Check if EXTENSION_PLAZO_PAGO is actually working capital 46 | CumploCreditType.EXTENSION_PLAZO_PAGO: CreditType.WORKING_CAPITAL, 47 | CumploCreditType.INVOICE: CreditType.FACTORING, 48 | CumploCreditType.FACTURA_FUTURA: CreditType.FACTORING, 49 | CumploCreditType.ANTICIPO_FACTURA: CreditType.FACTORING, 50 | CumploCreditType.ANTICIPO_FACTURA_USD: CreditType.FACTORING, 51 | CumploCreditType.ANTICIPO_RIEGO: CreditType.TREASURY_SUBSIDY, 52 | CumploCreditType.IRRIGATION: CreditType.TREASURY_SUBSIDY, 53 | CumploCreditType.ANTICIPO_SERVIU: CreditType.HUD_SUBSIDY, 54 | } 55 | 56 | 57 | class CumploFundingRequest(BaseModel): 58 | id: int = Field(..., alias="id_operacion") 59 | score: Decimal = Field(...) 60 | irr: Decimal = Field(..., alias="tir") 61 | currency: Currency = Field(..., alias="moneda") 62 | amount: int = Field(..., alias="monto_financiar") 63 | credit_type: CreditType = Field(..., alias="codigo_producto") 64 | due_date: str = Field(..., alias="fecha_vencimiento") 65 | raised_amount: int = Field(..., alias="total_inversion") 66 | maximum_investment: int = Field(..., alias="max_inversion") 67 | investors: int = Field(..., alias="cantidad_inversionistas") 68 | raised_percentage: Decimal = Field(..., alias="porcentaje_inversion") 69 | 70 | supporting_documents: list[str] = Field(default_factory=list, alias="tipo_respaldo") 71 | duration: CumploFundingRequestDuration = Field(..., alias="plazo") 72 | simulation: CumploFundingRequestSimulation = Field(...) 73 | debtors: list[Debtor] = Field(default_factory=list, alias="pagadores") 74 | borrower: Borrower = Field(..., alias="solicitante") 75 | 76 | @field_validator("id", "amount", "raised_amount", "maximum_investment", "investors", mode="before") 77 | @classmethod 78 | def parse_integer_fields(cls, value: Any) -> int | None: 79 | """Ensure integer fields are parsed as integers.""" 80 | return None if value is None else int(value) 81 | 82 | @model_validator(mode="before") 83 | @classmethod 84 | def _preprocess_data(cls, data: dict) -> dict: 85 | """Format the data before validating.""" 86 | cls._set_dicom_status(data) 87 | return data 88 | 89 | @classmethod 90 | def _set_dicom_status(cls, data: dict) -> None: 91 | """Set the DICOM status of the borrower and debtors.""" 92 | debtor_dicom, borrower_dicom = cls._identify_dicom_status(data) 93 | 94 | data["solicitante"]["dicom"] = borrower_dicom 95 | for debtor in data["pagadores"]: 96 | debtor["dicom"] = debtor_dicom 97 | 98 | @staticmethod 99 | def _identify_dicom_status(data: dict) -> tuple[bool | None, bool | None]: 100 | """Identify the DICOM status of the borrower and debtors.""" 101 | debtor_description = clean_text(data["vitrina_descripcion_empresa_deudora"]) 102 | borrower_description = clean_text(data["vitrina_descripcion_empresa_solicitante"]) 103 | description = f"{borrower_description} {debtor_description}" 104 | 105 | debtor_dicom, borrower_dicom = None, None 106 | 107 | if DicomMarker.BOTH_TRUE in description: 108 | return True, True 109 | 110 | if DicomMarker.BOTH_FALSE in description: 111 | return False, False 112 | 113 | if DicomMarker.DEBTOR_TRUE in description: 114 | debtor_dicom = True 115 | 116 | if any(marker in description for marker in DicomMarker.BORROWER_TRUE): 117 | borrower_dicom = True 118 | 119 | if DicomMarker.BORROWER_FALSE in description: 120 | borrower_dicom = False 121 | 122 | if borrower_dicom is None and any(marker in description for marker in DicomMarker.SINGLE_FALSE): 123 | borrower_dicom = False 124 | 125 | elif borrower_dicom is None and any(marker in description for marker in DicomMarker.SINGLE_TRUE): 126 | borrower_dicom = True 127 | 128 | return debtor_dicom, borrower_dicom 129 | 130 | @field_validator("supporting_documents", mode="before") 131 | @classmethod 132 | def _format_supporting_documents(cls, value: Any) -> list[str]: 133 | """Format the supporting documents names.""" 134 | return [clean_text(document) for document in value] 135 | 136 | @field_validator("raised_percentage", mode="before") 137 | @classmethod 138 | def raised_percentage_validator(cls, value: Any) -> Decimal: 139 | """Validate that the raised percentage is a valid decimal number.""" 140 | return round(Decimal(int(value) / 100), 2) 141 | 142 | @field_validator("credit_type", mode="before") 143 | @classmethod 144 | def credit_type_validator(cls, value: Any) -> CreditType: 145 | """Validate that the credit_type has a valid value.""" 146 | return CREDIT_TYPE_TRANSLATIONS[value] 147 | 148 | @cached_property 149 | def is_completed(self) -> bool: 150 | """Check if the funding request is fully funded.""" 151 | return self.raised_percentage == Decimal(1) 152 | 153 | def export(self) -> FundingRequest: 154 | """Export the CumploFundingRequest to a FundingRequest.""" 155 | return FundingRequest.model_validate(self.model_dump(exclude_none=True, exclude_unset=True)) 156 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Attribution-NonCommercial 4.0 International 3 | 4 | ======================================================================= 5 | 6 | Creative Commons Corporation ("Creative Commons") is not a law firm and 7 | does not provide legal services or legal advice. Distribution of 8 | Creative Commons public licenses does not create a lawyer-client or 9 | other relationship. Creative Commons makes its licenses and related 10 | information available on an "as-is" basis. Creative Commons gives no 11 | warranties regarding its licenses, any material licensed under their 12 | terms and conditions, or any related information. Creative Commons 13 | disclaims all liability for damages resulting from their use to the 14 | fullest extent possible. 15 | 16 | Using Creative Commons Public Licenses 17 | 18 | Creative Commons public licenses provide a standard set of terms and 19 | conditions that creators and other rights holders may use to share 20 | original works of authorship and other material subject to copyright 21 | and certain other rights specified in the public license below. The 22 | following considerations are for informational purposes only, are not 23 | exhaustive, and do not form part of our licenses. 24 | 25 | Considerations for licensors: Our public licenses are 26 | intended for use by those authorized to give the public 27 | permission to use material in ways otherwise restricted by 28 | copyright and certain other rights. Our licenses are 29 | irrevocable. Licensors should read and understand the terms 30 | and conditions of the license they choose before applying it. 31 | Licensors should also secure all rights necessary before 32 | applying our licenses so that the public can reuse the 33 | material as expected. Licensors should clearly mark any 34 | material not subject to the license. This includes other CC- 35 | licensed material, or material used under an exception or 36 | limitation to copyright. More considerations for licensors: 37 | wiki.creativecommons.org/Considerations_for_licensors 38 | 39 | Considerations for the public: By using one of our public 40 | licenses, a licensor grants the public permission to use the 41 | licensed material under specified terms and conditions. If 42 | the licensor's permission is not necessary for any reason--for 43 | example, because of any applicable exception or limitation to 44 | copyright--then that use is not regulated by the license. Our 45 | licenses grant only permissions under copyright and certain 46 | other rights that a licensor has authority to grant. Use of 47 | the licensed material may still be restricted for other 48 | reasons, including because others have copyright or other 49 | rights in the material. A licensor may make special requests, 50 | such as asking that all changes be marked or described. 51 | Although not required by our licenses, you are encouraged to 52 | respect those requests where reasonable. More_considerations 53 | for the public: 54 | wiki.creativecommons.org/Considerations_for_licensees 55 | 56 | ======================================================================= 57 | 58 | Creative Commons Attribution-NonCommercial 4.0 International Public 59 | License 60 | 61 | By exercising the Licensed Rights (defined below), You accept and agree 62 | to be bound by the terms and conditions of this Creative Commons 63 | Attribution-NonCommercial 4.0 International Public License ("Public 64 | License"). To the extent this Public License may be interpreted as a 65 | contract, You are granted the Licensed Rights in consideration of Your 66 | acceptance of these terms and conditions, and the Licensor grants You 67 | such rights in consideration of benefits the Licensor receives from 68 | making the Licensed Material available under these terms and 69 | conditions. 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. Copyright and Similar Rights means copyright and/or similar rights 88 | closely related to copyright including, without limitation, 89 | performance, broadcast, sound recording, and Sui Generis Database 90 | Rights, without regard to how the rights are labeled or 91 | categorized. For purposes of this Public License, the rights 92 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 93 | Rights. 94 | d. Effective Technological Measures means those measures that, in the 95 | absence of proper authority, may not be circumvented under laws 96 | fulfilling obligations under Article 11 of the WIPO Copyright 97 | Treaty adopted on December 20, 1996, and/or similar international 98 | agreements. 99 | 100 | e. Exceptions and Limitations means fair use, fair dealing, and/or 101 | any other exception or limitation to Copyright and Similar Rights 102 | that applies to Your use of the Licensed Material. 103 | 104 | f. Licensed Material means the artistic or literary work, database, 105 | or other material to which the Licensor applied this Public 106 | License. 107 | 108 | g. Licensed Rights means the rights granted to You subject to the 109 | terms and conditions of this Public License, which are limited to 110 | all Copyright and Similar Rights that apply to Your use of the 111 | Licensed Material and that the Licensor has authority to license. 112 | 113 | h. Licensor means the individual(s) or entity(ies) granting rights 114 | under this Public License. 115 | 116 | i. NonCommercial means not primarily intended for or directed towards 117 | commercial advantage or monetary compensation. For purposes of 118 | this Public License, the exchange of the Licensed Material for 119 | other material subject to Copyright and Similar Rights by digital 120 | file-sharing or similar means is NonCommercial provided there is 121 | no payment of monetary compensation in connection with the 122 | exchange. 123 | 124 | j. Share means to provide material to the public by any means or 125 | process that requires permission under the Licensed Rights, such 126 | as reproduction, public display, public performance, distribution, 127 | dissemination, communication, or importation, and to make material 128 | available to the public including in ways that members of the 129 | public may access the material from a place and at a time 130 | individually chosen by them. 131 | 132 | k. Sui Generis Database Rights means rights other than copyright 133 | resulting from Directive 96/9/EC of the European Parliament and of 134 | the Council of 11 March 1996 on the legal protection of databases, 135 | as amended and/or succeeded, as well as other essentially 136 | equivalent rights anywhere in the world. 137 | 138 | l. You means the individual or entity exercising the Licensed Rights 139 | under this Public License. Your has a corresponding meaning. 140 | 141 | Section 2 -- Scope. 142 | 143 | a. License grant. 144 | 145 | 1. Subject to the terms and conditions of this Public License, 146 | the Licensor hereby grants You a worldwide, royalty-free, 147 | non-sublicensable, non-exclusive, irrevocable license to 148 | exercise the Licensed Rights in the Licensed Material to: 149 | 150 | a. reproduce and Share the Licensed Material, in whole or 151 | in part, for NonCommercial purposes only; and 152 | 153 | b. produce, reproduce, and Share Adapted Material for 154 | NonCommercial purposes only. 155 | 156 | 2. Exceptions and Limitations. For the avoidance of doubt, where 157 | Exceptions and Limitations apply to Your use, this Public 158 | License does not apply, and You do not need to comply with 159 | its terms and conditions. 160 | 161 | 3. Term. The term of this Public License is specified in Section 162 | 6(a). 163 | 164 | 4. Media and formats; technical modifications allowed. The 165 | Licensor authorizes You to exercise the Licensed Rights in 166 | all media and formats whether now known or hereafter created, 167 | and to make technical modifications necessary to do so. The 168 | Licensor waives and/or agrees not to assert any right or 169 | authority to forbid You from making technical modifications 170 | necessary to exercise the Licensed Rights, including 171 | technical modifications necessary to circumvent Effective 172 | Technological Measures. For purposes of this Public License, 173 | simply making modifications authorized by this Section 2(a) 174 | (4) never produces Adapted Material. 175 | 176 | 5. Downstream recipients. 177 | 178 | a. Offer from the Licensor -- Licensed Material. Every 179 | recipient of the Licensed Material automatically 180 | receives an offer from the Licensor to exercise the 181 | Licensed Rights under the terms and conditions of this 182 | Public License. 183 | 184 | b. No downstream restrictions. You may not offer or impose 185 | any additional or different terms or conditions on, or 186 | apply any Effective Technological Measures to, the 187 | Licensed Material if doing so restricts exercise of the 188 | Licensed Rights by any recipient of the Licensed 189 | Material. 190 | 191 | 6. No endorsement. Nothing in this Public License constitutes or 192 | may be construed as permission to assert or imply that You 193 | are, or that Your use of the Licensed Material is, connected 194 | with, or sponsored, endorsed, or granted official status by, 195 | the Licensor or others designated to receive attribution as 196 | provided in Section 3(a)(1)(A)(i). 197 | 198 | b. Other rights. 199 | 200 | 1. Moral rights, such as the right of integrity, are not 201 | licensed under this Public License, nor are publicity, 202 | privacy, and/or other similar personality rights; however, to 203 | the extent possible, the Licensor waives and/or agrees not to 204 | assert any such rights held by the Licensor to the limited 205 | extent necessary to allow You to exercise the Licensed 206 | Rights, but not otherwise. 207 | 208 | 2. Patent and trademark rights are not licensed under this 209 | Public License. 210 | 211 | 3. To the extent possible, the Licensor waives any right to 212 | collect royalties from You for the exercise of the Licensed 213 | Rights, whether directly or through a collecting society 214 | under any voluntary or waivable statutory or compulsory 215 | licensing scheme. In all other cases the Licensor expressly 216 | reserves any right to collect such royalties, including when 217 | the Licensed Material is used other than for NonCommercial 218 | purposes. 219 | 220 | Section 3 -- License Conditions. 221 | 222 | Your exercise of the Licensed Rights is expressly made subject to the 223 | following conditions. 224 | 225 | a. Attribution. 226 | 227 | 1. If You Share the Licensed Material (including in modified 228 | form), You must: 229 | 230 | a. retain the following if it is supplied by the Licensor 231 | with the Licensed Material: 232 | 233 | i. identification of the creator(s) of the Licensed 234 | Material and any others designated to receive 235 | attribution, in any reasonable manner requested by 236 | the Licensor (including by pseudonym if 237 | designated); 238 | 239 | ii. a copyright notice; 240 | 241 | iii. a notice that refers to this Public License; 242 | 243 | iv. a notice that refers to the disclaimer of 244 | warranties; 245 | 246 | v. a URI or hyperlink to the Licensed Material to the 247 | extent reasonably practicable; 248 | 249 | b. indicate if You modified the Licensed Material and 250 | retain an indication of any previous modifications; and 251 | 252 | c. indicate the Licensed Material is licensed under this 253 | Public License, and include the text of, or the URI or 254 | hyperlink to, this Public License. 255 | 256 | 2. You may satisfy the conditions in Section 3(a)(1) in any 257 | reasonable manner based on the medium, means, and context in 258 | which You Share the Licensed Material. For example, it may be 259 | reasonable to satisfy the conditions by providing a URI or 260 | hyperlink to a resource that includes the required 261 | information. 262 | 263 | 3. If requested by the Licensor, You must remove any of the 264 | information required by Section 3(a)(1)(A) to the extent 265 | reasonably practicable. 266 | 267 | 4. If You Share Adapted Material You produce, the Adapter's 268 | License You apply must not prevent recipients of the Adapted 269 | Material from complying with this Public License. 270 | 271 | Section 4 -- Sui Generis Database Rights. 272 | 273 | Where the Licensed Rights include Sui Generis Database Rights that 274 | apply to Your use of the Licensed Material: 275 | 276 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 277 | to extract, reuse, reproduce, and Share all or a substantial 278 | portion of the contents of the database for NonCommercial purposes 279 | only; 280 | 281 | b. if You include all or a substantial portion of the database 282 | contents in a database in which You have Sui Generis Database 283 | Rights, then the database in which You have Sui Generis Database 284 | Rights (but not its individual contents) is Adapted Material; and 285 | 286 | c. You must comply with the conditions in Section 3(a) if You Share 287 | all or a substantial portion of the contents of the database. 288 | 289 | For the avoidance of doubt, this Section 4 supplements and does not 290 | replace Your obligations under this Public License where the Licensed 291 | Rights include other Copyright and Similar Rights. 292 | 293 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 294 | 295 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 296 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 297 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 298 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 299 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 300 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 301 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 302 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 303 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 304 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 305 | 306 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 307 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 308 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 309 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 310 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 311 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 312 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 313 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 314 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 315 | 316 | c. The disclaimer of warranties and limitation of liability provided 317 | above shall be interpreted in a manner that, to the extent 318 | possible, most closely approximates an absolute disclaimer and 319 | waiver of all liability. 320 | 321 | Section 6 -- Term and Termination. 322 | 323 | a. This Public License applies for the term of the Copyright and 324 | Similar Rights licensed here. However, if You fail to comply with 325 | this Public License, then Your rights under this Public License 326 | terminate automatically. 327 | 328 | b. Where Your right to use the Licensed Material has terminated under 329 | Section 6(a), it reinstates: 330 | 331 | 1. automatically as of the date the violation is cured, provided 332 | it is cured within 30 days of Your discovery of the 333 | violation; or 334 | 335 | 2. upon express reinstatement by the Licensor. 336 | 337 | For the avoidance of doubt, this Section 6(b) does not affect any 338 | right the Licensor may have to seek remedies for Your violations 339 | of this Public License. 340 | 341 | c. For the avoidance of doubt, the Licensor may also offer the 342 | Licensed Material under separate terms or conditions or stop 343 | distributing the Licensed Material at any time; however, doing so 344 | will not terminate this Public License. 345 | 346 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 347 | License. 348 | 349 | Section 7 -- Other Terms and Conditions. 350 | 351 | a. The Licensor shall not be bound by any additional or different 352 | terms or conditions communicated by You unless expressly agreed. 353 | 354 | b. Any arrangements, understandings, or agreements regarding the 355 | Licensed Material not stated herein are separate from and 356 | independent of the terms and conditions of this Public License. 357 | 358 | Section 8 -- Interpretation. 359 | 360 | a. For the avoidance of doubt, this Public License does not, and 361 | shall not be interpreted to, reduce, limit, restrict, or impose 362 | conditions on any use of the Licensed Material that could lawfully 363 | be made without permission under this Public License. 364 | 365 | b. To the extent possible, if any provision of this Public License is 366 | deemed unenforceable, it shall be automatically reformed to the 367 | minimum extent necessary to make it enforceable. If the provision 368 | cannot be reformed, it shall be severed from this Public License 369 | without affecting the enforceability of the remaining terms and 370 | conditions. 371 | 372 | c. No term or condition of this Public License will be waived and no 373 | failure to comply consented to unless expressly agreed to by the 374 | Licensor. 375 | 376 | d. Nothing in this Public License constitutes or may be interpreted 377 | as a limitation upon, or waiver of, any privileges and immunities 378 | that apply to the Licensor or You, including from the legal 379 | processes of any jurisdiction or authority. 380 | 381 | ======================================================================= 382 | 383 | Creative Commons is not a party to its public 384 | licenses. Notwithstanding, Creative Commons may elect to apply one of 385 | its public licenses to material it publishes and in those instances 386 | will be considered the “Licensor.” The text of the Creative Commons 387 | public licenses is dedicated to the public domain under the CC0 Public 388 | Domain Dedication. Except for the limited purpose of indicating that 389 | material is shared under a Creative Commons public license or as 390 | otherwise permitted by the Creative Commons policies published at 391 | creativecommons.org/policies, Creative Commons does not authorize the 392 | use of the trademark "Creative Commons" or any other trademark or logo 393 | of Creative Commons without its prior written consent including, 394 | without limitation, in connection with any unauthorized modifications 395 | to any of its public licenses or any other arrangements, 396 | understandings, or agreements concerning use of licensed material. For 397 | the avoidance of doubt, this paragraph does not form part of the 398 | public licenses. 399 | 400 | Creative Commons may be contacted at creativecommons.org. --------------------------------------------------------------------------------