def test_convert_legacy_history(testdir: str, legacyhist: str,
                                hoistedhist: str, log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                 history_filename=legacyhist,
                                 log_filename=log)
    bs.load_history()

    assert bs.history != []

    mbs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                  history_filename=hoistedhist,
                                  log_filename=log)
    mbs.load_history()

    assert len(bs.history) == len(mbs.history)
    for i, elem in enumerate(bs.history):
        melem = mbs.history[i]
        assert elem == melem
def test_load_existing_modern_history(testdir: str, testhist: str,
                                      log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                 history_filename=testhist,
                                 log_filename=log)
    bs.load_history()

    assert bs.history != []
    assert len(bs.history) == 2
def test_birdsite_activates_with_owner_id_correctly(testdir: str,
                                                    credentials: str,
                                                    log: str) -> None:
    # no owner set
    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    assert (bs.outputs["birdsite"]["active"])
    birdsite_obj: Any = bs.outputs["birdsite"]["obj"]
    assert (birdsite_obj.owner_handle == "")

    TESTSTR = "foo"
    TESTFILE = os.path.join(credentials, "OWNER_HANDLE")
    with open(TESTFILE, "w") as f:
        f.write(TESTSTR)

    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    assert (bs.outputs["birdsite"]["active"])
    birdsite_obj = bs.outputs["birdsite"]["obj"]
    assert (birdsite_obj.owner_handle == TESTSTR)

    os.remove(TESTFILE)
Exemple #4
0
def test_mastodon_activates_with_instance_id_correctly(testdir: str,
                                                       credentials: str,
                                                       log: str) -> None:
    # no instance set
    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    assert (bs.outputs["mastodon"]["active"])
    mastodon_obj: Any = bs.outputs["mastodon"]["obj"]
    assert (mastodon_obj.instance_base_url == "https://mastodon.social")

    TESTSTR = "https://butts.butts"
    TESTFILE = os.path.join(credentials, "INSTANCE_BASE_URL")
    with open(TESTFILE, "w") as f:
        f.write(TESTSTR)

    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    assert (bs.outputs["mastodon"]["active"])
    mastodon_obj = bs.outputs["mastodon"]["obj"]
    assert (mastodon_obj.instance_base_url == TESTSTR)

    os.remove(TESTFILE)
def test_repair_corrupted_history(testdir: str, corruptedhist: str,
                                  repairedcorruptedhist: str,
                                  log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                 history_filename=corruptedhist,
                                 log_filename=log)
    bs.load_history()

    assert bs.history != []

    # compare against repairedhist. they should be identical.
    mbs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                  history_filename=repairedcorruptedhist,
                                  log_filename=log)
    mbs.load_history()

    assert mbs.history != []

    assert len(bs.history) == len(mbs.history)
    for i, elem in enumerate(bs.history):
        melem = mbs.history[i]
        for key, item in elem.__dict__.items():
            assert str(item) == str(melem.__dict__[key])
def test_load_null_history(testdir: str, log: str) -> None:
    name = "foobot"
    bs = botskeleton.BotSkeleton(bot_name=name,
                                 secrets_dir=testdir,
                                 log_filename=log)

    assert bs.history == []

    open(bs.history_filename, "w").close()
    bs.load_history()
    assert bs.history == []

    os.remove(bs.history_filename)
    os.remove(f"{bs.history_filename}.bak")
def test_idempotency(testdir: str, testhist: str, log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir,
                                 history_filename=testhist,
                                 log_filename=log)
    bs.load_history()

    # maybe find a less hacky way to do this.
    for entry in bs.history:
        entry._version = "9999"
    bs.update_history()

    identical = True
    hist_source = os.path.join(JSON, "test_entries.json")
    with open(testhist, "r") as f1, open(hist_source, "r") as f2:
        for line1, line2 in zip(f1, f2):
            if line1 != line2:
                identical = False
                break

    if not identical:
        pytest.fail("Test history changed when it shouldn't have been.")
Exemple #8
0
"""Main class for bot."""

import math
import os

import botskeleton

import gb_query

# Delay between tweets in seconds.
DELAY = math.ceil(3600 * 3.5)

if __name__ == "__main__":
    SECRETS_DIR = os.path.join(gb_query.HERE, "SECRETS")
    BOT_SKELETON = botskeleton.BotSkeleton(SECRETS_DIR,
                                           bot_name="goties_bot",
                                           delay=DELAY)
    LOG = BOT_SKELETON.log

    while True:
        LOG.info("Determining goties.")
        res = gb_query.get_goties()
        year = res["year"]

        LOG.info(f"Sending out goties for {year}.")
        TWEET = f"The Games of the Year for {year} are:"

        BOT_SKELETON.send_with_many_media(
            TWEET,
            gb_query.TOP_THREE_FILENAMES[0],
            gb_query.TOP_THREE_FILENAMES[1],
def test_birdsite_activates_correctly(testdir: str, credentials: str,
                                      log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    assert (bs.outputs["birdsite"]["active"])
Exemple #10
0
import botskeleton
from PIL import Image

import album_art_gen

# Delay between tweets in seconds.
DELAY = 3600

ALBUM_ART_FILENAME = album_art_gen.ALBUM_ART_FILENAME
MAX_IMAGE_SIZE_BYTES = 3072 * 1024

if __name__ == "__main__":
    SECRETS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                               "SECRETS")
    BOT_SKELETON = botskeleton.BotSkeleton(SECRETS_DIR,
                                           bot_name="isthisska_bot",
                                           delay=DELAY)
    LOG = BOT_SKELETON.log

    while True:
        LOG.info("Grabbing random album art from Musicbrainz.")
        try:
            info = album_art_gen.produce_random_album_art()

        except album_art_gen.APIException as e:
            LOG.error("Encountered an API Exception.")
            LOG.error(f"Code: {e.code} Message: {e.message}")

            if e.code == 503:
                LOG.error(
                    "API error or rate limiting - waiting at least a few minutes."
def test_outputs_start_inactive(testdir: str, log: str) -> None:
    bs = botskeleton.BotSkeleton(secrets_dir=testdir, log_filename=log)

    # it would be funny to make this a mapped lambda
    for _, output in bs.outputs.items():
        assert not output["active"]
def test_no_secrets_dir_fails(log: str) -> None:
    try:
        bs = botskeleton.BotSkeleton(log_filename=log)
        pytest.fail("Should throw exception for no secrets dir.")
    except botskeleton.BotSkeletonException as e:
        pass
Exemple #13
0
#!/usr/bin/env python3
"""Main class for bot."""

import os.path as path

import botskeleton

import gen

# Delay between tweets in seconds.
DELAY = 7200

if __name__ == "__main__":
    SECRETS_DIR = path.join(path.abspath(path.dirname(__file__)), "SECRETS")
    BOT_SKELETON = botskeleton.BotSkeleton(SECRETS_DIR,
                                           bot_name="dirtyunix_bot",
                                           delay=DELAY)

    while True:
        garbage = gen.gen()

        BOT_SKELETON.log.info(f"Tweeting: {garbage}.")
        BOT_SKELETON.send(garbage)

        BOT_SKELETON.nap()