Repository: wagtail/wagtail
File: wagtail/embeds/embeds.py (Python)

Code before:

from django.utils.timezone import now

from wagtail.coreutils import accepts_kwarg, safe_md5

from .exceptions import EmbedUnsupportedProviderException
from .finders import get_finders
from .models import Embed


def get_finder_for_embed(url, max_width=None, max_height=None):
    for finder in get_finders():
        if finder.accept(url):
            kwargs = {}
            if accepts_kwarg(finder.find_embed, "max_height"):
                kwargs["max_height"] = max_height
            return finder.find_embed(url, max_width=max_width, **kwargs)

    raise EmbedUnsupportedProviderException


def get_embed(url, max_width=None, max_height=None):
    """
    Retrieve an embed for the given URL using the configured finders.

    Note: The 'finder' parameter has been removed (see GitHub issue #7287).
    """
    embed_hash = get_embed_hash(url, max_width, max_height)

    # Check database
    try:
        return Embed.objects.exclude(cache_until__lte=now()).get(hash=embed_hash)
    except Embed.DoesNotExist:
        pass

    embed_dict = get_finder_for_embed(url, max_width, max_height)

    # Make sure width and height are valid integers before inserting into database
    try:
        embed_dict["width"] = int(embed_dict["width"])
    except (TypeError, ValueError):
        embed_dict["width"] = None

    try:
        embed_dict["height"] = int(embed_dict["height"])
    except (TypeError, ValueError):
        embed_dict["height"] = None

    # Make sure html field is valid
    if "html" not in embed_dict or not embed_dict["html"]:
        embed_dict["html"] = ""

Review comment (refactor):
```suggestion
```

No need to add this reference, we can see the commits in the history if needed, especially as this is just an 'internal' clean up.

Code after:

from django.utils.timezone import now

from wagtail.coreutils import accepts_kwarg, safe_md5

from .exceptions import EmbedUnsupportedProviderException
from .finders import get_finders
from .models import Embed


def get_finder_for_embed(url, max_width=None, max_height=None):
    for finder in get_finders():
        if finder.accept(url):
            kwargs = {}
            if accepts_kwarg(finder.find_embed, "max_height"):
                kwargs["max_height"] = max_height
            return finder.find_embed(url, max_width=max_width, **kwargs)

    raise EmbedUnsupportedProviderException


def get_embed(url, max_width=None, max_height=None):
    """
    Retrieve an embed for the given URL using the configured finders.
    """
    embed_hash = get_embed_hash(url, max_width, max_height)

    # Check database
    try:
        return Embed.objects.exclude(cache_until__lte=now()).get(hash=embed_hash)
    except Embed.DoesNotExist:
        pass

    embed_dict = get_finder_for_embed(url, max_width, max_height)

    # Make sure width and height are valid integers before inserting into database
    try:
        embed_dict["width"] = int(embed_dict["width"])
    except (TypeError, ValueError):
        embed_dict["width"] = None

    try:
        embed_dict["height"] = int(embed_dict["height"])
    except (TypeError, ValueError):
        embed_dict["height"] = None

    # Make sure html field is valid
    if "html" not in embed_dict or not embed_dict["html"]:
        embed_dict["html"] = ""

    # If the finder does not return an thumbnail_url, convert null to '' before inserting into the db