Browse Source

itunes: show play count and dt last played

Fabian Peter Hammerle 6 years ago
parent
commit
e242ff8f07
1 changed files with 29 additions and 7 deletions
  1. 29 7
      symuid

+ 29 - 7
symuid

@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
+import dateutil.parser
 import mutagen
 import mutagen.id3
 import mutagen.mp4
@@ -76,11 +77,14 @@ def get_itunes_dict_value(dict_node, key):
         return value_node.text
     elif value_node.tag == 'integer':
         return int(value_node.text)
+    elif value_node.tag == 'date':
+        return dateutil.parser.parse(value_node.text)
     else:
         return value_node
 
 
-def import_itunes_play_count(itunes_library_path, itunes_library_root_url):
+def import_itunes_play_count(itunes_library_path, itunes_library_root_url, itunes_library_root_path):
+    itunes_library_root_path = os.path.expanduser(itunes_library_root_path)
     lib = xml.etree.ElementTree.parse(itunes_library_path)
     # WORKAROUND find('.//key[.="Library Persistent ID"]')
     #  -> SyntaxError: invalid predicate
@@ -92,16 +96,29 @@ def import_itunes_play_count(itunes_library_path, itunes_library_root_url):
             track_url = get_itunes_dict_value(track_node, 'Location')
         except KeyError:
             track_url = None
+        try:
+            play_count = get_itunes_dict_value(track_node, 'Play Count')
+        except KeyError:
+            play_count = 0
         if track_url and track_url.startswith(itunes_library_root_url):
-            track_path = urllib.parse.unquote(
-                track_url[len(itunes_library_root_url):],
+            track_path = os.path.join(
+                itunes_library_root_path,
+                urllib.parse.unquote(track_url[len(itunes_library_root_url):]),
             )
-            print(track_path)
+            if play_count > 0:
+                last_play_dt = get_itunes_dict_value(track_node, 'Play Date UTC')
+            else:
+                last_play_dt = None
+            print(track_path, play_count, last_play_dt)
 
 
-def symuid(path, itunes_library_path=None, itunes_library_root_url=None):
+def symuid(path, itunes_library_path=None, itunes_library_root_url=None, itunes_library_root_path=None):
     if itunes_library_path:
-        import_itunes_play_count(itunes_library_path, itunes_library_root_url)
+        import_itunes_play_count(
+            itunes_library_path,
+            itunes_library_root_url,
+            itunes_library_root_path,
+        )
     if os.path.isdir(path):
         for dirpath, dirnames, filenames in os.walk(path):
             for filename in filenames:
@@ -129,7 +146,12 @@ def _init_argparser():
     argparser.add_argument(
         '--itunes-library-root-url',
         default='file://localhost/',
-        help='(default: %(default)s)',
+        help='(default: %(default)r)',
+    )
+    argparser.add_argument(
+        '--itunes-library-root-path',
+        default='',
+        help='(default: %(default)r)',
     )
     return argparser