Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:26:22+00:00 2026-06-17T13:26:22+00:00

I am trying to use the MusicBrainz API via python and am having a

  • 0

I am trying to use the MusicBrainz API via python and am having a harrowing time trying to figure out why my loop wont break after a certain condition is met. I am passing the duration of a song and the acoustic id via a URL that fetches JSON. I am not sure if the JSON is invaild, but am getting the formatted output to play with. The part where I am stuck is inspite of me telling the code to break when the duration of the song matches the duration of the returned JSON, it still continues to show the rest of the data that matched the duration. Also, I am getting a KeyError for one of the main data elements, which looks good to me! Please help!

Python Code being used:

import urllib2
import urllib
import simplejson
import json
import pprint

duration = 266
output = 'AQABz0q6JJFknHgOHH0q9DyQw8lxIzmaLzl6HHyE7yO6G4dP9Mc3JDry4FJR5RfIvccWH0d_PD-O63gi9KTQPjme4-uDxl04hMqcQf0RfXgmKWDSDD8aF9dYTDl5hEPy46gnHY-iPEWZEWfw42

-

SIHZiQQ9x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1zI-dxZzme-

HjQkuiLH36RM0j4oD8-EueDLjE8EvUyfBc-hhF-

C8n4IH1QncELRoFTgjADFCCKdIOIBYQZ0UABgDAnsAGCQuGEAgwhwACGDBFgMIIAAQKsMIQY4QARQFAAiJGeEQOoBEgpEZxQAAEipDAOEYCEAU4AZAEBQAoYDGFAMGKIwIIRgIg4jiDFlIAAIEIEBMwwBJQkX

hiohHEGIKAkcsIp4RAigAHBgPKSAKIIAoAxgJBjlhhkCIDAIAQkIgYAKSyQCDpCARIESWUAMYQ4YQGzGhhkADBAEOIEMAAhZRlQRgGkBBECMeAIQYAghxBAChAigANEIWKQcEbB5ZxlChGKACCAEKWEsIIyiw

RiTAABgBNKAAIIQMQhJCCoTAgliAIAICYwAIRZowRgxCllDBOAAEGABQIRoQ0ABIFgHCEECSEUMhQBAhkDwABCBANIACSABMxoAYGEBFRCASBEGAMBEMAIxBQQABkkLALAAIaIYIwggoACShgEjLEKAQ0AQsY

goBwAAImMFBBIQQUUQcgQARQI'
data = {}
data['client'] = '8XaBELgH'
data['format'] = 'json'
data['meta'] = '2'
data['duration'] = str(duration)
data['fingerprint'] = output
resp = urllib2.urlopen('http://api.acoustid.org/v2/lookup', urllib.urlencode(data))
str_to_load = resp.read()
json = simplejson.loads(str_to_load)
#pprint.pprint(json)
for result in json['results']:
    for recording in result['recordings']:
        for track in recording['tracks']:
            track_artist = ''
            for artist in track['artists']:
                try:
                    track_artist += repr(artist['name']) + ' '
                except:
                    track_artist = ''
            #print str(track['duration']) + '-' + track['medium']['release']['title'] + '-' + track['title'] + '-' + track_artist
            if str(track['duration']) == str(duration):
                print 'Matched:' + str(track['duration']) + '-' + track['medium']['release']['title'] + '-' + track['title'] + '-' + track_artist
                break

Returned JSON:

