Skip to content

Instantly share code, notes, and snippets.

@seanghay
seanghay / index.html
Last active February 10, 2024 13:36
Xiaomi Light Bar Remote via RPi + nRF24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Xiaomi LightBar Controller</title>
<style>
* {
@seanghay
seanghay / run.sh
Last active July 12, 2023 08:23
Download & Trim Video using yt-dlp and ffmpeg
#!/usr/bin/env bash
# Download & Trim Video
# ./run.sh video_id out.mp4 00:00 1:00
OUTPUT_FILE=$2
VIDEO_ID=$1
yt-dlp --quiet --force-overwrites \
-f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" \
#!/usr/bin/env python
# encoding: utf-8
import time
from transformers import ViTFeatureExtractor, ViTForImageClassification
from PIL import Image
import json
from flask import Flask, request
app = Flask(__name__)
/**
* Note: Not the fastest solution! It's a demo on how to use bitwise operator to avoid branching.
* Convert 24-hour time to 12-hour.
* @returns {string}
* @param {number} a 24-hour number.
*/
function to12time(a) {
return (a - 1) % 12 + 1 + String.fromCharCode(112 - 15 * (a < 12 ^ 0)) + "m"
}
export default function lunar(date = new Date()) {
const aharakoune = y => (y * 292207 + 373) % 800;
const harakoune = (y) => Math.floor((y * 292207 + 373) / 800) + 1;
const avomane = (y) => (11 * harakoune(y) + 650) % 692;
const regularLeap = y => (800 - aharakoune(y)) <= 207
function bodethey(y) {
const ha = harakoune(y);
return (ha + Math.floor((11 * ha + 650) / 692)) % 30;
}
@seanghay
seanghay / mean-median-mode-math.js
Last active October 30, 2022 04:43
Mean, Median & Mode functions using JavaScript
/**
* An average of a set of numbers.
* @param {number[]} values
* @returns {number}
*/
export function mean(...values) {
if (values.length === 0) return;
return values.reduce((a, b) => a + b, 0) / values.length;
}
@seanghay
seanghay / khmer-lexer.js
Last active October 24, 2022 07:17
Khmer Tokenizer
const SPACE_CODEPOINTS = [
0xa, // newline r
0xd, // newline r
0x9, // tab,
0x0020, // space
0x00A0, // no-break space
0x1680, // Ogham space mark
0x180E, // Mongolian vowel separator
0x2000, // en quad
0x2001, // em quad
@seanghay
seanghay / fetch-postal-code-cambodia.js
Created October 19, 2022 16:08
Download Postal Code Info in Cambodia
import axios from 'axios'
import { load } from 'cheerio'
import fs from 'fs/promises'
import fse from 'fs-extra'
import Interpreter from 'js-interpreter'
// download provinces list
async function downloadProvinceList() {
const { data: html } = await axios.get('https://www.cambodiapost.post/page/postal-codes', { responseType: "text" });
@seanghay
seanghay / khmer-number.js
Last active October 16, 2022 15:16
Convert Khmer Numerals to ASCII
const km2ascii = (s) =>[...s].reduce((p,c)=>p+(c=c.charCodeAt(0),String.fromCharCode(c-(c>=6112&&c<=6121?6064:0))),'')
km2ascii('1234abc១២៣៤')
// => '1234abc1234'
@seanghay
seanghay / khmer-zwsp-insert.js
Last active September 21, 2022 09:38
Automatically insert ZWSP into text.
import MagicString from "magic-string";
/**
* Automatically insert ZWSP into text.
* @param {string} value
* @param {string | undefined} sep
* @returns {string}
*/
export function insertZeroWidthSpace(value, sep = '\u200b') {