16
submitted 10 months ago by sabreW4K3@lemmy.tf to c/python@programming.dev

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?

you are viewing a single comment's thread
view the rest of the comments
[-] ishanpage@programming.dev 2 points 10 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
[-] sabreW4K3@lemmy.tf 1 points 10 months ago* (last edited 10 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 6 points 10 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

[-] sabreW4K3@lemmy.tf 1 points 10 months ago

Thanks that was really helpful

this post was submitted on 12 Jan 2024
16 points (100.0% liked)

Python

6375 readers
54 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS