Skip to content

Commit

Permalink
initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
montaguethomas committed Jun 10, 2024
0 parents commit 2bd690d
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dockerignore
Dockerfile
101 changes: 101 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
schedule:
- cron: '42 17 * * *'
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0
with:
cosign-release: 'v2.2.4'

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
/storage
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3-alpine
RUN apk upgrade --no-cache
WORKDIR /app
COPY requirements.txt /app/
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY . /app/
VOLUME /app/storage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Ash Wilson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Slackmojis

Downloads emojis from [slackmojis.com](https://slackmojis.com) and generates "packs" by
category. Allows uploading of emojis to Slack as a normal Slack user with permissions
to add emojis.

## Usage



## References

- https://github.com/lambtron/emojipacks
- https://github.com/smashwilson/slack-emojinator
164 changes: 164 additions & 0 deletions download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env python3
import functools, io, json, multiprocessing, os, requests, yaml
from PIL import Image


SLACKMOJIS_JSON_URL = "https://slackmojis.com/emojis.json"
STORAGE_DIR = "storage"
DOWNLOAD_DIR = "downloaded"
PACK_DIR = "packs"
SLACKMOJIS_JSON_FILE = "slackmojis.json"
PARALLELISM = multiprocessing.cpu_count() - 1


def create_dirs(dir):
"""Create directory and all intermediate-level directories"""
if not os.path.isdir(dir):
os.makedirs(dir)

def download_file(url, output_file):
response = requests.get(url)
with open(output_file, "wb") as f:
f.write(response.content)
return response

def write_yaml_file(data, output_file):
with open(output_file, "w") as f:
yaml.dump(data, f, default_flow_style=False)

def get_slackmojis(url, output_file):
if os.path.isfile(output_file):
with open(output_file, "r") as f:
return json.load(f)

print("fetching slackmojis ...")
page=0
slackmojis = []
while True:
print(f"... page {page}")
response = requests.get(url + f"?page={page}")
response.raise_for_status()
emojis = response.json()
if len(emojis) == 0:
break
slackmojis.extend(emojis)
page = page + 1
with open(output_file, "w") as f:
json.dump(slackmojis, f)
print("done.")
return slackmojis

def get_categories(slackmojis):
categories = set()
categories.add("uncategorized")
for slackmoji in slackmojis:
if "category" in slackmoji:
category = slackmoji["category"]["name"].lower().replace(" ", "-")
categories.add(category)
return categories

def valid_image(name, src):
try:
ext = os.path.splitext(src)[1]
# the downloaded filename is different from if you download it manually
# because of the possible duplicates
dl_file = os.path.join(STORAGE_DIR, DOWNLOAD_DIR, f"{name}{ext}")
if os.path.isfile(dl_file):
with open(dl_file, "rb") as f:
body = f.read()
else:
response = download_file(src, dl_file)
body = response.content

with io.BytesIO(body) as f:
# Is it an image?
im = Image.open(f)
if im.width > 256 or im.height > 256:
print(f":{name}: ({dl_file}) is {im.size}\t{src}")
return False, None

return True, dl_file
except Exception as e:
print(f":{name}: ({dl_file}) - unknown exception - {e}")
return False, None


def process_slackmoji(slackmoji, name_count, packs):
name = slackmoji["name"]
print(f"... {name}")

category = "uncategorized"
if "category" in slackmoji:
category = slackmoji["category"]["name"].lower().replace(" ", "-")

# Special cases - a.k.a stupid cases
if name == "yes2":
# there are two "yes" and one "yes2" emojis already
name = "yes2-1"
if name == "no2":
# there are two "no" and one "no2" emojis already
name = "no2-1"
sports = ["mlb", "nba", "nfl", "nhl"]
if category in sports:
# The NFL logo should not be :nfl-nfl:
if name == "nfl":
pass
else:
name = f"{category}-{name}"
if "facebook" in category:
name = f"fb-{name}"
if "scrabble" in category:
name = f"scrabble-{name}"

name_count[name] = name_count[name] + 1 if name in name_count else 1
if name_count[name] > 1:
name = f"{name}{name_count[name]}"
src = slackmoji["image_url"].split("?")[0]

success, dl_file = valid_image(name, src)
if not success:
return

packs[category]["emojis"].append({
"name": name,
"file": dl_file,
"src": src,
})


def main():
slackmoji_pack_dir = os.path.join(STORAGE_DIR, PACK_DIR)
create_dirs(slackmoji_pack_dir)
create_dirs(os.path.join(STORAGE_DIR, DOWNLOAD_DIR))

slackmojis = get_slackmojis(SLACKMOJIS_JSON_URL, os.path.join(STORAGE_DIR, SLACKMOJIS_JSON_FILE))
categories = get_categories(slackmojis)

with multiprocessing.Manager() as manager:
# Initialize dicts
name_count = manager.dict()
packs = manager.dict()
for category in categories:
packs[category] = {
"title": f"slackmoji-{category}",
"emojis": manager.list(),
}

# Process slackmojis
with multiprocessing.Pool(processes=PARALLELISM) as pool:
print("processing slackmojis ...")
_process_slackmoji = functools.partial(process_slackmoji, name_count=name_count, packs=packs)
pool.map(_process_slackmoji, slackmojis)
print("done")

print("writing category files ...")
for category in categories:
print(f"... {category}")
data = packs[category]
data["emojis"] = list(data["emojis"])
write_yaml_file(data, os.path.join(slackmoji_pack_dir, f"slackmojis-{category}.yaml"))
print("done")


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
beautifulsoup4
pillow
pyyaml
requests
Loading

0 comments on commit 2bd690d

Please sign in to comment.