import json
import requests


def find_most_recent_tag_dockerhub(name, url):
    """Find most recent tag of an image from Docker Hub

    Arguments:
        name {str} -- Name of the image
        url {str} -- API URL of the Docker Hub image repository

    Returns:
        new_tag {str} -- Most recent image tag from Docker Hub
    """
    res = json.loads(requests.get(url).text)

    updates_sorted = sorted(res["results"], key=lambda k: k["last_updated"])

    if updates_sorted[-1]["name"] == "latest":
        new_tag = updates_sorted[-2]["name"]
    else:
        new_tag = updates_sorted[-1]["name"]

    return new_tag