~nova/fletcher

fletcher/pinterest.py -rw-r--r-- 4.0 KiB
ece10afa — Novalinium f-string issue 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import aiohttp
import copy
import datetime
import discord
from py3pin.Pinterest import Pinterest
import io
import logging
import messagefuncs
import random
from sys import exc_info
import ujson
from cachetools import cached, TTLCache

logger = logging.getLogger("fletcher")

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}"
    try:
        board_entry = board_cache[cachekey].pop()
    except (IndexError, KeyError):
        await message.channel.trigger_typing()
        board_cache[cachekey] = copy.deepcopy(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=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_image(url=orig_url)
    await messagefuncs.sendWrappedMessage(
        None, target=message.channel, embed=embedPreview
    )


@cached(TTLCache(1024, 600))
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)
    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:
        board_feed += feed_batch
        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
    global pinterest
    ch.add_command(
        {
            "trigger": ["!debug_prt"],
            "function": lambda message, client, args: get_boards(args[0]),
            "async": False,
            "admin": False,
            "hidden": True,
            "args_num": 1,
            "args_name": ["username"],
            "description": "Return a random image from the board specified",
        }
    )
    ch.add_command(
        {
            "trigger": ["!prt"],
            "function": pinterest_randomize_function,
            "async": True,
            "admin": False,
            "hidden": True,
            "args_num": 2,
            "args_name": ["username", "board"],
            "description": "Return a random image from the board specified",
        }
    )
    ch.add_command(
        {
            "trigger": ["!possum"],
            "function": lambda message, client, args: pinterest_randomize_function(
                message, client, ["jerryob1", "Opossums"]
            ),
            "async": True,
            "admin": False,
            "hidden": False,
            "args_num": 0,
            "args_name": ["username", "board"],
            "description": "Return a random image from the board specified",
        }
    )
    # for guild in ch.guilds:
    #     for ch.config.get(guild=guild, section="pinterest"):
    pinterest = Pinterest(
        email=ch.config.get(section="pinterest", key="email"),
        password=ch.config.get(section="pinterest", key="password"),
        username=ch.config.get(section="pinterest", key="username"),
        cred_root=ch.config.get(section="pinterest", key="tmpdir"),
    )
    if datetime.datetime.now() == 8:
        pinterest.login()


async def autounload(ch):
    pass