foley.sources.music_library.acquisition#
Acquire a music library’s titles: verified files, one provenance row each, a resumable table.
acquire() turns titles into download URLs (the library’s rule), fetches each
from the first of its mirrors whose bytes verify accepts, writes the file to
out and returns one AcquiredTrack per title. Every step is a seam
(keyword arguments with working defaults):
fetch(url -> bytes) ortransport(a foleyTransport): by default the fetch goes throughmake_fetch()(the resilience layer perhost: pace,
429/Retry-After, breaker); afetchof the caller’s own is paced per host too (pace_per_host(), from the config’srate).clock/sleeptime both;verify(bytes -> seconds):probe_verify();out: any{file name: bytes}store, a folder by default;intended_use: theIntendedUseeach title’s licence must allow (defaultfoley.licensing.DEFAULT_INTENDED_USE: commercial publishing).
A row’s licence is a LicenseRecord
(track_license()), put through foley’s
fail-closed gate (foley.licensing.refusal_reason()) before anything is
fetched: a title it refuses is a refused row, with the reason, and is not
downloaded. url is where the bytes came from (the mirror that served them),
page_url the library’s own address. A title that fails is a failed row with
its reasons; the batch goes on. A file whose digest a previous run recorded
(kept, from kept_digests() or kept_sources()) is not fetched again,
so a rerun resumes. A paid library fetches nothing: its rows are wishlist rows.
AcquiredTrack.row() is a flat, JSON-safe dict (a caller such as an bridges
it into its own asset rows without foley knowing it); tracks_table() is those
rows as CSV, and attribution_text() the blocks to paste under a video. The way
back is read_tracks_table() (the CSV’s rows, as written) and
AcquiredTrack.from_row() (a row as the track it records, its licence rebuilt
from the library’s registry); held_rows() keeps those whose file is held.
Module Attributes
the licence gate refused the title for the intended use (nothing fetched); |
|
the licence gate refused the title for the intended use (nothing fetched); |
|
the licence gate refused the title for the intended use (nothing fetched); |
|
the licence gate refused the title for the intended use (nothing fetched); |
|
the licence gate refused the title for the intended use (nothing fetched); |
|
Every status a row may have. |
|
The statuses whose file is in |
|
The tracks table's columns, in order (a row's other metadata, a catalogue's |
|
The metadata keys a titles file may set that are columns (the rest the library states). |
|
what the library states, which |
|
How a list is joined in one cell of the tracks table ( |
|
The byte-order mark an editor (Excel) writes before a UTF-8 CSV's first column. |
|
Decimal places a duration is written with. |
|
a file's recorded sha256, or |
Functions
|
Download |
|
The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once. |
|
|
|
|
|
The acquired or kept rows (with a file and a digest) of a tracks table, as row dicts: |
|
|
|
|
|
The rows of a tracks table ( |
|
The tracks table as CSV text: |
Classes
|
One row of the tracks table: a title, what became of it, and its provenance. |
Exceptions
A tracks-table row named columns that set nothing, and |
- foley.sources.music_library.acquisition.ACQUIRED = 'acquired'#
the licence gate refused the title for the intended use (nothing fetched);
wishlist: a paid library’s title.- Type:
The row statuses.
refused
- class foley.sources.music_library.acquisition.AcquiredTrack(title, library, status, license, file=None, sha256=None, duration_s=None, url=None, mirror=None, page_url=None, credited_by=(), metadata=dict(), error=None)[source]#
Bases:
SerializableMixinOne row of the tracks table: a title, what became of it, and its provenance.
urlis the URL that served the bytes (a mirror’s),mirrorthat mirror’s name,page_urlthe library’s own address for the title;credited_bythe videos that credit it andmetadataa titles file’s other fields (feel,genre, …).- classmethod from_row(row, *, libraries=None)[source]#
The track a tracks-table
rowrecords: the inverse ofrow().rowis arow()dict (or one read back from JSON), or a line of the table (read_tracks_table(): every cell text, an empty one nothing). Itslibraryis looked up inlibraries(defaultMUSIC_LIBRARIES) and its licence rebuilt from it (track_license()), never read from the row. The row’s licence cells (LICENSE_COLUMNS) must still say what the library states now: a cell someone edited, or a library re-registered under other terms since the table was written, is refused rather than silently widened or narrowed.A column outside
TRACK_COLUMNSis metadata, as the table wrote it (a list or mapping a titles file gave comes back as its cell’s text). A column a titles file may not set (a downstream provenance field such ascost_usdorcacheable, a licence flag) is dropped with aDroppedColumnsWarning: it never sets anything.- Raises:
MusicLibraryError – For a row with a key that is not text, a blank title, no library or status, a status not in
STATUSES, a library that is not registered, aduration_sthat is not a finite number, or licence cells that differ from the library’s (naming them).- Return type:
- foley.sources.music_library.acquisition.BYTE_ORDER_MARK: str = '\ufeff'#
The byte-order mark an editor (Excel) writes before a UTF-8 CSV’s first column.
- foley.sources.music_library.acquisition.DURATION_DECIMALS: int = 3#
Decimal places a duration is written with.
- exception foley.sources.music_library.acquisition.DroppedColumnsWarning[source]#
Bases:
UserWarningA tracks-table row named columns that set nothing, and
AcquiredTrack.from_row()dropped them.
- foley.sources.music_library.acquisition.FAILED = 'failed'#
the licence gate refused the title for the intended use (nothing fetched);
wishlist: a paid library’s title.- Type:
The row statuses.
refused
- foley.sources.music_library.acquisition.HELD: tuple[str, ...] = ('acquired', 'kept')#
The statuses whose file is in
out(what a rerun may keep, what is credited).
- foley.sources.music_library.acquisition.KEPT = 'kept'#
the licence gate refused the title for the intended use (nothing fetched);
wishlist: a paid library’s title.- Type:
The row statuses.
refused
- foley.sources.music_library.acquisition.Kept#
a file’s recorded sha256, or
{"sha256", "url", "mirror"}(kept_sources()), whoseurl/mirrora kept row carries on.- Type:
kept’s values
- foley.sources.music_library.acquisition.LICENSE_COLUMNS: tuple[str, ...] = ('license', 'license_url', 'rights_verified', 'commercial_ok', 'author', 'attribution')#
what the library states, which
AcquiredTrack.from_row()checks a row’s cells against.- Type:
The columns
AcquiredTrack.row()reads off the licence
- foley.sources.music_library.acquisition.LIST_SEPARATOR: str = '; '#
How a list is joined in one cell of the tracks table (
credited_by).
- foley.sources.music_library.acquisition.METADATA_COLUMNS: tuple[str, ...] = ('credited_by',)#
The metadata keys a titles file may set that are columns (the rest the library states).
- foley.sources.music_library.acquisition.REFUSED = 'refused'#
the licence gate refused the title for the intended use (nothing fetched);
wishlist: a paid library’s title.- Type:
The row statuses.
refused
- foley.sources.music_library.acquisition.STATUSES: tuple[str, ...] = ('acquired', 'kept', 'failed', 'refused', 'wishlist')#
Every status a row may have.
- foley.sources.music_library.acquisition.TRACK_COLUMNS: tuple[str, ...] = ('title', 'library', 'status', 'file', 'duration_s', 'sha256', 'license', 'license_url', 'rights_verified', 'commercial_ok', 'author', 'url', 'source_page_url', 'mirror', 'attribution', 'credited_by', 'error')#
The tracks table’s columns, in order (a row’s other metadata, a catalogue’s
feelorgenre, follows in name order).
- foley.sources.music_library.acquisition.WISHLIST = 'wishlist'#
the licence gate refused the title for the intended use (nothing fetched);
wishlist: a paid library’s title.- Type:
The row statuses.
refused
- foley.sources.music_library.acquisition.acquire(library, titles, *, out, fetch=None, transport=None, mirrors=(<function direct>, <function wayback>), verify=<function probe_verify>, kept=None, intended_use=IntendedUse(commercial=True, publish=True, redistribute_standalone=False, will_train=False, can_attribute=True, revenue_usd=0, allow_voice_or_trademark=False, territory=None), libraries=None, config=None, clock=<built-in function monotonic>, sleep=<built-in function sleep>)[source]#
Download
titlesoflibraryintoout, verified, one row each.titlesis a list, or{title: metadata}whose metadata (credited_by: the videos that credit it; a catalogue’sfeel,genre, …) rides on the row.libraryis aMusicLibraryor a name inlibraries(defaultMUSIC_LIBRARIES).Each title’s licence is put through
foley.licensing.refusal_reason()forintended_usefirst (fail-closed: an error in the gate is a refusal); a refused title is arefusedrow and is not fetched. The others are fetched from each ofmirrorsin turn until one gives bytesverifyaccepts.fetch(url -> bytes) replaces the HTTP path, paced per host by the config’srate; otherwise the files come throughtransport(default:requests) wrapped by foley’s resilience layer per host; that path honoursfoley.offline().configis laid overMUSIC_LIBRARY_HTTP;clock/sleeptime the waits.keptis{file name: sha256}a previous run recorded (kept_digests()), or{file name: {"sha256", "url", "mirror"}}(kept_sources(), whoseurlandmirrorthe kept row keeps): a file inoutwith that digest is kept, any other is fetched again. A paid library downloads nothing and returns wishlist rows.- Raises:
MusicLibraryError – For an unknown library, titles of the wrong shape, metadata that restates the row, two titles saved as one file, or a
keptvalue that is neither a digest nor a mapping with one.MusicToolMissing – When
verifyis the default and no probe can run (before anything is downloaded).ValueError – When both
fetchandtransportare given.
- Return type:
- foley.sources.music_library.acquisition.attribution_text(rows)[source]#
The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once.
Each block is
foley.provenance.credits.attribution_line()of the row’s licence: the library’s own wording, verbatim.>>> from foley.sources.music_library import MUSIC_LIBRARIES, track_license >>> lic = track_license(MUSIC_LIBRARIES["incompetech"], "A") >>> print(attribution_text([AcquiredTrack("A", "incompetech", ACQUIRED, lic)] * 2)) "A" Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 4.0 License http://creativecommons.org/licenses/by/4.0/
- Return type:
- foley.sources.music_library.acquisition.fetch_verified(url, *, mirrors, fetch, verify)[source]#
(bytes, seconds, the URL that served them, its mirror's name)from the first ofmirrorswhose bytesverifyaccepts; every mirror’s reason in oneMusicLibraryErrorwhen none does.The one mirror chain:
acquire()and the registered library sources’download(foley.sources.music_library.adapter) both run it.
- foley.sources.music_library.acquisition.files_store(out)[source]#
outas a{file name: bytes}store: a mapping is used as it is; a folder becomes foley’s file store over it (foley.stores.make_byte_store(), which makes the folder on its first write).- Return type:
- foley.sources.music_library.acquisition.held_rows(table)[source]#
The acquired or kept rows (with a file and a digest) of a tracks table, as row dicts:
tableistracks_table()’s CSV text (read_tracks_table()) or the rows themselves (AcquiredTrackor theirrow()dicts).
- foley.sources.music_library.acquisition.kept_digests(table)[source]#
{file: sha256}of the acquired or kept rows of a tracks table: what a rerun may keep.tableistracks_table()’s CSV text, or the rows themselves (AcquiredTrackor theirrow()dicts, read back from JSON).
- foley.sources.music_library.acquisition.kept_sources(table)[source]#
{file: {"sha256", "url", "mirror"}}of the acquired or kept rows of a tracks table:kept_digests()with where each file came from, so a kept row still says which URL and mirror served it (acquire(kept=...)takes either).
- foley.sources.music_library.acquisition.read_tracks_table(text)[source]#
The rows of a tracks table (
tracks_table()’s CSV text), as written; a byte-order mark an editor added (Excel does) is not part of the first column.>>> read_tracks_table("\ufefftitle,library\r\nA,incompetech\r\n") [{'title': 'A', 'library': 'incompetech'}]
- Raises:
MusicLibraryError – For a header that names a column twice, or a line with more cells than the header has columns (naming its line).
- Return type:
- foley.sources.music_library.acquisition.tracks_table(rows)[source]#
The tracks table as CSV text:
TRACK_COLUMNS, then any metadata the rows carry, in name order (read_tracks_table()reads it back).- Return type: