Skip to content

Instantly share code, notes, and snippets.

View kakarukeys's full-sized avatar

Wong Jiang Fung kakarukeys

View GitHub Profile
@kakarukeys
kakarukeys / solution1.js
Created May 21, 2017 13:49
solution1 - string compression
// if performance is not important
function compress(str) {
/* shorten <str> by reducing consecutive character repetitions */
return str.replace(/(.)\1*/g, function(fullMatch, g1) {
return g1 + fullMatch.length;
});
}
console.assert(compress('') === '', "blank string");
console.assert(compress('a') === "a1", "single char");
@kakarukeys
kakarukeys / solution2.js
Created May 21, 2017 13:51
solution2 - URI comparison
function parseURI(uri) {
/* parse <uri> into invidual components (loosely, some errors allowed) */
// see https://regex101.com/r/l96l7F/1 for explanation
var regex = /(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?((?:\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?[^?#\/]*)(?:\?([^#]*))?(?:#(.*))?/,
groupNames = [
"fullMatch",
"scheme",
"username",
"password",
from collections import defaultdict
from asyncio import Semaphore
MAX_CONNECTIONS_PER_MX = 1
leaky_bucket = defaultdict(lambda: Semaphore(MAX_CONNECTIONS_PER_MX))
async def ping_server_handler(request):
...
@kakarukeys
kakarukeys / .bash_profile
Last active December 4, 2021 00:50
shell script to enable Poetry .env loading
function poetry() {
dot_env_path=$(pwd)
while [[ "$dot_env_path" != "" && ! -e "$dot_env_path/.env" ]]; do
dot_env_path=${dot_env_path%/*}
done
# if POETRY_DONT_LOAD_ENV is *not* set, then load .env if it exists
if [[ -z "$POETRY_DONT_LOAD_ENV" && -f "$dot_env_path/.env" ]]; then
>&2 echo 'Loading .env environment variables…'
@kakarukeys
kakarukeys / borrow_cookies.py
Created April 22, 2021 03:41
borrow_cookies.py
# -*- coding: utf-8 -*-
import json
import logging
import os.path
import subprocess
import time
from http.cookiejar import LWPCookieJar
from urllib.parse import urlparse
from selenium import webdriver
@kakarukeys
kakarukeys / test_mp.py
Created April 29, 2021 04:27
Testing multi-processing processes
import time
import random
from multiprocessing import Process
def f():
while True:
time.sleep(10)
dice = random.randint(1, 10)
@kakarukeys
kakarukeys / test_mp.py
Last active April 30, 2021 06:52
python multiprocessing, clean up child processes upon shutdown
import time
import random
from signal import signal, SIGINT, SIGTERM
from multiprocessing import Process, Event
"""
There are two examples:
(1) with graceful shutdown
(2) without graceful shutdown
@kakarukeys
kakarukeys / crawler.py
Last active May 12, 2021 06:59
requests crawler (tutorial 1)
import time
import zlib
from io import BytesIO
from zipfile import ZipFile
import requests
from bs4 import BeautifulSoup
HEADERS = {
@kakarukeys
kakarukeys / crawler.py
Last active May 12, 2021 06:59
automate login (tutorial 2)
import os
import time
from PIL import Image
import requests
from bs4 import BeautifulSoup
# requires brotlipy, as the server uses brotli compression
username = os.environ["SSM_USERNAME"]
@kakarukeys
kakarukeys / crawler.py
Last active May 12, 2021 06:59
automate login again (tutorial 10)
import time
from PIL import Image
import requests
from bs4 import BeautifulSoup
HEADERS = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:88.0) Gecko/20100101 Firefox/88.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",