Write data using hook plugin?

I’m trying to help the importer find album types by scanning for things like “OST” in the file path. I can’t figure out how to write data after I’ve found something I want to change.

I’m on windows, using cmder. Here are my hooks:

hook:
  hooks:
    #############################################
    - event: album_imported
      command: echo "\"{album}\" | \"{album.albumtype}\" | \"{album.albumtypes}\""
    - event: before_item_moved
      command: echo "before item moved | {item} | {item.albumtype} |" 
    - event: write
      command: 'if "soundtrack" in {path}: beet modify {item} albumtype=soundtrack'
    - event: write
      command: 'if "OST" in {path}: beet modify {item} albumtype=soundtrack'
    - event: write
      command: 'if "BGM" in {path}: beet modify {item} albumtype=soundtrack'

I get the error:

hook: hook for write failed: [WinError 2] The system cannot find the file specified

Hi, welcome!

The hook plugin runs a system command (like echo or beet), so for your write events it’s telling windows to execute the command if "soundtrack" in {path}: beet modify {item} albumtype=soundtrack, which isn’t a known command (windows can’t find the command file).

Are you trying to help the importer more quickly identify albums during autotagging, or do you want to overwrite the albumtype that is set by the autotagger?

Are you trying to help the importer more quickly identify albums during autotagging, or do you want to overwrite the albumtype that is set by the autotagger?

Overwrite what is set by the autotagger. I’ve found I have several albums that aren’t tagged correctly or won’t update unless I use the edit plugin. I’m trying to speed up & automate the process of fixing them.

Hmm, I don’t think there’s a way to tell the importer to look for things in the path, but I could be wrong. There are a couple ways I can think of (that I have not tested well, so use with care):

Option 1

Use the --set option with import and only import certain files, e.g. using shell globbing

beet import .\music\to\import\**soundtrack** --set albumtype="soundtrack"

Obviously this only works for albums that haven’t been imported yet.

Option 2

Stop beets from moving or copying the files, so that the original paths are retained after import

import:
  copy: no
  move: no

Use the inline plugin to define a field like

item_fields:
  soundtrack_in_path: "yes" if "soundtrack" in path or "OST" in path or "BGM" in path else None

Then modify the items

beet modify soundtrack_in_path:yes albumtype=soundtrack

Then you can copy or move the files, and get rid of the custom field if you don’t need it any more.

To modify automatically after import you could set that as a hook

hook:
  hooks:
    - event: import_task_files
      command: beet modify soundtrack_in_path:yes albumtype=soundtrack
1 Like

the inline field is what I needed, thanks for the help!

note: I had to change "soundtrack" to b"soundtrack" and similar for the other strings

1 Like