|
@@ -9,35 +9,12 @@ import mutagen.mp4
|
|
import os
|
|
import os
|
|
import symuid.library.itunes
|
|
import symuid.library.itunes
|
|
import urllib.parse
|
|
import urllib.parse
|
|
-import xml.etree.ElementTree
|
|
|
|
|
|
|
|
|
|
|
|
def generate_play_count_tag_label(player, library_id, reg_dt):
|
|
def generate_play_count_tag_label(player, library_id, reg_dt):
|
|
return 'symuid:pcnt:{}:{}:{}'.format(player, library_id, int(reg_dt.timestamp()))
|
|
return 'symuid:pcnt:{}:{}:{}'.format(player, library_id, int(reg_dt.timestamp()))
|
|
|
|
|
|
|
|
|
|
-def get_itunes_dict_value_node(dict_node, key):
|
|
|
|
- assert isinstance(dict_node, xml.etree.ElementTree.Element)
|
|
|
|
- assert isinstance(key, str)
|
|
|
|
- # WORKAROUND method getnext() is sadly not available
|
|
|
|
- for child_idx, child_node in enumerate(dict_node):
|
|
|
|
- if child_node.tag == 'key' and child_node.text == key:
|
|
|
|
- return dict_node[child_idx + 1]
|
|
|
|
- raise KeyError()
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def get_itunes_dict_value(dict_node, key):
|
|
|
|
- value_node = get_itunes_dict_value_node(dict_node, key)
|
|
|
|
- if value_node.tag == 'string':
|
|
|
|
- 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 set_play_count_tag(track_path, player, library_id, reg_dt, play_count):
|
|
def set_play_count_tag(track_path, player, library_id, reg_dt, play_count):
|
|
assert isinstance(reg_dt, dt.datetime), reg_dt
|
|
assert isinstance(reg_dt, dt.datetime), reg_dt
|
|
tag_label = generate_play_count_tag_label(
|
|
tag_label = generate_play_count_tag_label(
|
|
@@ -77,17 +54,17 @@ def set_play_count_tag(track_path, player, library_id, reg_dt, play_count):
|
|
def symuid_import_itunes(xml_library_path, root_url, root_path):
|
|
def symuid_import_itunes(xml_library_path, root_url, root_path):
|
|
root_path = os.path.expanduser(root_path)
|
|
root_path = os.path.expanduser(root_path)
|
|
lib = symuid.library.itunes.XmlLibrary(xml_library_path)
|
|
lib = symuid.library.itunes.XmlLibrary(xml_library_path)
|
|
- for track_node in lib._root_dict['Tracks'].iterfind('./dict'):
|
|
+ for track_dict in lib._root_dict['Tracks'].values():
|
|
try:
|
|
try:
|
|
- track_url = get_itunes_dict_value(track_node, 'Location')
|
|
+ track_url = track_dict['Location']
|
|
except KeyError:
|
|
except KeyError:
|
|
track_url = None
|
|
track_url = None
|
|
try:
|
|
try:
|
|
- play_count = get_itunes_dict_value(track_node, 'Play Count')
|
|
+ play_count = track_dict['Play Count']
|
|
except KeyError:
|
|
except KeyError:
|
|
play_count = 0
|
|
play_count = 0
|
|
try:
|
|
try:
|
|
- last_play_dt = get_itunes_dict_value(track_node, 'Play Date UTC')
|
|
+ last_play_dt = track_dict['Play Date UTC']
|
|
except KeyError:
|
|
except KeyError:
|
|
last_play_dt = None
|
|
last_play_dt = None
|
|
# TODO create tag if last_play_dt is None
|
|
# TODO create tag if last_play_dt is None
|