symuid-import-cmus 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: # check before symuid.Track to improve performance
  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.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. symuid.PlayCount(
  28. player=PLAYER_NAME,
  29. library_id=library_id,
  30. register_dt=dt.datetime.now(),
  31. count=cmus_track.play_count,
  32. ),
  33. tag_set_cb=lambda tr, tag:
  34. print('{!r}: set tag {!r}'.format(tr.path, tag)),
  35. )
  36. def _init_argparser():
  37. import argparse
  38. argparser = argparse.ArgumentParser(description=None)
  39. argparser.add_argument('library_id')
  40. argparser.add_argument(
  41. 'cache_path',
  42. nargs='?',
  43. default='~/.config/cmus/cache',
  44. help='(default: %(default)r)',
  45. )
  46. return argparser
  47. def main(argv):
  48. argparser = _init_argparser()
  49. args = argparser.parse_args(argv[1:])
  50. symuid_import_cmus(**vars(args))
  51. return 0
  52. if __name__ == "__main__":
  53. import sys
  54. sys.exit(main(sys.argv))