Skip to content

Commit

Permalink
Initial code import
Browse files Browse the repository at this point in the history
  • Loading branch information
DoomHammer committed Oct 30, 2023
1 parent ba09ed0 commit 0ecbad9
Show file tree
Hide file tree
Showing 11 changed files with 1,218 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotenv
use_nix
45 changes: 45 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Create and publish a Docker image
on:
push:
branches: ['main']
pull_request:
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Setup QEMU
id: qemu
uses: docker/[email protected]
with:
platforms: linux/amd64,linux/arm/v7
- name: Set up Docker Buildx
id: buildx
uses: docker/[email protected]
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
platforms: linux/amd64,linux/arm/v7
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Check PR
on:
workflow_dispatch:
pull_request:
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- run: |
wget -O /usr/local/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64
chmod +x /usr/local/bin/hadolint
- uses: actions/[email protected]
- uses: actions/[email protected]
- uses: cloudposse/[email protected]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
54 changes: 54 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-merge-conflict
- id: check-ast
- id: debug-statements
- id: check-toml
- id: check-json
- id: check-xml
- repo: https://github.com/google/yamlfmt
rev: v0.7.1
hooks:
- id: yamlfmt
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args: ["--max-line-length=130"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.800
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
hooks:
- id: bandit
- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
hooks:
- id: hadolint
- repo: https://github.com/python-poetry/poetry
rev: 1.2.2 # This should match the version from default.nix
hooks:
- id: poetry-check
- id: poetry-lock
args: ["--no-update"]
- id: poetry-export
args: ["-f", "requirements.txt", "-o", "requirements.txt"]
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ARG PYTHON_VERSION=3.10.9

FROM python:$PYTHON_VERSION-slim

ENV \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential=12.9 \
curl=7.74.0-1.3+deb11u7 \
gcc=4:10.2.1-1 \
&& rm -rf /var/lib/apt/lists/*

RUN useradd --create-home appuser
USER appuser

WORKDIR /home/appuser/bot

ENV PYTHONPATH=/home/appuser

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY bot.py .
CMD ["python", "bot.py"]
77 changes: 77 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json
import logging
import os

import discord
from pytz import timezone

discord_token = os.getenv("DISCORD_TOKEN")

# Logging configuration
logging.basicConfig(level=logging.INFO)

# Discord configuration
intents = discord.Intents.default()
client = discord.Client(intents=intents)

local_timezone = timezone("Europe/Warsaw")


@client.event
async def on_ready():
guilds_total = len(client.guilds)
guilds_processed = 0
for guild in client.guilds:
guilds_processed += 1
logging.info(f"Scraping {guild}")
logging.info(f"{client.user} has connected to Discord server {guild}!")
for event in guild.scheduled_events:
full_event = await guild.fetch_scheduled_event(event.id)
event = full_event
start_time = event.start_time.astimezone(local_timezone)
end_time = event.end_time.astimezone(local_timezone)
logging.info(event)
logging.info(event.creator)
logging.info(event.creator_id)
logging.info(event.description)
logging.info(start_time)
date = start_time.strftime("%Y-%m-%d")
directory = start_time.strftime("%Y/%m/%d")
start_time = start_time.strftime("%H:%M")
end_time = end_time.strftime("%H:%M")
filename = f"{date}-{event.name.replace('/', '').replace(':', '')}.md"
fields = f"""---
title: {json.dumps(event.name)}
tags: ["hs3"]
outputs:
- html
- calendar
discord_event:
id: {event.id}
link: {event.url}
interested: {event.user_count}
organizer: {event.creator}
location: {event.location}
featureImage: {event.cover_image}
eventInfo:
dates:
extra:
{date} {start_time}-{end_time}: null
---
{event.description}
"""
print(fields)
try:
os.makedirs(directory)
except FileExistsError:
pass
with open(f"{directory}/{filename}", "w") as f:
f.write(fields)
logging.info(
f"Guilds processed: {guilds_processed}, guilds total: {guilds_total}"
)
if guilds_processed == guilds_total:
await client.close()


client.run(discord_token)
21 changes: 21 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ pkgs ? import (
builtins.fetchTarball {
url = "https://github.com/nixos/nixpkgs/archive/22.11.tar.gz";
sha256 = "11w3wn2yjhaa5pv20gbfbirvjq6i3m7pqrq2msf0g7cv44vijwgw";
}
) {} }:

with pkgs;

mkShell {
buildInputs = [
hadolint
(python310.withPackages (ps: with ps; [
poetry
]))
pre-commit
];
shellHook = ''
pre-commit install
'';
}
Loading

0 comments on commit 0ecbad9

Please sign in to comment.