Repository: naftulikay/mutagen-tools

Issue:
Disc Count isn't Being Copied Properly from EasyTAG FLACs to MP3s
**TL;DR** EasyTAG removes the `encoder` tag :unamused: and screws things up with the tags it uses.

Three bugs have been identified in `flac2id3`. two of which are fixable in this project:

 - Prevent EasyTag from removing the `encoder` tag.
 - [ ] Correctly bring in `tracktotal` into `TRCK` conversions.
 - [ ] Correctly bring in `disctotal` into `TPOS` conversions.

Comments:

Patch:
diff --git a/src/mutagentools/flac/convert.py b/src/mutagentools/flac/convert.py
index 0ce867e..8b0438a 100644
--- a/src/mutagentools/flac/convert.py
+++ b/src/mutagentools/flac/convert.py
@@ -46,8 +46,8 @@ def convert_flac_to_id3(flac):
         result.append(convert_genre_to_tcon(tags.pop('genre'), tags.pop('style') if 'style' in tags.keys() else []))
 
     if 'discnumber' in tags.keys():
-        result.append(convert_disc_number_to_tpos(tags.pop('discnumber'),
-            tags.pop('totaldiscs') if 'totaldiscs' in tags.keys() else None))
+        result.append(convert_disc_number_to_tpos(first_of_list(tags.pop('discnumber')),
+            first_of_list(first(pop_keys(tags, 'totaldiscs', 'disctotal')))))
 
     if contains_any(tags.keys(), 'date', 'year'):
         result.append(convert_date_to_tdrc(first(pop_keys(tags, 'date', 'year'))))
@@ -67,7 +67,7 @@ def convert_flac_to_id3(flac):
 
     if 'tracknumber' in tags.keys():
         tracknumber = first_of_list(tags.pop('tracknumber'))
-        totaltracks = first_of_list(tags.pop('totaltracks')) if 'totaltracks' in tags.keys() else None
+        totaltracks = first_of_list(first(pop_keys(tags, 'totaltracks', 'tracktotal')))
 
         if PART_OF_SET.match(tracknumber):
             # it's a complicated dude


Test patch:
diff --git a/src/mutagentools/flac/tests.py b/src/mutagentools/flac/tests.py
index d57b6e9..7f42ec8 100644
--- a/src/mutagentools/flac/tests.py
+++ b/src/mutagentools/flac/tests.py
@@ -242,6 +242,62 @@ class FullConversionTestCase(unittest.TestCase):
         self.assertEqual(['Artist'], id3.get('TPE1'))
         self.assertEqual(2017, int(str(id3.get('TDRC').text[0])))
 
+    def test_convert_tracktotal(self):
+        """Tests that converting a track number and total number of tracks is accomplished."""
+        tags = {
+            'tracknumber': '1',
+            'totaltracks': '3',
+            'tracktotal': '5',
+        }
+
+        flac_mock = mock.MagicMock()
+        flac_mock.tags = tags
+
+        id3 = ID3()
+        list(map(lambda t: id3.add(t), convert_flac_to_id3(flac_mock)))
+        # make sure that no TXXX tags are created
+        self.assertEqual(0, len(list(
+            filter(lambda f: f.FrameID == 'TXXX', id3.values()
+        ))))
+
+    def test_convert_tracktotal_no_total(self):
+        """Tests that total track numbers are detected properly."""
+        # test that the track got populated singularly
+        flac_mock = mock.MagicMock()
+        flac_mock.tags = { 'tracknumber': '1' }
+
+        id3 = ID3()
+        list(map(lambda t: id3.add(t), convert_flac_to_id3(flac_mock)))
+        self.assertEqual('01', id3.get('TRCK'))
+
+    def test_convert_disctotal_no_total(self):
+        """Tests that total disc numbers something something."""
+        # test that the track got populated singularly
+        flac_mock = mock.MagicMock()
+        flac_mock.tags = { 'discnumber': '1' }
+
+        id3 = ID3()
+        list(map(lambda t: id3.add(t), convert_flac_to_id3(flac_mock)))
+        self.assertEqual('1', id3.get('TPOS'))
+
+    def test_convert_disctotal(self):
+        """Tests that total disc numbers something something."""
+        # test that the track got populated singularly
+        flac_mock = mock.MagicMock()
+        flac_mock.tags = {
+            'discnumber': '1',
+            'totaldiscs': '3',
+            'disctotal': '5',
+        }
+
+        id3 = ID3()
+        list(map(lambda t: id3.add(t), convert_flac_to_id3(flac_mock)))
+        self.assertEqual('1/3', id3.get('TPOS'))
+        # make sure that no TXXX tags are created
+        self.assertEqual(0, len(list(
+            filter(lambda f: f.FrameID == 'TXXX', id3.values()
+        ))))
+
 
 class IndividualConversionTestCase(unittest.TestCase):