{
    "status": "ok",
    "results": [{
        "recordings": [{
            "duration": 264,
            "tracks": [{
                "duration": 264,
                "position": 7,
                "medium": {
                    "release": {
                        "id": "036ce98e-0397-4ddb-97ff-e6784fdd21a4",
                        "title": "Live in Paris"
                    },
                    "position": 1,
                    "track_count": 10,
                    "format": "CD"
                },
                "artists": [{
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Sail On"
            }],
            "id": "0083c88e-1ca2-484e-b1ff-d384358942bb"
        }, {
            "duration": 268,
            "tracks": [{
                "duration": 268,
                "position": 12,
                "medium": {
                    "release": {
                        "id": "9c0a98e8-da23-49cf-a6ae-df27340deebb",
                        "title": "Love Songs"
                    },
                    "position": 1,
                    "track_count": 16,
                    "format": "CD"
                },
                "artists": [{
                    "id": "60d41417-feda-4734-bbbf-7dcc30e08a83",
                    "name": "Diana Ross"
                }, {
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Endless Love"
            }],
            "id": "07be8b05-a1cb-4e88-8217-2ef5e0dafcb3"
        }, {
            "duration": 267,
            "tracks": [{
                "duration": 267,
                "position": 2,
                "medium": {
                    "release": {
                        "id": "a4c1dce4-0e16-483e-95de-aabff4378711",
                        "title": "Top 40 Hitdossier 1981-1982"
                    },
                    "position": 1,
                    "track_count": 20
                },
                "artists": [{
                    "id": "60d41417-feda-4734-bbbf-7dcc30e08a83",
                    "name": "Diana Ross"
                }, {
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Endless Love"
            }],
            "id": "1fb13442-b0e7-4891-97de-255ba7e8730f"
        }, {
            "duration": 263,
            "tracks": [{
                "duration": 263,
                "position": 16,
                "medium": {
                    "release": {
                        "id": "add4c09f-b9f3-4e3f-b36b-22f30309ce2b",
                        "title": "Steve Wright's Sunday Love Songs, Volume 2"
                    },
                    "position": 1,
                    "track_count": 22,
                    "format": "CD"
                },
                "artists": [{
                    "id": "60d41417-feda-4734-bbbf-7dcc30e08a83",
                    "name": "Diana Ross"
                }, {
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Endless Love"
            }],
            "id": "20df4da3-005e-4bde-9337-46925e709110"
        }, {
            "duration": 264,
            "tracks": [{
                "duration": 264,
                "position": 2,
                "medium": {
                    "release": {
                        "id": "d23d377b-b69a-424b-856c-b78dfde11feb",
                        "title": "Always And Forever (disc 1)"
                    },
                    "position": 1,
                    "track_count": 20,
                    "format": "CD"
                },
                "artists": [{
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Endless Love"
            }],
            "id": "2cf76055-5a41-4a39-82ba-611b7142ab91"
        }, {
            "duration": 265,
            "tracks": [{
                "duration": 265,
                "position": 9,
                "medium": {
                    "release": {
                        "id": "3bc3d08b-7e50-47d0-951d-de95a10e1ee8",
                        "title": "The Love Album II"
                    },
                    "position": 1,
                    "track_count": 20,
                    "format": "CD"
                },
                "artists": [{
                    "id": "60d41417-feda-4734-bbbf-7dcc30e08a83",
                    "name": "Diana Ross"
                }, {
                    "id": "3cb25fb2-5547-4b05-adec-1a5e37830d46",
                    "name": "Lionel Richie"
                }],
                "title": "Endless Love"
            }],
            "id": "33f02920-5832-490d-bf5f-3cce793925bf"
        }....too big to display here..

Please dont mind me pasting the output here..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:26:24+00:00Added an answer on June 17, 2026 at 1:26 pm

    From python’s documentation,

    The break statement, like in C, breaks out of the smallest enclosing
    for or while loop.

    In your case

    for track in recording['tracks']:
    

    Try putting your code in a function and use return:

    def find_it(json, duration):
      for result in json['results']:
          for recording in result['recordings']:
              for track in recording['tracks']:
                  track_artist = ''
                  for artist in track['artists']:
                      try:
                          track_artist += repr(artist['name']) + ' '
                      except:
                          track_artist = ''
                  #print str(track['duration']) + '-' + track['medium']['release']['title'] + '-' + track['title'] + '-' + track_artist
                  if str(track['duration']) == str(duration):
                    return track, track_artist
      return None, None
    
    track, track_artist = find_it(json, duration)
    print 'Matched:' + str(track['duration']) + '-' + track['medium']['release']['title'] + '-' + track['title'] + '-' + track_artist
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying use facebook API to upload photo in my fan page. I downloaded
I'm trying use Places API from Google frmo a node.js server This is the
I'm trying use Google APIs Client Library for Python to rename drive file, here
I'm trying use epoch time dates in my series data. The array looks like
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Hi I'm trying use a datepicker on a field I have. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.