Skip to content

Instantly share code, notes, and snippets.

View wecsam's full-sized avatar

David Tsai wecsam

View GitHub Profile

Keybase proof

I hereby claim:

  • I am wecsam on github.
  • I am wecsam (https://keybase.io/wecsam) on keybase.
  • I have a public key whose fingerprint is EADF 834A CB9A 4425 BFA7 9286 86A1 72A6 1CB7 6334

To claim this, I am signing this object:

@wecsam
wecsam / dtmf.py
Created October 10, 2017 22:50
Generates touch tones like on a telephone
#!/usr/bin/env python3
# DTMF = Dual Tone Multi-Frequency
import itertools, math, pyaudio, random, sys
BITRATE = 8000
TONE_DURATION = 0.1 # seconds
KEY_FREQUENCIES = {
'1': (697, 1209),
'2': (697, 1336),
'3': (697, 1477),
'A': (697, 1633),
@wecsam
wecsam / envify.py
Created October 10, 2017 22:54
Substitutes environment variables into the given path
#!/usr/bin/python3
import os, sys
# This script takes a string as an argument and outputs the same path with as many
# environment variables substituted in as possible.
def variable_syntax(variable_name):
try:
return {
"nt": lambda variable_name: "%" + variable_name + "%",
"posix": lambda variable_name: "$" + variable_name
}[os.name](variable_name)
@wecsam
wecsam / sessionfromedge.py
Last active December 16, 2017 03:17
This class is derived from requests.Session. It supports everything that requests.Session supports. Upon instantiation, this class opens Microsoft Edge, loads the specified URL, and copies the cookies, user agent string, and HTTP Referer header from the browser into this session. This allows your code to make requests as that browser.
import os, requests, threading, time, winreg
from selenium import webdriver
# Module-wide lock to prevent multiple WebDriver instances from running at the same time
_webdriver_lock = threading.Lock()
def get_windows_build_number():
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion")
try:
return int(winreg.QueryValueEx(key, "CurrentBuild")[0])
@wecsam
wecsam / outlook.py
Last active November 24, 2018 03:57
Creates a MailItem object in Microsoft Outlook with stuff already filled in
import win32com.client
class AttachmentInfo:
def __init__(self, file_path, mime_type, attachment_id=None):
self.file_path = file_path
self.mime_type = mime_type
self.id = attachment_id
def draft_html(
to=(),
@wecsam
wecsam / Controlled Folder Access Log.bat
Created March 23, 2018 22:27
Displays events from the last day that are related to Controlled Folder Access in Windows 10
@REM Pull events with IDs 1123, 1124, and 5007 from the last day.
@REM Get-WinEvent "Microsoft-Windows-Windows Defender/Operational" | ? { $_.Id -eq 1123 -or $_.Id -eq 1124 -or $_.Id -eq 5007 } | ? { ((Get-Date) - $_.TimeCreated).TotalDays -le 1 } | Format-List
@powershell -EncodedCommand RwBlAHQALQBXAGkAbgBFAHYAZQBuAHQAIAAiAE0AaQBjAHIAbwBzAG8AZgB0AC0AVwBpAG4AZABvAHcAcwAtAFcAaQBuAGQAbwB3AHMAIABEAGUAZgBlAG4AZABlAHIALwBPAHAAZQByAGEAdABpAG8AbgBhAGwAIgAgAHwAIAA/ACAAewAgACQAXwAuAEkAZAAgAC0AZQBxACAAMQAxADIAMwAgAC0AbwByACAAJABfAC4ASQBkACAALQBlAHEAIAAxADEAMgA0ACAALQBvAHIAIAAkAF8ALgBJAGQAIAAtAGUAcQAgADUAMAAwADcAIAB9ACAAfAAgAD8AIAB7ACAAKAAoAEcAZQB0AC0ARABhAHQAZQApACAALQAgACQAXwAuAFQAaQBtAGUAQwByAGUAYQB0AGUAZAApAC4AVABvAHQAYQBsAEQAYQB5AHMAIAAtAGwAZQAgADEAIAB9ACAAfAAgAEYAbwByAG0AYQB0AC0ATABpAHMAdAA=
@PAUSE
@wecsam
wecsam / serve0.py
Last active March 24, 2018 00:10
Serves stuff from STDIN over HTTP
#!/usr/bin/env python3
import argparse, io, socketserver, sys, threading
class StringServer(socketserver.BaseRequestHandler):
# These headers will be sent with every HTTP reply.
HEADERS = (
b"HTTP/1.1 200 OK\r\n"
b"Content-Type: application/octet-stream\r\n"
)
# If True, then data that is received will be forwarded to STDOUT.
@wecsam
wecsam / orifice.py
Last active April 28, 2018 01:01
Like cat but with rate limiting
#!/usr/bin/env python3
import collections, os.path, queue, sys, textwrap, threading, time
UNITS = {
"char": lambda fin: fin.read(1),
"line": lambda fin: fin.readline(),
}
def producer(backlog, stop, unit_getter, fin):
'''
Continually does backlog.put(unit_getter(fin)). If KeyboardInterrupt
@wecsam
wecsam / AddAttachmentsToSelection.vba
Created July 12, 2018 18:16
Attach selected files to selected Outlook items
' This macro is for Microsoft Outlook. It opens a file picker dialog and then
' attaches the selected files to the selected items.
' This macro requires the object library for the installed version of Microsoft
' Word. In the VBA editor, go to Tools > References... and make sure that it is
' checked off. For Word 2016, choose the Microsoft Word 16.0 Object Library.
Sub AddAttachmentsToSelection()
Dim objMessage As Variant
Dim strPath As Variant
' Outlook does not have a file picker. Use an instance of Word instead.
Dim appWord As Word.Application
@wecsam
wecsam / prevent_parallel.py
Created September 11, 2018 04:49
Prevents concurrent instances of a Python script
#!/usr/bin/env python3
'''
Import this module to ensure that at most one instance of the main script can
be running at once. If another instance is detected, sys.exit() will be called.
The psutil package is required: pip install psutil
'''
import atexit, logging, math, os, psutil, socket, sys
logger = logging.getLogger("prevent_parallel")
PID_FILE = \