Skip to main content

zytools-fs

zytools-fs is a small collection of Python utilities for personal crawling, FTP transfer, task heartbeats, persistent URL de-duplication, and Bilibili video downloads.

The package is intentionally lightweight: every helper can be imported and used directly in scripts without a framework.

Features

  • FTP and SFTP recursive download/upload with retry, progress, skip-by-size, and concurrent transfer support.
  • Bilibili video downloader based on requests.
  • LMDB-backed URL filter for large crawl de-duplication.
  • Article page detector and simple same-domain crawler.
  • Task heartbeat helper for reporting script status to a /tasks endpoint.
  • zytools command for a quick installation check.

Installation

pip install zytools-fs

Python 3.9 or newer is required.

For Bilibili DASH video merging, install ffmpeg and make sure it is available in PATH. If ffmpeg is not found, audio and video streams are kept as separate files.

Quick Check

zytools

Expected output:

zytools installed successfully.

FTP and SFTP Transfer

FTPClient and SFTPClient share the same high-level API for recursive upload/download. Both clients support per-file progress logs, retries, skip-by-size, concurrent directory transfers, and final transfer summaries.

FTP

from zytools.utils import FTPClient

with FTPClient(
    host="127.0.0.1",
    user="user",
    password="password",
    port=21,
    passive=True,
    workers=4,
) as ftp:
    ftp.download("/remote/path", "./downloads")
    ftp.upload("./reports", "/remote/reports")

SFTP

from zytools.utils import SFTPClient

with SFTPClient(
    host="127.0.0.1",
    user="user",
    password="password",
    port=22,
    # key_filename="~/.ssh/id_rsa",
    workers=4,
) as sftp:
    sftp.download("/remote/path", "./downloads")
    sftp.upload("./reports", "/remote/reports")

Transfer Behavior

  • Direct file paths transfer one file; directory paths are traversed recursively.
  • Existing target files with the same size are skipped and counted as success.
  • Uploads and downloads print per-file progress when show_progress=True.
  • Each download() or upload() call ends with total, success, and error.
  • workers>1 enables concurrent file transfers for directories; each worker uses its own connection.

Example summary:

upload summary: total=10 success=9 error=1
download summary: total=10 success=10 error=0

Useful options:

  • port: FTP defaults to 21; SFTP defaults to 22.
  • encoding: FTP filename encoding, default utf-8.
  • passive: FTP passive mode, default True (FTP only).
  • key_filename: SSH private key path for SFTP authentication.
  • download_retries and upload_retries: retry count.
  • retry_wait_seconds: wait time between retries.
  • workers: concurrent workers for directory downloads and uploads, default 1.
  • show_progress: print transfer progress through loguru.

Bilibili Video Download

from zytools.video import download_bili_video

ok = download_bili_video(
    "https://www.bilibili.com/video/BVxxxx",
    output_dir="./downloads",
    quality="max",
    page="all",
    filename="Bilibili_{BV}_{Date}_{Page}_{PartTitle}",
    cookie={
        "SESSDATA": "your_sessdata",
        "bili_jct": "your_bili_jct",
    },
    proxies={"https": "http://127.0.0.1:7890"},
)

print(ok)

Parameters:

  • quality: "max" for the highest available stream, "min" for the lowest. The actual quality depends on Bilibili account permissions and returned DASH streams.
  • page: "all", a single page such as "1", or a range/list such as "1,3-5".
  • cookie: optional Bilibili cookies for videos that require login.
  • proxies: optional proxies for Bilibili page/API requests; media stream downloads do not use it.
  • force: when False, existing final video files are skipped; when True, they are overwritten.

Filename template fields:

  • {Title}: video title.
  • {BV}: BV id.
  • {Date}: publish date in YYYYMMDD format.
  • {Page}: page number.
  • {Part}: same as page number.
  • {Duration}: duration in seconds.
  • {PartTitle}: page title.

Only download content that you own or are allowed to download.

URL Filter

UrlFilter stores compact MD5 fingerprints in LMDB. Unlike a Bloom filter, it does not intentionally produce false positives.

from zytools.utils import UrlFilter

with UrlFilter(file_path="url_seen.lmdb") as url_filter:
    url = "https://example.com/video?id=1"

    if url_filter.add(url):
        print("new url")
    else:
        print("seen before")

    print(len(url_filter))

Batch import and export:

from zytools.utils import UrlFilter

with UrlFilter("url_seen.lmdb") as url_filter:
    added = url_filter.add_many(
        [
            "https://example.com/a",
            "https://example.com/b",
        ]
    )
    url_filter.to_csv("url_seen.csv")

print(f"added {added} urls")

UrlFilter.to_lmdb("url_seen.csv", file_path="url_seen_copy.lmdb")

Article Detection

Use check_response to request one URL and classify it as an article, other HTML page, binary resource, or fetch error.

from zytools.artice import check_response

result = check_response("https://example.com/news/1.html")

if result["type"] == "article":
    print(result["title"])
    print(result["date"])
    print(result["text"][:300])
else:
    print(result["type"], result.get("reason"))

Return type values:

  • article: article page with extracted title, date, author, and text.
  • other: HTML page that does not look like an article.
  • binary: image, PDF, JavaScript, CSS, video, archive, or other non-HTML file.
  • fetch_error: request failed or returned a bad HTTP status.

Simple URL Crawler

UrlCrawler starts from one URL, follows links breadth-first, and yields article items. It can optionally use UrlFilter to avoid saving the same article URL across runs.

from zytools.artice import UrlCrawler
from zytools.utils import UrlFilter

with UrlFilter("article_urls.lmdb") as url_filter:
    crawler = UrlCrawler(
        start_url="https://example.com/",
        max_saved_urls=20,
        same_domain=True,
        max_depth=5,
        url_fp=url_filter,
    )

    for item in crawler.crawl():
        print(item["title"], item["url"])

    crawler.save_url_filter()

Each yielded item has:

  • title: extracted article title.
  • creat_date: extracted article publish date.
  • content: extracted article text.
  • url: final article URL.
  • get_date: crawl batch date.

Task Heartbeat

Use update_task for a single heartbeat request, or TaskUpdater when a script needs repeated updates with a minimum interval.

from zytools.utils import TaskUpdater, update_task

result = update_task(
    name="daily job",
    machine_id="machine-1",
    script_path="/path/to/script.py",
    server="http://127.0.0.1:8001",
)

print(result)

task = TaskUpdater(
    name="daily job",
    machine_id="machine-1",
    script_path="/path/to/script.py",
    server="http://127.0.0.1:8001",
    min_interval=60,
)

task.update()
task.update(force=True)

The server is expected to accept POST /tasks with a JSON body containing name, machine_id, script_path, enabled, and timeout_seconds.

Development

Build the package locally:

python -m build

Check the distribution metadata:

python -m twine check dist/*

Publish to PyPI:

python -m twine upload dist/*

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zytools_fs-0.0.12.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zytools_fs-0.0.12-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file zytools_fs-0.0.12.tar.gz.

File metadata

  • Download URL: zytools_fs-0.0.12.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for zytools_fs-0.0.12.tar.gz
Algorithm Hash digest
SHA256 62de56f6dda0287c5ccb720432c340ab86d7b6f53d2d4852898741b834507224
MD5 d7a1f7143fac41ff4f2398f8c0937987
BLAKE2b-256 35b75c6a474c57dea68882b669e3ad379fa1651b82979108fc164de1a0210c75

See more details on using hashes here.

File details

Details for the file zytools_fs-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: zytools_fs-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for zytools_fs-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 28ac2df1c84eff9f7382b8c1e3845961cc6b2ff3189dbb987b7885d4ce09f1fe
MD5 b2d829612a90500c8d09cc9679c1f642
BLAKE2b-256 e484247aac24745eae4ecd6122abe78521023d1cf53d794d959b1f6e6c456bc0

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.43

2 files

0.0.42

1 file

0.0.41

1 file

0.0.40

1 file

0.0.39

1 file

0.0.38

1 file

0.0.37

1 file

0.0.36

2 files

0.0.35

2 files

0.0.34

2 files

0.0.33

2 files

0.0.32

2 files

0.0.31

2 files

0.0.30

2 files

0.0.29

2 files

0.0.28

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.17

2 files

0.0.16

2 files

0.0.14

2 files

0.0.13

2 files

This release

0.0.12 This release

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 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