__init__.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import datetime as dt
  2. import os
  3. import re
  4. import typing
  5. import mutagen
  6. from symuid import _tag_interface
  7. from symuid._datetime import datetime_utc_now, unix_epoch_time_to_datetime_utc
  8. class PlayCount:
  9. def __init__(self, player, library_id, register_dt, count):
  10. self.player = player
  11. self.library_id = library_id
  12. assert isinstance(register_dt, dt.datetime), register_dt
  13. assert register_dt.tzinfo is not None, register_dt
  14. self.register_dt = register_dt
  15. assert isinstance(count, int) and count >= 0, count
  16. self.count = count
  17. def __eq__(self, other):
  18. # pylint: disable=unidiomatic-typecheck
  19. return type(self) == type(other) and vars(self) == vars(other)
  20. def __hash__(self):
  21. attrs_sorted = sorted(vars(self).items(), key=lambda p: p[0])
  22. return hash(tuple(v for k, v in attrs_sorted))
  23. def __repr__(self):
  24. return 'PlayCount({})'.format(', '.join(
  25. '{}={!r}'.format(k, v) for k, v in vars(self).items()
  26. ))
  27. class Track:
  28. PATH_DEFAULT_IGNORE_REGEX = r'\.(itdb|itc2|itl|jpg|midi?|plist|xml|zip)$'
  29. def __init__(self, path):
  30. self._iface = self._select_tag_interface(path)
  31. @staticmethod
  32. def _select_tag_interface(track_path) -> _tag_interface.TagInterface:
  33. mutagen_file = mutagen.File(track_path)
  34. if mutagen_file is None:
  35. raise NotImplementedError(track_path)
  36. if isinstance(mutagen_file.tags, mutagen.id3.ID3):
  37. return _tag_interface.ID3(mutagen_file)
  38. if isinstance(mutagen_file.tags, mutagen.mp4.MP4Tags):
  39. return _tag_interface.MP4(mutagen_file)
  40. if isinstance(mutagen_file, (mutagen.oggopus.OggOpus,
  41. mutagen.oggvorbis.OggVorbis)):
  42. return _tag_interface.Ogg(mutagen_file)
  43. raise NotImplementedError((track_path, type(mutagen_file)))
  44. @property
  45. def path(self):
  46. return self._iface.track_path
  47. @property
  48. def comment(self):
  49. return self._iface.get_comment()
  50. @comment.setter
  51. def comment(self, comment):
  52. self._iface.set_comment(comment)
  53. self._iface.save()
  54. def get_uuid(self):
  55. return self._iface.get_track_uuid()
  56. def assign_uuid(self, uuid):
  57. if self.get_uuid():
  58. raise Exception("{!r} already has an uuid".format(self.path))
  59. self._iface.set_track_uuid(uuid)
  60. self._iface.save()
  61. def _get_play_counts(self, player=None, library_id=None):
  62. label = 'symuid:pcnt'
  63. assert library_id is None or player is not None
  64. if player:
  65. label += ':' + player
  66. if library_id:
  67. label += ':' + library_id
  68. elif library_id:
  69. raise Exception((player, library_id))
  70. for k, count in self._iface.get_free_ints(label):
  71. player, library_id, register_ts_dec = k.split(':')[2:]
  72. yield PlayCount(
  73. player=player,
  74. library_id=library_id,
  75. register_dt=unix_epoch_time_to_datetime_utc(
  76. int(register_ts_dec)),
  77. count=count,
  78. )
  79. def get_play_counts(self) -> typing.Iterator[PlayCount]:
  80. return self._get_play_counts()
  81. def _get_latest_play_counts(self, player=None, library_id=None):
  82. latest = {}
  83. for count in self._get_play_counts(player, library_id):
  84. if not count.player in latest:
  85. latest[count.player] = {}
  86. if not count.library_id in latest[count.player] \
  87. or latest[count.player][count.library_id].register_dt < count.register_dt:
  88. latest[count.player][count.library_id] = count
  89. return [c for p in latest.values() for c in p.values()]
  90. def get_latest_play_count(self, player, library_id):
  91. assert player is not None, player
  92. assert library_id is not None, library_id
  93. counts = self._get_latest_play_counts(player, library_id)
  94. if len(counts) == 0:
  95. return None
  96. assert len(counts) == 1, counts
  97. return counts[0]
  98. def get_play_count_sum(self, player=None, library_id=None):
  99. return sum(c.count for c in self._get_latest_play_counts(player, library_id))
  100. def register_play_count(self, play_count, tag_set_cb=None):
  101. assert isinstance(play_count, PlayCount), play_count
  102. tag_label = 'symuid:pcnt:{}:{}:{:.0f}'.format(
  103. play_count.player, play_count.library_id,
  104. play_count.register_dt.timestamp(),
  105. )
  106. current_count = self._iface.get_free_int(tag_label)
  107. if current_count is None:
  108. new_tag = self._iface.set_free_int(tag_label, play_count.count)
  109. self._iface.save()
  110. if tag_set_cb:
  111. tag_set_cb(self, new_tag)
  112. elif current_count != play_count.count:
  113. raise Exception((current_count, play_count.count))
  114. def increase_play_count(self, player, library_id, tag_set_cb=None):
  115. current_pc = self.get_latest_play_count(player, library_id)
  116. self.register_play_count(
  117. PlayCount(
  118. player=player,
  119. library_id=library_id,
  120. register_dt=datetime_utc_now(),
  121. count=current_pc.count + 1 if current_pc else 1,
  122. ),
  123. tag_set_cb=tag_set_cb,
  124. )
  125. @classmethod
  126. def walk(cls, root_path, path_ignore_regex, ignored_cb=None, unsupported_cb=None):
  127. for dirpath, _, filenames in os.walk(root_path):
  128. for filename in filenames:
  129. track_path = os.path.join(dirpath, filename)
  130. if path_ignore_regex.search(track_path):
  131. if ignored_cb is not None:
  132. ignored_cb(track_path)
  133. else:
  134. try:
  135. yield cls(track_path)
  136. except NotImplementedError as exc:
  137. if unsupported_cb is not None:
  138. unsupported_cb(track_path, exc)