import random
import requests


def get_rising_submissions(subreddit):
    """Connects to the Reddit API and queries the top rising submission
    from the specified subreddit.
    
    Parameters
    ----------
    subreddit : str
        The name of the subreddit without forward slashes.

    Returns
    -------
    tuple
        A tuple containing a formatted message and an image url.
    
    """

    endpoint = random.choice(['top','rising'])

    url = f"https://www.reddit.com/r/{subreddit}/{endpoint}.json?limit=1"
    headers = {"User-Agent": "Reddit Rising Checker v1.0"}

    with requests.get(url, headers=headers) as response:

        data = response.json()["data"]["children"]

        # Iterate over all the children.
        for item in data:

            item_data = item["data"]

            # We will collect only the fields we are interested in.
            title = item_data["title"]
            permalink = "https://reddit.com" + item_data["permalink"]
            author = item_data["author"]
            score = item_data["score"]
            image_url = item_data["url"]

            # Compose a Markdown message using string formatting.
            message = f"[{title}]({permalink})\nby **{author}**\n**{score:,}** points"

            return (message, image_url)