123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import argparse
- import os
- import re
- import sys
- import symuid
- import symuid.library.itunes
- # TODO rename module to itunes.py
- # after ImportMismatchError on `pytest --doctest-modules` was fixed
- def import_itunes_xml_library(xml_library_path, path_regex_sub):
- lib = symuid.library.itunes.XmlLibrary(xml_library_path)
- for itunes_track in lib.tracks:
- # TODO create tag if last_play_dt is None
- if itunes_track.last_play_dt and itunes_track.local:
- track_path = itunes_track.local_path
- for pattern, repl in path_regex_sub:
- track_path = re.sub(pattern, repl, track_path)
- if not os.path.exists(track_path):
- sys.stderr.write('{!r}: not found\n'.format(track_path))
- else:
- symuid_track = symuid.Track(path=track_path)
- # TODO dt=dt.datetime.now()
- symuid_track.register_play_count(
- symuid.PlayCount(
- player='itunes',
- library_id=lib.id,
- register_dt=itunes_track.last_play_dt,
- count=itunes_track.play_count,
- ),
- tag_set_cb=lambda tr, tag:
- print('{!r}: set tag {!r}'.format(tr.path, tag)),
- )
- def _init_argparser():
- argparser = argparse.ArgumentParser(description=None)
- argparser.add_argument('xml_library_path')
- argparser.add_argument(
- '--path-regex-sub',
- nargs=2,
- action='append',
- metavar=('regex', 'replacement'),
- default=[],
- help='(default: %(default)r)',
- )
- return argparser
- def _main():
- argparser = _init_argparser()
- args = argparser.parse_args()
- import_itunes_xml_library(**vars(args))
|