Skip to content

Instantly share code, notes, and snippets.

View linw1995's full-sized avatar
🎯
Focusing

林玮 (Jade Lin) linw1995

🎯
Focusing
View GitHub Profile
@linw1995
linw1995 / main.py
Created April 9, 2021 05:51
Click with async command
# Standard Library
import asyncio
import functools
# Third Party Library
import click
def async_command(coro_func):
@functools.wraps(coro_func)
@linw1995
linw1995 / main.py
Last active December 12, 2023 07:51
A simple example shows how to hook response with mitmproxy in script.
# Standard Library
import asyncio
import contextlib
from pathlib import Path
from typing import Callable, Optional
# Third Party Library
import httpx
@linw1995
linw1995 / main.py
Created April 6, 2021 10:39
aiohttp integrates with mitmproxy
import ssl
import asyncio
from aiohttp import ClientSession
async def main():
async with ClientSession() as session:
sslcontext = ssl.create_default_context(cafile='~/.mitmproxy/mitmproxy-ca.pem')
r = await session.get('https://example.com', proxy='http://localhost:8080', ssl=sslcontext)
@linw1995
linw1995 / main.py
Last active April 6, 2021 10:05
Convert pyppeteer cookies to SimpleCookie
import logging
from http.cookies import CookieError, Morsel, SimpleCookie
from pyppeteer.page import Page
logger = logging.getLogger(__name__)
PYPPETEER_2_MORSEL = {"httpOnly": "httponly", "sameSite": "samesite"}
@linw1995
linw1995 / main.py
Last active April 6, 2021 09:23
Get browser timezone in pyppeteer.
from datetime import timedelta, timezone
from pyppeteer.page import Page
async def get_browser_timezone(page: Page) -> timezone:
js_timezone_offset = await page.evaluate(
"""
function () {
return new Date().getTimezoneOffset();
@linw1995
linw1995 / README.org
Last active April 2, 2021 06:49
Get PDM to work with lsp-python-ms in Emacs

Get PDM to work with lsp-python-ms in Emacs

A code snippet to show how to use PDM to work with lsp-python-ms in Emacs.

;; TODO: Cache result
(defun linw1995/pdm-get-python-executable (&optional dir)
  (let ((pdm-get-python-cmd "pdm info --python"))
    (string-trim
     (shell-command-to-string
@linw1995
linw1995 / utils.py
Last active March 24, 2021 07:16
Enum class for creating enum members with name and value being same.
import enum
class AutoValue(enum.Enum):
# is same with the example of docs.python.org
# https://docs.python.org/3/library/enum.html#using-automatic-values
def _generate_next_value_(name, start, count, last_values):
return name
@linw1995
linw1995 / demo.py
Last active March 10, 2021 07:39
Click with optional arguments which are enum type.
# Standard Library
import enum
# Third Party Library
import click
class Order(enum.Enum):
latest = "latest"
oldest = "oldest"
@linw1995
linw1995 / example.py
Created March 9, 2021 08:19
gracefully shutdown the top asyncio coroutine in Python
import asyncio
import signal
import logging
import contextlib
logger = logging.getLogger(__name__)
def asyncio_run(coro):
task = asyncio.ensure_future(coro)
@linw1995
linw1995 / cmd_with_retry.py
Last active May 9, 2021 13:09
Simple script for executing command with retry
import os
import subprocess
import sys
def run_cmd_with_retry(
cmd,
retry_count_limit=3,
retry_delay_seconds=60,
stdout=sys.stdout.fileno(),