Skip to content

Instantly share code, notes, and snippets.

@azechi
azechi / oauth2_authz_code_grant.py
Last active May 14, 2022 00:12
Get OAuth 2.0 Refresh Tokens
async def oauth2_authz_code_grant(scope, client_id, client_secret, auth_uri, token_uri, redirect_port=0, timeout=60):
code, redirect_uri = await get_authorization_code(scope, client_id, auth_uri, redirect_port, timeout)
return get_token(code, redirect_uri, client_id, client_secret, token_uri)
async def get_authorization_code(scope, client_id, auth_uri, port=0, timeout=60):
from asyncio import start_server, wait_for, Event, TimeoutError
from urllib.parse import urlencode, urlparse, parse_qs
event = Event()
@azechi
azechi / read_pin.sh
Last active February 13, 2022 03:34
Dump Raspberry Pi GPIO edge detection into VCD format file with pigpio notifications and pig2vcd.
#!/usr/bin/env bash
b=0
for i
do
b=$(($b | $((1 << $i))))
done
n=`pigs no`
pig2vcd </dev/pigpio$n >report_pin.vcd &
@azechi
azechi / AEHA_IR_Transmitter.ino
Last active February 18, 2022 01:24
Infared remote control for AEHA format with Arduino Uno.
#include <util/atomic.h>
int T = 470;
void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
pinMode(9, OUTPUT);
@azechi
azechi / client.js
Created January 24, 2021 01:48
discord.js, voice channel, streaming, raw PCM, child process
const BOT_TOKEN = process.env.DISCORD_BOT_TOKEN;
const CHANNEL_ID = process.env.VOICE_CHANNEL_ID;
const { spawn } = require('child_process');
const Discord = require('discord.js');
const prism = require('prism-media');
const client = new Discord.Client();
client.on('ready', async () => {
@azechi
azechi / pkce.js
Created September 26, 2020 05:16
JavaScript RFC7636 PKCE code_verifier and SHA-256 code_challenge with Web Cryptography API
//https://tools.ietf.org/html/rfc7636#appendix-B
const code_verifier = btoa(
String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32)))
).replace(/\/|\+|=/g, (x) => ({ "/": "_", "+": "-", "=": "" }[x]));
const hash = await crypto.subtle.digest(
"SHA-256",
new Uint8Array([...code_verifier].map((e) => e.charCodeAt(0)))
);
@azechi
azechi / Dockerfile
Created April 10, 2020 13:40
docker discordrb
FROM ruby AS builder
COPY Gemfile Gemfile.lock ./
RUN env DEBCONF_NOWARNINGS=yes bundle install -j "$(getconf _NPROCESSORS_ONLN)" --retry 3 \
&& rm -rf /usr/local/bundle/cache/*.gem \
&& find /usr/local/bundle/gems/ -name '*.c' -delete \
&& find /usr/local/bundle/gems/ -name '*.o' -delete
# app
FROM ruby:slim
RUN apt-get update \
@azechi
azechi / win32api_WindowRect.ipynb
Created December 3, 2019 00:47
windows api, QueryFullProcessImageName, DwmGetWindowAttribute,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@azechi
azechi / GetIPAddress.js
Last active April 24, 2019 01:38
Find out IP Address on the public side of a NAT using public stun server
function getIPAddress() {
const S = "stun.l.google.com:19302";
return new Promise(resolve=>{
const pc = new RTCPeerConnection({
"iceServers": [{
"urls": ["stun:" + S]
}]
@azechi
azechi / 0_read_QR_Code_from_webcam.html
Last active April 4, 2019 03:41
Read QR Code from video with web browser using cozmo/jsQR
<!DOCTYPE html>
<html>
<head>
<style>
#canvas {
width: 100%;
vertical-align: bottom;
}
body {
max-width: 640px;
@azechi
azechi / RandomBase32String.js
Last active July 3, 2023 17:14
Generate a random base32 string(length:6) using Web Crypto API
const base = "abcdefghijklmnopqrstuvwxyz234567" //a-z, 2-7
const array = new Int32Array(1)
window.crypto.getRandomValues(array)
const i = array[0]
var result =
base[i >>> 27 & 0x1f] +
base[i >>> 22 & 0x1f] +
base[i >>> 17 & 0x1f] +