Add `ol tag add` to tag every work matching a set of subjects

Open
#458 0 comments 0 reactions 1 assignee View on GitHub

@mekarpeles is already working on this.

Since Nov 21, 2026.

Assessment

This issue has not been assessed yet.

Description

CLI enhancement Works
Goal

Add one command to the ol CLI that tags every work matching a set of subjects:

ol tag add OL167T --subjects graphic_design,paintings,art_german

This adds the Tag /tags/OL167T to up to 100 works that have all of those subjects and don't have the Tag yet. Today this takes a hand-written script.

Command
ol tag add <TAG_OLID> --subjects <slug>[,<slug>...]
           [--rm-subjects <slug>[,<slug>...]]
           [--limit 100]
           [--yes]
Option Meaning
TAG_OLID The Tag to add, e.g. OL167T
--subjects Subject slugs (as in /subjects/graphic_design), comma-separated, no spaces. A work must have all of them.
--rm-subjects Subject slugs to remove from each tagged work. Default: remove nothing.
--limit Maximum number of works to change. Default 100.
--yes Save the changes. Without it, the command is a dry run that prints each change.
How it works
  1. Log in with the credentials from ol --configure. Print the account it will write as.
  2. Resolve the Tag. Fetch /tags/OL167T. Its tag_type decides the work field: genre → genres, subgenre → subgenres, and so on. Refuse a Tag whose type has no work field.
  3. Find works with search.json, which matches subject slugs and supports AND and NOT:
    subject_key:graphic_design AND subject_key:paintings AND subject_key:art_german AND -genre_key:OL167T, limited to --limit.
    Infobase queries (query.json) can't AND two values of the same field, so use search here.
  4. Fetch the works in one batch call and re-check each one: search can lag, so skip any work that already has the Tag. For --rm-subjects, remove each subject string whose slug matches.
  5. Save the batch with /api/save_many, writing the Tag as a reference: "genres": [{"key": "/tags/OL167T"}]. Keep existing entries; only add.
Built from what exists
Need Existing piece Work
Batch fetch #440 get_many() Fix first. It sends a comma list to /api/get_many.json (production returns 500) and doesn't unwrap the {"status": "ok", "result": {...}} reply. The working form is /api/get_many?keys=<JSON array>. Its tests mock the wrong shapes and need fixing too.
Safe search queries #454, URL-encoding search parameters Merge
Tag type → work field #457, explicit field map in Work.add_tags Reuse its map
Tag lookup olclient/entity_helpers/tag.py (Tag.get) Use as is
Save /api/save_many Add a small save_many() method

No other repository or script is needed: Tags are live and queryable on openlibrary.org.

Done when
  • #440 fixed and merged
  • ol tag add implemented as an ol subcommand, dry run by default
  • Tests: subject slugs → search query; Tag type → field; already-tagged works skipped; --rm-subjects removes only matching subjects; existing tags kept; dry run saves nothing
  • A --limit 5 dry run against openlibrary.org, output pasted in the PR
  • One small real run (--limit 5 --yes) only after a maintainer approves it in the PR
Not in scope
  • Replacing or removing Tags
  • Reshaping the rest of ol into subcommands
  • Query languages beyond --subjects
Dominant language
Python
Stars
509
Forks
116
Avg merge
11d 8h
Merged PRs (30d)
4

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from internetarchive/openlibrary-client

All issues in internetarchive/openlibrary-client

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.