Skip to content

Filenames

Every row gets a filename, relative to the save folder. The same row must get the same name every run: that's what makes resuming work.

Templates

A template is a Python format string. The default is {hash}{ext}.

Key Value Example
{hash} 16 hex characters of a BLAKE2b hash of the URL: unique per URL, stable across runs and machines 67ba57b74cee05cc
{index} The row's position in the input, from 0 (also when you use start) 41
{ext} The URL's image extension, or, when the URL has none, the real format detected from the bytes .jpg, .webp
{stem} The URL's filename without its extension (image if there is none) IMG_2041
{host} The URL's host name cdn.example.com
{column} Any column of your table, for example {label} or {entity_name} cats

A / creates subfolders. Standard format specs work, so {index:06d} pads and {hash:.2} takes the first two characters.

names="{index}_{hash}{ext}"            # 41_67ba57b74cee05cc.jpg
names="{label}/{index}{ext}"           # cats/41.jpg
names="{host}/{stem}{ext}"             # cdn.example.com/IMG_2041.jpg
names="{hash:.2}/{hash}{ext}"          # 67/67ba57b74cee05cc.jpg  (spreads files over 256 folders)

Preview before you download. machyon names prints the mapping without fetching anything:

Preview filenames
$ machyon names urls.csv --names {label}/{index}_{hash}{ext} --limit 5
url,filename
http://cdn.example.test/img/0.jpg,cats/0_d1e3c34fa02486a3.jpg
http://cdn.example.test/img/1.jpg,dogs/1_510873ebf99d1821.jpg
http://cdn.example.test/img/2.jpg,birds/2_a03a7f5097a3ea2a.jpg
http://cdn.example.test/img/3.jpg,cats/3_7a14df47744ec9b3.jpg
http://cdn.example.test/img/4.jpg,dogs/4_cea8ca3f3ee32a42.jpg

Recorded from Machyon 1.1.0 against the local test CDN (tools/record_casts.py).

A key that is neither built in nor a column stops the run before anything is downloaded, with a list of the keys you can use. A column that is blank in one row records that row as bad_name and carries on.

Automatic extensions

{ext} is resolved per row:

  • URL ends in an image extension (.jpg, .jpeg, .png, .gif, .webp, .avif, .heic, .jxl, .tif, .tiff, .bmp, .ico, .svg, .jfif, .apng): that extension is used.
  • Anything else (/photo?id=42, /img/42, .php): the extension comes from the downloaded bytes, so a WebP becomes .webp.

On resume, a name with an automatic extension matches whichever image extension is on disk.

Callables

For logic a template can't express, pass a function (url, index, row) -> name. row is the dict of columns, or None for plain URL lists.

naming_callable.py
import machyon


def name(url: str, index: int, row: dict | None) -> str:
    """Any logic you like. Return a path relative to the save folder; '/' makes subfolders."""
    label = (row or {}).get("label", "unlabelled")
    return f"{label}/{index:06d}.{{ext}}"    # "{ext}" (in braces) = the real format, from the bytes


report = machyon.download("products.csv", "images/", names=name)
print(report)

Return a literal {ext} (written {{ext}} inside an f-string) at the end of the name to get an automatic extension.

Explicit names

Give Machyon the names directly, either as pairs or as a second list the same length as the URLs:

machyon.download([("https://…/a.jpg", "cover.jpg"), ("https://…/b.png", "back.png")], "images/")
machyon.download(urls, "images/", names_list)     # len(names_list) must equal len(urls)

Safety rules

Before anything is written:

  • Characters. Names built from a template are cleaned part by part: characters that are invalid on Windows, macOS or Linux become _, trailing dots and spaces are removed, reserved Windows names (CON, NUL, …) get a leading _, and over-long parts are shortened, keeping the extension. A .. part becomes _, so a template can never leave the save folder. On Windows, names from every source are cleaned the same way.
  • Containment. A name you supply directly (pairs, a list, name_col or a callable) that would land outside the save folder, such as ../x.jpg or an absolute path, is refused and recorded as unsafe_name.
  • Conflicts. If two different URLs map to the same file, the first row keeps the name and the other is recorded as duplicate_name. Nothing is overwritten. On Windows, names that differ only in case count as the same file.
  • Duplicates. The same URL on several rows with different names is downloaded once and written to every name. The same URL and name repeated is simply counted as skipped.

Use {hash} for uniqueness

Names built only from columns ({label}{ext}) collide easily. Adding {hash} or {index} guarantees one file per URL or per row.