Skip to main content

Chii

A minimal marketplace bot maker.

Installation

pip install chii

Usage

Import and initialize the Chii object:

from chii.main import Chii

chii = Chii("<bot_token>")

Add decorators to the following callback functions:

This function takes in a query and must return an iterable of search results from the query (e.g. a List of result Dicts).

@chii.query
def make_query(query: str):

This function takes in a result (an element of the iterable returned by make_query) and returns a string containing the desired formatted message to be sent out to the user.

@chii.message
def get_message(result) -> str:

This function takes in a result and must return a uniquely identifiable attribute of the result to use as the 'primary key'.

@chii.key
def get_key(result):

This function takes in a result and must return the image url of the result.

@chii.image
def get_image(result) -> str:

Start the bot:

if __name__ == "__main__":
    chii.start()

Bot Usage

Add a query

/add <query>

Remove a query

/rm <query>

List queries

/ls

Fetch queries

/fetch

Example

Below is a complete example made for the Yahoo! Auctions marketplace.

import re
import requests
import urllib
from bs4 import BeautifulSoup
from datetime import datetime

from chii.main import Chii

YAHUOKU_SEARCH_TEMPLATE = r"https://auctions.yahoo.co.jp/search/search?p={query}&b={start}&n={count}&s1=new&o1=d"

POST_TIMESTAMP_REGEX = r"^.*i-img\d+x\d+-(\d{10}).*$"
AUCTION_TIMESTAMP_REGEX = r"^.*etm=(\d{10}),stm=(\d{10}).*$"

KEY_TITLE = "title"
KEY_URL = "url"
KEY_IMAGE = "img"
KEY_POST_TIMESTAMP = "post_ts"
KEY_END_TIMESTAMP = "end_ts"
KEY_START_TIMESTAMP = "start_ts"
KEY_BUYNOW_PRICE = "buynow_price"
KEY_CURRENT_PRICE = "curr_price"
KEY_START_PRICE = "start_price"
KEY_ITEM_ID = "item_id"

chii = Chii("<bot_token>")


@chii.image
def get_image(result):
    return result[KEY_IMAGE]


@chii.key
def get_key(result):
    return result[KEY_ITEM_ID]


@chii.message
def get_message(result):
    return f"{result[KEY_URL]}\n`{result[KEY_TITLE]}`\n_{datetime.fromtimestamp(result[KEY_POST_TIMESTAMP]).strftime('%d/%m/%Y %I:%M:%S %p')}_\n*{result[KEY_START_PRICE]}円*"


@chii.query
def make_query(query: str):
    url = YAHUOKU_SEARCH_TEMPLATE.format(query=urllib.parse.quote_plus(query), start=1, count=100)
    r = requests.get(url)

    return parse_raw_results(r.text)


def parse_raw_results(raw: str):
    results = []
    soup = BeautifulSoup(raw, "lxml")

    product_details = soup.find_all("div", class_="Product__detail")
    for product_detail in product_details:
        product_bonuses = product_detail.find_all("div", class_="Product__bonus")
        product_titlelinks = product_detail.find_all("a", class_="Product__titleLink")

        if not product_bonuses or not product_titlelinks:
            continue

        product_bonus = product_bonuses[0]
        product_titlelink = product_titlelinks[0]

        auction_title = product_titlelink["data-auction-title"]
        auction_img = product_titlelink["data-auction-img"]
        href = product_titlelink["href"]
        cl_params = product_titlelink["data-cl-params"]

        match = re.match(POST_TIMESTAMP_REGEX, auction_img)
        if not match:
            continue

        post_timestamp = int(match.group(1))

        match = re.match(AUCTION_TIMESTAMP_REGEX, cl_params)
        if not match:
            continue

        end_timestamp = int(match.group(1))
        start_timestamp = int(match.group(2))

        auction_id = product_bonus["data-auction-id"]
        auction_buynowprice = product_bonus["data-auction-buynowprice"]
        auction_price = product_bonus["data-auction-price"]
        auction_startprice = product_bonus["data-auction-startprice"]

        result = {
            KEY_TITLE: auction_title,
            KEY_IMAGE: auction_img,
            KEY_URL: href,
            KEY_POST_TIMESTAMP: post_timestamp,
            KEY_END_TIMESTAMP: end_timestamp,
            KEY_START_TIMESTAMP: start_timestamp,
            KEY_ITEM_ID: auction_id,
            KEY_BUYNOW_PRICE: auction_buynowprice,
            KEY_CURRENT_PRICE: auction_price,
            KEY_START_PRICE: auction_startprice,
        }

        results.append(result)

    return results


if __name__ == "__main__":
    chii.start()

Metadata

Release files for Chii 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Chii 1.1.4
File Size Uploaded
Chii-1.1.4.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Chii 1.1.4
File Interpreter ABI Platform
Chii-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / Chii-1.1.4.tar.gz

Download URL Chii-1.1.4.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
90aef0fd7e666e8791df068a363b5aa7c4dc970f3281bdc02af5b10aedb2ea30
BLAKE2b-256 checksum
How to use checksums
44f87d6b58052650bc47217759aa626378b458527eef4bbf15d6615a500e61a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / Chii-1.1.4-py3-none-any.whl

Download URL Chii-1.1.4-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cc5b6f2c697c3691ecc20e3de05944b7cab02203cc24bd06ec540c931ec78b4
BLAKE2b-256 checksum
How to use checksums
8607a11817e75b3992fa13a59be7f41a7e4ac0f41e78d2260241f4a0983958c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page