~nova/fletcher

af9e3b2572a98c1a99f364989e2fe7c13078f121 — Novalinium a month ago 357206b
Working pinterest version
1 files changed, 23 insertions(+), 6 deletions(-)

M pinterest.py
M pinterest.py => pinterest.py +23 -6
@@ 15,6 15,7 @@ board_cache = {}


async def pinterest_randomize_function(message, client, args):
    global board_cache
    username = args[0]
    boardname = " ".join(args[1:])
    cachekey = f"u:{username},b:{boardname}"


@@ 23,27 24,38 @@ async def pinterest_randomize_function(message, client, args):
        board_cache[cachekey] = get_board(username, boardname)
        random.shuffle(board_cache[cachekey])
    board_entry = board_cache[cachekey].pop()
    logger.debug(board_entry)
    title = board_entry.get("grid_title", "")
    attribution = board_entry.get("attribution", {}) or {}
    author = attribution.get("author_name", "")
    url = board_entry.get("link", "")
    images = board_entry.get("images", {}) or {}
    orig = images.get("orig", {}) or {}
    orig_url = orig.get("url", "")
    embedPreview = discord.Embed(
        title=board_entry["grid_title"],
        description=board_entry.get("attribution", dict()).get("author_name", ""),
        url=board_entry.get("link", "")
        title=title,
        description=author,
        url=url,
    )
    embedPreview.set_footer(
        icon_url="http://download.nova.anticlack.com/fletcher/pinterest.png",
        text=f"On behalf of {message.author.display_name}",
    )
    embedPreview.set_thumbnail(url=board_entry["images"]["orig"]["url"])
    embedPreview.set_image(url=orig_url)
    await message.channel.send(embed=embedPreview)


@cached(TTLCache(1024, 600))
def get_board(username, boardname):
def get_boards(username):
    boards = []
    board_batch = pinterest.boards(username=username)
    while len(board_batch) > 0:
        boards += board_batch
        board_batch = pinterest.boards(username=username)
    board_id = discord.utils.find(lambda b: b["name"] == boardname, boards)["id"]
    return boards

@cached(TTLCache(1024, 600))
def get_feeds(board_id):
    board_feed = []
    feed_batch = pinterest.board_feed(board_id=board_id)
    while len(feed_batch) > 0:


@@ 51,6 63,11 @@ def get_board(username, boardname):
        feed_batch = pinterest.board_feed(board_id=board_id)
    return board_feed

@cached(TTLCache(1024, 600))
def get_board(username, boardname):
    board_id = discord.utils.find(lambda b: b["name"] == boardname, get_boards(username))["id"]
    return get_feeds(board_id)


def autoload(ch):
    global config