In my file, I have

from mutagen.id3 import ID3
tags = ID3(mp3_file)

Now if I do print(tags.keys())

It informs me that there’s a TXXX:FMPS_Rating_Amarok_Score

But when I attempt to print(tags["TXXX:FMPS_Rating_Amarok_Score"])

It says there’s a KeyError. What am I doing wrong?

  • @ggwithgg@feddit.nl
    link
    fedilink
    105 months ago

    I suspect your ‘tags’ is not a dict object, but some extension of it. Lookup its type, or the documentation of the library you are using for how to retrieve values from an ID3 object.

    • Jason NovingerM
      link
      fedilink
      35 months ago

      Looking at the docs, it looks like it’s an instance of ID3Tags, which appears to be based on couple of helper classes mutagen._util.DictProxy and mutagen._tags.Tags, where DictProxy (and its base DictMixin) provides the dict-like interface. Underneath that, it looks like it’s storing the actual values in a simple dict (DictProxy.__dict) and proxying to that.

      I’m not seeing anything obvious that would muck with the incoming lookup key anywhere in ID3Tags or DictProxy.__getitem__ or any of the other base classes.

      I have to jump off to pack for a trip, but might try this out later in a live shell session to see if there’s something odd going on with the API.

      In the meantime, OP, are you positive you were looking at the same file each time? Was this in a script or in a live Python shell session?

      • @sabreW4K3OP
        link
        English
        25 months ago

        So I tried all the different methods you mentioned but everyone fails and so when I took a deep breath, I figured that I’m probably not understanding properly how it works.

        I can say this without a doubt though, the needless complexity regarding these rating tags is stupid. I don’t understand why people thought this made sense. It’s illogical!

  • @ishanpage@programming.dev
    link
    fedilink
    English
    25 months ago

    This is strange. I tried your snippet with your file and it works for me:

    (env) ➜  testing cat x.py
    from mutagen.id3 import ID3
    tags = ID3("myfile.mp3")
    print(tags["TXXX:FMPS_Rating_Amarok_Score"])                                                                                                                                               
    (env) ➜  testing python x.py
    0.78
    
    • @sabreW4K3OP
      link
      English
      1
      edit-2
      5 months ago

      I’m getting 0.66 when I do get a rating. But I’m definitely not getting that to print. Here’s the full file

      import os
      import mutagen
      import requests
      import urllib.parse
      from mutagen.easyid3 import EasyID3
      from mutagen.id3 import ID3
      from pprint import pprint
      
      # Navidrome credentials
      navidrome_url = "http://navidrome.local:4533"
      navidrome_username = "your-username"
      navidrome_password = "your-password"
      hex_encoded_pass = navidrome_password.encode().hex()
      headers = None
      
      # Directory containing MP3 files
      mp3_directory = "/nfs"
      
      def extract_rating(mp3_file):
          global rating
          audio = mutagen.File(mp3_file)
          tags = ID3(mp3_file)
      
          print(tags["TXXX:FMPS_Rating_AMarok_Score"]) #Gives KeyError
      
          for frame in tags.getall("TXXX"):
            rating = frame #This is terrible. The last key is the rating and since I can't call it by the key, I'm just refilling the variable
            print(rating)
      
      # It was moaning about strings and floats, so commented out
      #    if rating >= 1.0:
      #      return 5
      #    elif rating >= 0.8:
      #      return 4
      #    elif rating >= 0.6:
      #      return 3
      #    elif rating >= 0.4:
      #      return 2
      #    elif rating >= 0.2:
      #      return 1
      #    else:
      #      return 0
      
      #    return rating
      
      def update_rating_on_navidrome(track_id, rating):
          url = f"{navidrome_url}/rest/setRating?id={track_id}&u={navidrome_username}&p=enc: {hex_encoded_pass}&v=1.12.0&rating={rating}"
          data = {"rating": rating}
          response = requests.get(url, headers=headers, json=data)
      
      def find_track_id_on_navidrome(mp3_file):
          url = urllib.parse(url)
          url = f"{navidrome_url}/rest/getSong?path={mp3_file.encode()}&u={navidrome_username}&p=enc: {hex_encoded_pass}&v=1.12.0"
          response = requests.get(url, headers=headers, json=data)
          return url[track_id]
      
          if response.status_code == 204:
              print(f"Rating updated successfully for track {track_id}")
          else:
              print(f"Failed to update rating for track {track_id}: {response.text}")
      
      print("hello")
      #test = 
      print(os.listdir(mp3_directory))
      
      for foldername in os.listdir(mp3_directory):
        folderpath = "/".join([mp3_directory, foldername])
        for filename in os.listdir(folderpath):
          if filename.endswith(".mp3"):
              mp3_file = "/".join([folderpath, filename])
              rating = extract_rating(mp3_file)
              print(mp3_file, rating, sep= "_____")
              print(rating)
      #
      #        # Implement logic to find the track ID on Navidrome based on filename or other metadata
      #        track_id = find_track_id_on_navidrome(filename)  # Replace with your implementation#
      #
      #        if track_id:
      #            update_rating_on_navidrome(track_id, rating)
      #        else:
      #            print(f"Track ID not found on Navidrome for {filename}")
      

      Have I called something erroneously that would mess it up?

      Sorry if it’s terrible to read, up until I started trying to do this, I had never touched Python before and haven’t attempted to code for years.

      • @ishanpage@programming.dev
        link
        fedilink
        65 months ago

        Dict keys are case sensitive in python. In your code I can see the key you’ve used has a capital M in Amarok. Maybe that’s the issue here

        • @sabreW4K3OP
          link
          English
          15 months ago

          Thanks that was really helpful