#!/usr/bin/env python3 # -*- coding: utf-8 -*- import datetime as dt import os import symuid import symuid.library.cmus 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( path=os.path.expanduser(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: # TODO play_count = 0 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, cmus_track.play_count) if last_count is None or last_count.count != cmus_track.play_count: symuid_track.register_play_count( player=PLAYER_NAME, library_id=library_id, register_dt=dt.datetime.now(), play_count=cmus_track.play_count, tag_set_cb=lambda tr, tag: print('{!r}: set tag {!r}'.format(tr.path, tag)), ) def _init_argparser(): import argparse argparser = argparse.ArgumentParser(description=None) argparser.add_argument('library_id') argparser.add_argument( 'cache_path', nargs='?', default='~/.config/cmus/cache', help='(default: %(default)r)', ) return argparser def main(argv): argparser = _init_argparser() args = argparser.parse_args(argv[1:]) symuid_import_cmus(**vars(args)) return 0 if __name__ == "__main__": import sys sys.exit(main(sys.argv))