mirror of
https://github.com/SrIzan10/hctv.git
synced 2026-06-05 16:46:50 +00:00
feat: chat emoji step 1, fetch emojis
This commit is contained in:
16
.github/workflows/docker.yml
vendored
16
.github/workflows/docker.yml
vendored
@@ -29,6 +29,22 @@ jobs:
|
||||
images: srizan10/hclive
|
||||
tags: latest
|
||||
|
||||
- name: Download latest emoji importer
|
||||
run: |
|
||||
RELEASE_URL=$(curl -s https://api.github.com/repos/srizan10/hclive/releases/latest | \
|
||||
grep "browser_download_url.*slack-import-emojis-linux-x86_64" | \
|
||||
cut -d '"' -f 4)
|
||||
|
||||
curl -L -o slack-import-emojis $RELEASE_URL
|
||||
chmod +x slack-import-emojis
|
||||
|
||||
mkdir -p apps/web/src/lib/instrumentation/
|
||||
|
||||
export SLACK_TOKEN=${{ secrets.SLACK_TOKEN }}
|
||||
./slack-import-emojis
|
||||
|
||||
mv emojis.json apps/web/src/lib/instrumentation/
|
||||
|
||||
- name: Build and push Docker image
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
|
||||
68
.github/workflows/emojis.yml
vendored
Normal file
68
.github/workflows/emojis.yml
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
name: Slack Emoji Importer Release
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
paths:
|
||||
- 'slack-import-emojis/**'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-release:
|
||||
name: Build and create release
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ./slack-import-emojis
|
||||
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Cache Rust dependencies
|
||||
uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
workspaces: "./slack-import-emojis -> target"
|
||||
|
||||
- name: Build release binary
|
||||
run: cargo build --release
|
||||
|
||||
- name: Extract version from Cargo.toml
|
||||
id: get_version
|
||||
run: |
|
||||
VERSION=$(grep '^version =' Cargo.toml | head -n 1 | cut -d '"' -f 2)
|
||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: emoji-importer-v${{ steps.get_version.outputs.version }}
|
||||
release_name: Slack Emoji Importer v${{ steps.get_version.outputs.version }}
|
||||
body: |
|
||||
Slack Emoji Importer v${{ steps.get_version.outputs.version }}
|
||||
|
||||
Fetches emojis from the Slack workspace and exports them to JSON.
|
||||
|
||||
## Usage
|
||||
```
|
||||
export SLACK_TOKEN=xoxb-your-token
|
||||
./slack-import-emojis
|
||||
```
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Linux binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./slack-import-emojis/target/release/slack-import-emojis
|
||||
asset_name: slack-import-emojis-linux-x86_64
|
||||
asset_content_type: application/octet-stream
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -42,4 +42,7 @@ dev/redis
|
||||
|
||||
.turbo
|
||||
packages/db/generated/client
|
||||
*dist
|
||||
*dist
|
||||
|
||||
slack-import-emojis/target
|
||||
**/*/emojis.json
|
||||
@@ -1,3 +1,5 @@
|
||||
# quick and dirty ai generated benchmark i created for hls streams
|
||||
|
||||
#!/usr/bin/env python3
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
@@ -28,4 +28,9 @@ export async function register() {
|
||||
}
|
||||
console.log('cron stuff registered');
|
||||
}
|
||||
|
||||
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
||||
const { emojisWriteRedis } = await import('@/lib/instrumentation/emojisWriteRedis');
|
||||
await emojisWriteRedis();
|
||||
}
|
||||
}
|
||||
|
||||
44
apps/web/src/lib/instrumentation/emojisWriteRedis.ts
Normal file
44
apps/web/src/lib/instrumentation/emojisWriteRedis.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getRedisConnection } from '@hctv/db';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { existsSync } from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export async function emojisWriteRedis() {
|
||||
const startTime = performance.now();
|
||||
const fileLocation = getPath();
|
||||
const redis = getRedisConnection();
|
||||
|
||||
const emojisFile = await readFile(fileLocation, 'utf-8');
|
||||
const emojis = JSON.parse(emojisFile) as Record<string, string>;
|
||||
|
||||
// janky way to remove not existing emojis,
|
||||
// just obliterate the emojis hash and rewrite it lol help
|
||||
await redis.del('emojis');
|
||||
for (const [name, url] of Object.entries(emojis)) {
|
||||
await redis.hset('emojis', name, url);
|
||||
}
|
||||
|
||||
const endTime = performance.now();
|
||||
console.log(`Finished writing emojis to Redis in ${(endTime - startTime).toFixed(2)}ms`);
|
||||
}
|
||||
|
||||
function getPath() {
|
||||
const possiblePaths = [
|
||||
// original
|
||||
'src/lib/instrumentation/emojis.json',
|
||||
// relative
|
||||
path.join(__dirname, 'emojis.json'),
|
||||
// standalone nextjs
|
||||
path.join(process.cwd(), 'src/lib/instrumentation/emojis.json')
|
||||
];
|
||||
console.log('Writing emojis to Redis...');
|
||||
|
||||
for (const p of possiblePaths) {
|
||||
if (existsSync(p)) {
|
||||
console.log(`Found emojis at: ${p}`);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('emojis json not found anywhere');
|
||||
}
|
||||
4
rustfmt.toml
Normal file
4
rustfmt.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
max_width = 100
|
||||
tab_spaces = 2
|
||||
hard_tabs = false
|
||||
trailing_semicolon = true
|
||||
1545
slack-import-emojis/Cargo.lock
generated
Normal file
1545
slack-import-emojis/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
slack-import-emojis/Cargo.toml
Normal file
10
slack-import-emojis/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "slack-import-emojis"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
2
slack-import-emojis/Justfile
Normal file
2
slack-import-emojis/Justfile
Normal file
@@ -0,0 +1,2 @@
|
||||
run:
|
||||
"dotenvx run -- cargo run"
|
||||
52
slack-import-emojis/src/main.rs
Normal file
52
slack-import-emojis/src/main.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use reqwest;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SlackEmojiResponse {
|
||||
emoji: HashMap<String, String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[allow(dead_code)]
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
if std::env::var("SLACK_TOKEN").is_err() {
|
||||
eprintln!("Error: SLACK_TOKEN environment variable is not set.");
|
||||
return;
|
||||
}
|
||||
|
||||
let emojis = slack_request()
|
||||
.await
|
||||
.expect("Failed to fetch emojis from Slack API");
|
||||
|
||||
println!("{:?} emojis fetched", emojis.emoji.len());
|
||||
let mut file = File::create("emojis.json").expect("failed to create file for some reason");
|
||||
let json_data = serde_json::to_string(&emojis.emoji).expect("failed to serialize emojis wtf");
|
||||
file.write_all(json_data.as_bytes())
|
||||
.expect("failed to write emojis to file");
|
||||
println!("saved :yay:");
|
||||
}
|
||||
|
||||
async fn slack_request() -> Result<SlackEmojiResponse, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let res = client
|
||||
.get("https://slack.com/api/emoji.list")
|
||||
.header(
|
||||
"Authorization",
|
||||
format!("Bearer {}", std::env::var("SLACK_TOKEN").expect("SLACK_TOKEN not set")),
|
||||
)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(response) => Ok(response.json().await?),
|
||||
Err(err) => {
|
||||
eprintln!("Error: {:?}", err);
|
||||
Err(Box::new(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user