symuid-import-cmus 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import datetime as dt
  4. import os
  5. import symuid
  6. import symuid.library.cmus
  7. PLAYER_NAME = 'cmus'
  8. LIBRARY_ID_LENGTH_MIN = 8
  9. def symuid_import_cmus(library_id, cache_path):
  10. assert len(library_id) >= LIBRARY_ID_LENGTH_MIN, library_id
  11. lib = symuid.library.cmus.Cache(
  12. path=os.path.expanduser(cache_path),
  13. )
  14. for cmus_track in lib.get_tracks():
  15. if not os.path.exists(cmus_track.path):
  16. sys.stderr.write('{!r}: not found\n'.format(cmus_track.path))
  17. elif cmus_track.play_count > 0: # TODO play_count = 0
  18. symuid_track = symuid.Track(path=cmus_track.path.decode())
  19. last_count = symuid_track.get_latest_play_count(
  20. player=PLAYER_NAME,
  21. library_id=library_id,
  22. )
  23. assert last_count is None or last_count.count <= cmus_track.play_count, \
  24. (symuid_track.path, last_count, cmus_track.play_count)
  25. if last_count is None or last_count.count != cmus_track.play_count:
  26. symuid_track.register_play_count(
  27. player=PLAYER_NAME,
  28. library_id=library_id,
  29. register_dt=dt.datetime.now(),
  30. play_count=cmus_track.play_count,
  31. tag_set_cb=lambda tr, tag:
  32. print('{!r}: set tag {!r}'.format(tr.path, tag)),
  33. )
  34. def _init_argparser():
  35. import argparse
  36. argparser = argparse.ArgumentParser(description=None)
  37. argparser.add_argument('library_id')
  38. argparser.add_argument(
  39. 'cache_path',
  40. nargs='?',
  41. default='~/.config/cmus/cache',
  42. help='(default: %(default)r)',
  43. )
  44. return argparser
  45. def main(argv):
  46. argparser = _init_argparser()
  47. args = argparser.parse_args(argv[1:])
  48. symuid_import_cmus(**vars(args))
  49. return 0
  50. if __name__ == "__main__":
  51. import sys
  52. sys.exit(main(sys.argv))