Browse Source

symuid-import-itunes: replace params --root-url/path with --path-regex-sub

Fabian Peter Hammerle 6 years ago
parent
commit
bf9de00f58
2 changed files with 46 additions and 24 deletions
  1. 22 24
      symuid-import-itunes
  2. 24 0
      symuid/library/itunes.py

+ 22 - 24
symuid-import-itunes

@@ -2,13 +2,12 @@
 # -*- coding: utf-8 -*-
 
 import datetime as dt
-import dateutil.parser
 import mutagen
 import mutagen.id3
 import mutagen.mp4
 import os
+import re
 import symuid.library.itunes
-import urllib.parse
 
 
 def generate_play_count_tag_label(player, library_id, reg_dt):
@@ -51,24 +50,25 @@ def set_play_count_tag(track_path, player, library_id, reg_dt, play_count):
         raise Exception(track_path)
 
 
-def symuid_import_itunes(xml_library_path, root_url, root_path):
-    root_path = os.path.expanduser(root_path)
+def symuid_import_itunes(xml_library_path, path_regex_sub):
     lib = symuid.library.itunes.XmlLibrary(xml_library_path)
     for track in lib.tracks:
         # TODO create tag if last_play_dt is None
-        if track.last_play_dt and track.location_url and track.location_url.startswith(root_url):
-            track_path = os.path.join(
-                root_path,
-                urllib.parse.unquote(track.location_url[len(root_url):]),
-            )
-            # TODO dt=dt.datetime.now()
-            set_play_count_tag(
-                track_path=track_path,
-                player='itunes',
-                library_id=lib.id,
-                reg_dt=track.last_play_dt,
-                play_count=track.play_count,
-            )
+        if track.last_play_dt and track.local:
+            track_path = 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):
+                raise ValueError(track_path)
+            else:
+                # TODO dt=dt.datetime.now()
+                set_play_count_tag(
+                    track_path=track_path,
+                    player='itunes',
+                    library_id=lib.id,
+                    reg_dt=track.last_play_dt,
+                    play_count=track.play_count,
+                )
 
 
 def _init_argparser():
@@ -76,13 +76,11 @@ def _init_argparser():
     argparser = argparse.ArgumentParser(description=None)
     argparser.add_argument('xml_library_path')
     argparser.add_argument(
-        '--root-url',
-        default='file://localhost/',
-        help='(default: %(default)r)',
-    )
-    argparser.add_argument(
-        '--root-path',
-        default='',
+        '--path-regex-sub',
+        nargs=2,
+        action='append',
+        metavar=('regex', 'replacement'),
+        default=[],
         help='(default: %(default)r)',
     )
     return argparser

+ 24 - 0
symuid/library/itunes.py

@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 
 import dateutil.parser
+import os
+import urllib.parse
 import xml.etree.ElementTree
 
 
@@ -55,17 +57,39 @@ class XmlDict:
 
 class Track:
 
+    LOCAL_LOCATION_URL_PREFIX = 'file://localhost/'
+
     def __init__(self, track_dict):
         assert isinstance(track_dict, XmlDict)
         self._dict = track_dict
+        self._id = self._dict['Track ID']
         self._location_url = self._dict.get('Location', None)
         self._play_count = self._dict.get('Play Count', 0)
+        assert isinstance(self._play_count, int)
         self._last_play_dt = self._dict.get('Play Date UTC', None)
 
+    @property
+    def id(self):
+        return self._id
+
     @property
     def location_url(self):
         return self._location_url
 
+    @property
+    def local(self):
+        return self.location_url is not None \
+            and self.location_url.startswith(self.LOCAL_LOCATION_URL_PREFIX)
+
+    @property
+    def local_path(self):
+        if self.local:
+            return os.path.sep + urllib.parse.unquote(
+                self.location_url[len(self.LOCAL_LOCATION_URL_PREFIX):],
+            )
+        else:
+            raise ValueError(self.location_url)
+
     @property
     def play_count(self):
         return self._play_count