symuid-import-itunes 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import datetime as dt
  4. import os
  5. import re
  6. import symuid
  7. import symuid.library.itunes
  8. import sys
  9. def symuid_import_itunes(xml_library_path, path_regex_sub):
  10. lib = symuid.library.itunes.XmlLibrary(xml_library_path)
  11. for itunes_track in lib.tracks:
  12. # TODO create tag if last_play_dt is None
  13. if itunes_track.last_play_dt and itunes_track.local:
  14. track_path = itunes_track.local_path
  15. for pattern, repl in path_regex_sub:
  16. track_path = re.sub(pattern, repl, track_path)
  17. if not os.path.exists(track_path):
  18. sys.stderr.write('{!r}: not found\n'.format(track_path))
  19. else:
  20. symuid_track = symuid.Track(path=track_path)
  21. # TODO dt=dt.datetime.now()
  22. symuid_track.register_play_count(
  23. player='itunes',
  24. library_id=lib.id,
  25. register_dt=itunes_track.last_play_dt,
  26. play_count=itunes_track.play_count,
  27. tag_set_cb=lambda tr, tag:
  28. print('{!r}: set tag {!r}'.format(tr.path, tag)),
  29. )
  30. def _init_argparser():
  31. import argparse
  32. argparser = argparse.ArgumentParser(description=None)
  33. argparser.add_argument('xml_library_path')
  34. argparser.add_argument(
  35. '--path-regex-sub',
  36. nargs=2,
  37. action='append',
  38. metavar=('regex', 'replacement'),
  39. default=[],
  40. help='(default: %(default)r)',
  41. )
  42. return argparser
  43. def main(argv):
  44. argparser = _init_argparser()
  45. args = argparser.parse_args(argv[1:])
  46. symuid_import_itunes(**vars(args))
  47. return 0
  48. if __name__ == "__main__":
  49. sys.exit(main(sys.argv))