Help trying to extract some data
from sabreW4K3@lemmy.tf to python@programming.dev on 12 Jan 2024 14:31
https://lemmy.tf/post/3288922

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?

#python

threaded - newest

ggwithgg@feddit.nl on 12 Jan 2024 14:40 next collapse

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.

jnovinger@programming.dev on 12 Jan 2024 20:39 collapse

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?

sabreW4K3@lemmy.tf on 12 Jan 2024 23:42 next collapse

For testing purposes, same file every time, this one: file.coffee/u/IxKmfKfUwgPybq_vv8YJc.mp3

sabreW4K3@lemmy.tf on 13 Jan 2024 21:19 collapse

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 on 13 Jan 2024 09:14 collapse

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
sabreW4K3@lemmy.tf on 13 Jan 2024 10:03 collapse

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
ishanpage@programming.dev on 14 Jan 2024 00:57 collapse

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

sabreW4K3@lemmy.tf on 14 Jan 2024 12:37 collapse

Thanks that was really helpful