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.library_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 _main(): argparser = argparse.ArgumentParser(description='Import play counts from iTunes XML Library') argparser.add_argument('xml_library_path') argparser.add_argument( '--path-regex-sub', nargs=2, action='append', metavar=('regex', 'replacement'), default=[], help='(default: no substitution)', ) args = argparser.parse_args() import_itunes_xml_library(**vars(args))