From 3f9eea6a512cec9d282ffaf2bac532cde43e279a Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Mon, 29 May 2023 13:02:04 -0500 Subject: [PATCH] merged production and dev containers --- .devcontainer/Dockerfile | 5 ++++- .devcontainer/devcontainer.json | 22 ++++++++++++++-------- .gitignore | 1 + docker-compose.yml | 14 ++++++++++---- src/kwaylon/kwaylon.py | 13 ++++++------- src/main.py | 28 +++++++++++++++++++++++++++- 6 files changed, 62 insertions(+), 21 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 13e998e..d347051 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -5,10 +5,13 @@ libblas-dev liblapack-dev \ libffi-dev libnacl-dev \ python3 python3-pip python3-dev +RUN mkdir -p /kwaylon/src +WORKDIR /kwaylon COPY ./requirements.txt ./requirements.txt RUN --mount=type=cache,mode=0755,target=/root/.cache/pip pip install -r ./requirements.txt RUN python3 -m nltk.downloader words punkt averaged_perceptron_tagger WORKDIR /usr/app -CMD [ "./src/main.py" ] +COPY ./src /kwaylon/src +CMD [ "/kwaylon/src/main.py" ] diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 51eb22a..b0dad84 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,13 +1,19 @@ { "name": "Kwaylon", - "build": { - "dockerfile": "./Dockerfile", - "context": "../" - }, - "mounts": [ - "source=/etc/localtime,target=/etc/localtime,type=bind,consistency=cached", - "source=/etc/timezone,target=/etc/timezone,type=bind,consistency=cached" - ], + "dockerComposeFile": "../docker-compose.yml", + "service": "kwaylon", + "workspaceFolder": "/kwaylon", + "shutdownAction": "stopCompose", + // "build": { + // "dockerfile": "./Dockerfile", + // "context": "../" + // }, + // "mounts": [ + // "source=/etc/localtime,target=/etc/localtime,type=bind", + // "source=/etc/timezone,target=/etc/timezone,type=bind" + // ], + // "workspaceMount": "source=${localWorkspaceFolder}/src,target=/usr/app/src,type=bind", + // "workspaceFolder": "/usr/app/src", "features": { "ghcr.io/devcontainers/features/git:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {}, diff --git a/.gitignore b/.gitignore index 0be4cf9..a54fa06 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ src/.env .ipynb_checkpoints discord.py *.db +pics \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d96e4b9..df214e4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,20 @@ -version: '3.8' +version: '3.3' services: kwaylon: container_name: kwaylon image: kwaylon:latest - build: ./ + build: + context: ./ + dockerfile: .devcontainer/Dockerfile # restart: unless-stopped network_mode: host + tty: true + stdin_open: true volumes: # give access to the system time as read-only - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro - - ./src:/usr/app/src - - ./data:/usr/app/data + - ./:/kwaylon + # - ./src:/usr/app/src + # - ./data:/usr/app/data + # - ./pics:/discord_pics diff --git a/src/kwaylon/kwaylon.py b/src/kwaylon/kwaylon.py index aa8b6f5..d1376d2 100644 --- a/src/kwaylon/kwaylon.py +++ b/src/kwaylon/kwaylon.py @@ -2,7 +2,7 @@ import asyncio import logging import re from pathlib import Path -from typing import List +from typing import AsyncIterator, List import pandas as pd from discord import (Client, Emoji, Guild, Message, RawReactionActionEvent, @@ -38,8 +38,6 @@ class Kwaylon(Client): # await alive(self.robotics_facility) _log.info('[bold bright_green]Done[/][bright_black], laying in wait...[/]') - await self.msg_gen(limit=10) - async def handle_message(self, message: Message): if message.author != self.user: await self.respond_to_joke(message) @@ -134,14 +132,15 @@ class Kwaylon(Client): channel = await guild.fetch_channel(row['channel_id']) return await channel.fetch_message(row['msg_id']) - async def msg_gen(self, **kwargs): - _log.debug(f'Starting scan of {self.fd_guild.name}') + async def msg_gen(self, **kwargs) -> AsyncIterator[Message]: + # _log.debug(f'Starting scan of {self.fd_guild.name}') for channel in (await self.fd_guild.fetch_channels()): if isinstance(channel, TextChannel): - _log.debug(f'[bold green]{channel.name}[/]') + # _log.debug(f'[bold green]{channel.name}[/]') msg: Message async for msg in channel.history(**kwargs): - _log.debug(f'[bold blue]{msg.author.display_name}[/] {msg.content[:200]}') + yield msg + # _log.debug(f'[bold blue]{msg.author.display_name}[/] {msg.content[:200]}') def log_info(): diff --git a/src/main.py b/src/main.py index 4c950f6..905c618 100644 --- a/src/main.py +++ b/src/main.py @@ -2,20 +2,44 @@ import logging import os +from datetime import datetime +from pathlib import Path from discord import Intents, Message from dotenv import load_dotenv +from rich.console import Console from rich.highlighter import NullHighlighter from rich.logging import RichHandler from kwaylon import Kwaylon +TZ = datetime.now().astimezone().tzinfo + +async def save_pictures(client: Kwaylon, dest: Path): + kwargs = dict( + limit=10, + after=datetime(year=2023, month=1, day=1, tzinfo=TZ), + oldest_first=True + ) + async for msg in client.msg_gen(**kwargs): + if len(msg.attachments) > 0: + for attachment in msg.attachments: + if attachment.content_type == 'image/jpeg': + filepath = Path(f'/kwaylon/pics/{msg.author.display_name.replace(".", "")}_{msg.created_at.strftime("%Y%m%d_%H%M%S.jpeg")}') + if not filepath.exists(): + logging.debug(f'[bold blue]{msg.author.display_name}[/] {msg.clean_content[:200]}') + logging.debug(f'Saving {filepath.name}') + await attachment.save(filepath) + else: + logging.debug(f'Skipping {filepath.name}') + if __name__ == '__main__': rich_handler = RichHandler( + console=Console(width=150), highlighter=NullHighlighter(), markup=True, rich_tracebacks=True, - tracebacks_suppress=['pandas'], + tracebacks_suppress=['pandas', 'discord'], ) dt_fmt = '%Y-%m-%d %I:%M:%S %p' # https://docs.python.org/3/library/logging.html#logrecord-attributes @@ -36,6 +60,8 @@ if __name__ == '__main__': @client.event async def on_ready(): await client.initialize() + # https://discordpy.readthedocs.io/en/stable/api.html#discord.TextChannel.history + # await save_pictures(client, Path('/kwaylon/pics')) @client.event async def on_message(message: Message):