Skip to content

Instantly share code, notes, and snippets.

@alexshpilkin
alexshpilkin / change
Last active February 5, 2019 13:38
Convert between Transtool and GNU gettext files
#!/bin/sh -eu
po=$(printf %s *.??.po)
detail=false; if [ "x${1-}" = x-d ]; then detail=true; shift; fi
base=${1-/dev/null}
prev=; next=; diff=
trap 'rm -f "$prev" "$next" "$diff"' EXIT
prev=$(mktemp); next=$(mktemp); diff=$(mktemp)
@alexshpilkin
alexshpilkin / push
Last active March 31, 2019 18:57
Watch the Ukrainian CVK website and send push notifications
#!/bin/sh -eu
ENDPOINT="https://middleman.ferdinand-muetsch.de/api/messages"
if [ "$#" -ne 2 ]; then
echo "usage: $0 TOKEN ORIGIN" >&2
exit 1
fi
jq -ac --unbuffered --arg token "$1" --arg origin "$2" \
'{"recipient_token": $token, "text": ., "origin": $origin}' | \
@alexshpilkin
alexshpilkin / datafile.py
Last active April 2, 2019 20:31
Write data from stdin to a ZIP file on the fly
#!/usr/bin/env python3
from collections import deque as Deque
from zipstream import ZipFile
class Buffer:
def __init__(self):
self.queue = Deque()
self.finished = False
@alexshpilkin
alexshpilkin / ruforum
Last active April 3, 2019 17:52
Scrape rutracker.org forums
#!/bin/sh -eu
set -o pipefail
TRACE=${TRACE-}; export TRACE
tracef() {
fmt=$1; shift
if [ -t 2 ]; then printf '\033[0K%s'"$fmt"'\r' "$TRACE" "$@" >&2; fi
}
trap 'tracef ""' EXIT
@alexshpilkin
alexshpilkin / aggregate.py
Last active April 14, 2019 20:12
Aggregate multiple CVK scrapes into precinct histories
#!/usr/bin/env python3
# NB Depends on dict preserving the insert order (CPython >= 3.6, PyPy all)
from csv import DictReader
from datetime import datetime as DateTime
from os import scandir
from simplejsonseq import dump
from sys import argv, stderr, stdout
import ietfcsv # ietf-tab CSV dialect
@alexshpilkin
alexshpilkin / drv.wsdl
Last active February 3, 2023 23:25
Access SOAP APIs of the Ukrainian DRV with Python and Zeep
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:tns="http://www.drv.gov.ua/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="http://www.drv.gov.ua/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.drv.gov.ua/">
@alexshpilkin
alexshpilkin / diffs.sh
Last active April 24, 2019 13:17
Add diffs to results posts
#!/bin/sh -eu
# NB: Contains literal tabs
caption() {
python -c '
from os import getenv
from sys import argv
from telegram import Bot
from telegram.error import BadRequest, TimedOut
@alexshpilkin
alexshpilkin / moscow.tsv
Last active May 10, 2019 14:14
Administrative divisions of Moscow
45 Город Москва
45 301 муниципальный округ Богородское
45 302 муниципальный округ Вешняки
45 303 муниципальный округ Восточное Измайлово
45 304 муниципальный округ Восточный
45 304 000 106 п Восточный
45 304 000 111 п Акулово
45 305 муниципальный округ Гольяново
45 306 муниципальный округ Ивановское
45 307 муниципальный округ Измайлово
@alexshpilkin
alexshpilkin / gstream.py
Created June 17, 2019 10:25
Pipe video from DVR to GStreamer
#!/usr/bin/env python3
from gi import require_version
require_version('Gst', '1.0')
require_version('Gtk', '3.0')
from gi.repository import GObject, Gst, Gtk
from os.path import abspath
from sys import argv, stderr
from threading import Thread
@alexshpilkin
alexshpilkin / trio_priority.py
Created January 7, 2021 22:11
Trio channels with priorities
from heapq import heappush, heappop
from math import inf
from trio import BrokenResourceError, ClosedResourceError, EndOfChannel, WouldBlock
from trio.abc import ReceiveChannel, SendChannel
from trio.lowlevel import ParkingLot, checkpoint, checkpoint_if_cancelled, cancel_shielded_checkpoint
from trio._channel import MemoryChannelStats
from trio._util import NoPublicConstructor
class MemoryChannelState:
__slots__ = ('data', 'max_buffer_size', 'number', 'open_send_channels',