def test_get_picture_post_no_pic_found():
    """ Tests get_picture_post() with no picture found in subreddit"""

    subreddit = bot.get_subreddit(reddit=reddit, name="Python")
    with pytest.raises(bot.BotException,
                       match="No picture posts in the defined limit"):
        bot.get_picture_post(subreddit_sort=subreddit.hot(limit=0))
def test_get_picture_post():
    """ Tests get_picture_post() to return picture submission """

    subreddit = bot.get_subreddit(reddit=reddit, name="wallpapers")
    picture = bot.get_picture_post(subreddit_sort=subreddit.hot())

    assert picture in subreddit.hot()
Beispiel #3
0
def main(cmd_args):
    logger.debug("Start of Program.")
    print("""
+---------------------------+
| W A L L P A P E R   B.O.T |
+---------------------------+
    """)

    # Create reddit object
    login_config = config["Login"]
    reddit = bot.get_reddit(login=login_config)

    # Checking if command line arguments exist
    if len(cmd_args) != 0:
        subreddit_name, prompt = parse_bot_arguments(args=cmd_args)
    else:
        subreddit_name = input("Enter the subreddit you want to access: ")
        prompt = None

    subreddit = bot.get_subreddit(reddit=reddit, name=subreddit_name)

    # Get picture submission based on 'Hot' sort
    picture = bot.get_picture_post(subreddit_sort=subreddit.hot())

    print(f"Title: {picture.title}")
    print(f"Author: {picture.author.name}\n")

    print("Downloading...")
    try:
        r = requests.get(url=picture.url)
        r.raise_for_status()
        logger.info("Downloading Picture")
        logger.debug(f"Getting request from picture {r}")
    except requests.RequestException as e:
        print(
            "Error: Connection broken; couldn't download image.",
            "Please try again."
        )
        logger.exception(f"{e}")
        logger.critical("Couldn't download image")
        sys.exit()
    print("Done.\n")

    # Getting image extension from url
    image_ext = pathlib.Path(picture.url).suffix

    # Getting picture path
    image_filename = picture.author.name + image_ext
    image_path = pathlib.Path(".").resolve() / "images" / image_filename

    bot.save_image(path=image_path, image=r.content)
    bot.set_image_background(image_path=str(image_path), prompt=prompt)

    print("Exiting..\n")
    logger.debug("End of program")
def test_get_picture_post_invalid_login():
    """ Tests get_picture_post() with invalid login reddit """

    false_login = {
        "client_id": "1",
        "client_secret": "2",
        "user_agent": "3",
    }
    reddit = bot.get_reddit(login=false_login)
    # :D
    subreddit = bot.get_subreddit(reddit=reddit, name="anime")
    with pytest.raises(bot.BotException,
                       match="Invalid client_ID or client_secret"):
        bot.get_picture_post(subreddit_sort=subreddit.hot())
def test_get_picture_post_invalid_subreddit():
    """ Tests get_picture_post() with invalid subreddit """

    subreddit = bot.get_subreddit(reddit=reddit, name="1231ijdjabshdn")
    with pytest.raises(bot.BotException, match="Couldn't access subreddit"):
        bot.get_picture_post(subreddit_sort=subreddit.hot())
def test_get_subreddit():
    """ Tests get_subreddit() to return subreddit object """

    assert bot.get_subreddit(reddit=reddit, name="Test") == reddit.subreddit(
        "Test"), "Didn't return subreddit object"
def test_get_subredit_empty_name():
    """ Tests get_subreddit() with empty name given """

    with pytest.raises(bot.BotException,
                       match="Couldn't create Subreddit Object") as exp:
        bot.get_subreddit(reddit=reddit, name="")