itunes_xml.py 1.7 KB

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