Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot search for music #1746

Closed
mi1acl opened this issue Jan 13, 2024 · 3 comments
Closed

[BUG] Cannot search for music #1746

mi1acl opened this issue Jan 13, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@mi1acl
Copy link
Contributor

mi1acl commented Jan 13, 2024

Describe the bug
When searching for music, throws pydantic's ValidationError here is the full explanation from copilot

The error message indicates that there's a validation error for the Track object in the instagrapi package. The territory_validity_periods field is missing, which is required for the Track object.

This could be due to the API response not containing this field or the field being named differently. You might want to check the data you're receiving from the cl.search_music('Friday') call.

If you don't have control over the API response, you might need to handle this in your code by checking if the field exists before trying to access it. If the field doesn't exist, you can provide a default value or handle the error appropriately.

If you're the maintainer of the instagrapi package or the API, you might need to update the Track model or the API to ensure the territory_validity_periods field is included in the response.

To Reproduce

from instagrapi import Client
from dotenv import load_dotenv
import os

load_dotenv()

username = os.environ.get('USERNAME')
password = os.environ.get('PASSWORD')

cl = Client()
cl.login(username, password)

music = cl.search_music('Friday')
print(music)

Traceback

Traceback (most recent call last):
  File "publish_utils.py", line 13, in <module>
    music = cl.search_music('Friday')
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/site-packages/instagrapi/mixins/fbsearch.py", line 57, in search_music
    return [extract_track(item["track"]) for item in result["items"]]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/site-packages/instagrapi/mixins/fbsearch.py", line 57, in <listcomp>
    return [extract_track(item["track"]) for item in result["items"]]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/site-packages/instagrapi/extractors.py", line 472, in extract_track
    return Track(**data)
           ^^^^^^^^^^^^^
  File "lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Track
territory_validity_periods
  Field required [type=missing, input_value={'audio_cluster_id': '752...y1Okfhg6UA&oe=65A8167A'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.5/v/missing

Expected behavior
Return a valid value complying with Track model

Desktop (please complete the following information):

  • Python 3.11.6
  • instagrapi==2.0.1
  • moviepy==1.0.3
  • imagemagick 7.1.1-26
@mi1acl mi1acl added the bug Something isn't working label Jan 13, 2024
@mi1acl mi1acl changed the title [BUG] [BUG] Cannot search for music Jan 13, 2024
@mi1acl
Copy link
Contributor Author

mi1acl commented Jan 13, 2024

I solved it by adding this line to /instagrapi/extractors.py

    data["territory_validity_periods"] = data.get("territory_validity_periods") or {}

here is the full extract_track() function

def extract_track(data):
    data["cover_artwork_uri"] = data.get("cover_artwork_uri") or None
    data["cover_artwork_thumbnail_uri"] = (
        data.get("cover_artwork_thumbnail_uri") or None
    )
    items = re.findall(r"<BaseURL>(.+?)</BaseURL>", data["dash_manifest"])
    data["uri"] = html.unescape(items[0]) if items else None
    data["territory_validity_periods"] = data.get("territory_validity_periods") or {}
    return Track(**data)

I can open a PR for this if needed.

@ggrelet
Copy link

ggrelet commented Feb 9, 2024

@mi1acl please do, this fix is indeed very useful!

@mi1acl
Copy link
Contributor Author

mi1acl commented Feb 13, 2024

@ggrelet created PR #1801 for this issue

subzeroid added a commit that referenced this issue Feb 27, 2024
fix issue #1746: Cannot search for music
@mi1acl mi1acl closed this as completed Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants