Skip to content

Instantly share code, notes, and snippets.

@lachesis
lachesis / weather-discussion.py
Created May 18, 2022 21:11
NWS weather textual discussion
#!/usr/bin/python
import argparse
import re
import sys
import requests
def main():
SHOW_GROUPS = [
r'SYNOPSIS',
r'DISCUSSION',
#!/usr/bin/python
# usage: python3 dump_cosmos_staking_rewards.py < address_list.txt > staking_rewards.tsv
import decimal
import fileinput
import collections
import re
import sys
import requests
import datetime
import json
@lachesis
lachesis / extract_osmosis_swaps.py
Created January 22, 2022 17:46
Simple tool using the Mintscan API to generate a TSV of Osmosis swaps
#!/usr/bin/python3
import json
import sys
import requests
def get_transactions(address):
resp = requests.get(
'https://api-osmosis.cosmostation.io/v1/account/new_txs/' + address,
params={'limit':5000, 'from': 0},
timeout=31,
@lachesis
lachesis / expiringlru.py
Last active December 3, 2022 20:25
Python expiring LRU
# License: Public Domain, use as you like, no warranty
import time
import threading
import pylru # from pip
class ExpiringLRUCache(object):
def __init__(self, size, exp=7200):
self.exp = exp
self.size = size
self.lru = pylru.lrucache(size)
#!/usr/bin/env python3
# Get duke energy hourly electricity usage
# before running: pip3 install requests
# use env vars to set username, password, and lookback days
# e.g. [email protected] DUKE_PASSWORD=hunter11 DUKE_DAYS_BACK=14 python dukeusage.py
import os
import datetime
import json
import re
@lachesis
lachesis / airnow.py
Last active December 3, 2022 20:25
Display AQI for a given LAT/LNG using data from https://fire.airnow.gov (AirNow, AirSis, Purple Air)
#!/usr/bin/python3
# Prints current PM2.5 concentrations based on fire.airnow.gov map
# pip3 install requests pytz geobuf
# usage: LAT=37.9053745 LNG=-122.3048239 MAX_FEATURES=6 MAX_DIST=11 python3 airnow.py
# optional, for AQI instead of raw concentration (µg/m³): pip3 install python-aqi and pass AQI=1
import collections
import datetime
import math
import os
import requests
#!/usr/bin/env python3
# Subscribes contacts in a CSV file to a Close campaign
# Requires python3.7 or newer (f-strings)
# Prepare: save this script in close_sub.py and `pip3 install closeio`
# CSV should be two columns with email followed by exact Sequence name on Close
# Usage: KEY=api_yourcloseiokey LIVE=0 python3 close_sub.py leads.csv
import csv
import fileinput
import os
import re
@lachesis
lachesis / statds-last.sh
Created June 11, 2019 02:42
Get last value of statsd gauge from grafana server
#!/bin/bash
# Get last value of statsd gauge from grafana server
# Set env vars:
# GRAFANA_BASE="https://www.example.com" <-- your grafana base URL
# GRAFANA_API_KEY="eyJryJzd.....IxMj0F9" <-- your grafana API key
curl --compressed --silent \
"$GRAFANA_BASE/api/datasources/proxy/1/render" \
-H "Authorization: Bearer $GRAFANA_API_KEY" \
--data-urlencode "target=summarize($1, "'"1h", "last")' \
@lachesis
lachesis / 2fa.py
Created September 21, 2018 20:07
Quick Py2/Py3 TOTP Generator
from __future__ import print_function, division, unicode_literals
import sys, time, re, struct, base64, hmac, hashlib
def get_hotp(secret, counter):
"""Return the HMAC-Based One-Time Password for the the given secret (base32 encoded) and the counter.
>>> [get_hotp('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', i) for i in xrange(10)]
[755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489]
"""
secret = re.sub(r'\s*', '', secret) # remove whitespace
secret = secret.upper() # uppercase
if len(secret) % 8 != 0:
@lachesis
lachesis / sb6190_notes.md
Last active November 13, 2016 04:43
Arris SB6190 bug notes

I'm experiencing the weirdest internet issue. I use Comcast Xfinity in El Cerrito, CA. I have a new Arris SB6190 cable modem running firmware version 9.1.93N. A few days ago, I noticed that new tabs start taking a few seconds to load, and curl requests to example.com took ~5.1 seconds. However, if I specified -4 or -6 to curl, it resolved immediately. I immediately suspected DNS, but "host" resolved immediately as well.

So this was too weird to ignore. I set my resolv.conf to point to 8.8.8.8 (instead of my EdgeRouter X) and did:

time strace -f -T curl -s http://www.example.com > /dev/null 2> bad
time strace -f -T curl -4 -s http://www.example.com > /dev/null 2> good

Now if you look at the resulting logs, you'll see that curl starts a new thread to handle DNS resolution. In the good logs, the relevant bit looks like:

[pid 12438] socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_IP) = 3 <0.000005>