import argparse import os import sys import symuid import symuid.library.cmus from symuid._datetime import datetime_utc_now # TODO rename module to cmus.py # after ImportMismatchError on `pytest --doctest-modules` was fixed _PLAYER_NAME = "cmus" _LIBRARY_ID_LENGTH_MIN = 8 def symuid_import_cmus(library_id, cache_path): assert len(library_id) >= _LIBRARY_ID_LENGTH_MIN, library_id lib = symuid.library.cmus.Cache(cache_path) for cmus_track in lib.get_tracks(): if not os.path.exists(cmus_track.path): sys.stderr.write("{!r}: not found\n".format(cmus_track.path)) elif ( cmus_track.play_count > 0 ): # check before symuid.Track to improve performance symuid_track = symuid.Track(path=cmus_track.path.decode()) last_count = symuid_track.get_latest_play_count( player=_PLAYER_NAME, library_id=library_id, ) assert last_count is None or last_count.count <= cmus_track.play_count, ( symuid_track.path, last_count.count, cmus_track.play_count, ) if last_count is None or last_count.count != cmus_track.play_count: symuid_track.register_play_count( symuid.PlayCount( player=_PLAYER_NAME, library_id=library_id, register_dt=datetime_utc_now(), count=cmus_track.play_count, ), tag_set_cb=lambda track, tag: print( "{!r}: set tag {!r}".format(track.path, tag) ), ) def _main(): argparser = argparse.ArgumentParser( description="Import play counts from cmus' cache" ) argparser.add_argument("library_id") argparser.add_argument( "cache_path", nargs="?", default=os.path.expanduser("~/.config/cmus/cache"), help="(default: %(default)r)", ) args = argparser.parse_args() symuid_import_cmus(**vars(args))