dP.  .dP .d8888b. .d8888b. 88d888b. d888888b
 `8bd8'  88'  `"" Y8ooooo. 88'  `88    .d8P'
 .d88b.  88.  ...       88 88        .Y8P   
dP'  `dP `88888P' `88888P' dP       d888888P
    
PROGRAMMER /ˈprōˌɡramər/
A person who solves problems you didn't know you had in ways you don't understand.
public wərk snippets contact

Code Snippets

Posting from Dart to a URL with a redirect: Since Dart's stock http package (and even the dio package) chose to handle redirects differently than basically every other language+package out there and disregard redirect responses returned to a POST request you have to do a little extra work. Worse than anything is every solution I have found was #doingItWrong and making the followup request a POST request complete with sending the payload each time. Even those approaches fail to successfully POST to a google app script (GAS). All of this is quite ironic since Dart is itself a product of Google. This is a simple proof of concept and only supports one redirect which all that is needed for a GAS doPost endpoint, but could be modified easily to accomodate other situations.
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart';
String url =
"https://script.google.com/macros/s/YOUR_DEPLOYMENT_HASH_HERE/exec";
final data = {
'foo': 456,
'bar': 'that',
'biz': 23,
};
void main(List<String> args) {
postRequestWithRedirect(url, jsonEncode(data)).then((r) {
print('code: ' + r.statusCode.toString());
print('body: ' + r.responseBody.toString());
});
}
class postRequestWithRedirectResult {
num? statusCode;
String? responseBody;
}
Future<postRequestWithRedirectResult> postRequestWithRedirect(
String url,
String body,
) async {
var httpclient = HttpClient();
HttpClientRequest request;
HttpClientResponse response;
postRequestWithRedirectResult result = postRequestWithRedirectResult();
try {
request = await httpclient.postUrl(Uri.parse(url));
request.headers.contentLength = body.length;
request.write(body);
response = await request.close();
final location = response.headers.value(HttpHeaders.locationHeader);
if (response.statusCode == 302 && location != null) {
request = await httpclient.getUrl(Uri.parse(location));
response = await request.close();
result.statusCode = response.statusCode;
result.responseBody = await response.transform(utf8.decoder).join();
} else {
result.statusCode = response.statusCode;
result.responseBody = await response.transform(utf8.decoder).join();
}
} finally {
httpclient.close(force: true);
return result;
}
}
Add a gentle fade-in from black to the beginning and a matching fade-out to the end of a video clip
#!/bin/bash
destfile() {
dir=$(dirname "$1")
filename=$(basename -- "$1");
extension="${filename##*.}";
basename="${filename%.*}";
filepath="${dir}/${basename}-faded.${extension}";
c=0
while [ -f "$filepath" ]; do
c=$((c+1))
filepath="${dir}/${basename}-faded-${c}.${extension}";
done
echo "${filepath}";
}
SRCFILE=$1
FADEIN=${2:-'1.5'}
FADEOUT=${3:-$FADEIN}
if [ "${SRCFILE}" == "" ]; then
echo "You must provide a source file";
exit;
fi
if [ ! -f "${SRCFILE}" ]; then
echo "${SRCFILE} does not exists.";
exit;
fi
echo "reading from ${SRCFILE}";
OUTFILE=$(destfile "${SRCFILE}");
echo "writing to ${OUTFILE}"
LENGTH=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${SRCFILE}")
STARTFADEOUT=$(echo "${LENGTH} ${FADEOUT}" | awk '{ print $1-$2 }')
ffmpeg -i "${SRCFILE}" -vf "fade=t=in:st=0:d=${FADEIN},fade=t=out:st=${STARTFADEOUT}:d=${FADEOUT}" "${OUTFILE}"
echo "done writing ${OUTFILE}."
Install Python 3.9 on CentOS or Amazon Linux
#!/bin/sh
sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel
cd /tmp
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
tar -xvf Python-3.9.6.tgz
cd Python-3.9.6
./configure --enable-optimizations
sudo make altinstall
python3.9 --version
Reconcile descrepencies in document counts within elasticsearch
from requests import get
from json import dumps, loads
from prettytable import PrettyTable
src_server = 'http://localhost:9200'
def docCount(idx=None, typ=None):
url = src_server
if idx:
url += "/%s" % idx
if typ:
url += "/%s" % typ
url += "/_count"
res = loads(get(url).content)
return res['count'] if 'count' in res else 0
tbl = PrettyTable(['Index', 'Count on Index', 'Docs In Types'])
tbl.add_row(['entire cluster', docCount(), 'n/a'])
tbl.add_row(['=====','=====','====='])
src_state=loads(get("%s/_cluster/state" % (src_server), timeout=600).content)
docsCountedInIndexes = 0
docsCountedInTypes = 0
for idx in src_state['metadata']['indices']:
docsInIndex = docCount(idx)
docsCountedInIndexes += docsInIndex
docsInTypes = 0
for typ in src_state['metadata']['indices'][idx]['mappings']:
docsInTypes += docCount(idx, typ)
docsCountedInTypes += docsInTypes
tbl.add_row([idx, docsInIndex, docsInTypes])
tbl.add_row(['-----','------','-----'])
tbl.add_row(['Totals', docsCountedInIndexes, docsCountedInTypes])
print(tbl)
This script prints out the current list of Amazon2 Linux AMI's by region (kernel 5.10, HVM, x86_64, gp2). Changing the image variable and the Path filter make this capable of searching for any public/shared image.
import boto3
from json import dumps
from yaml import dump
image = 'amzn2-ami-kernel-5.10-hvm-x86_64-gp2'
regions = [
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-south-1',
'ap-southeast-1',
'ap-southeast-2',
'ca-central-1',
'eu-central-1',
'eu-north-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
]
amis = {}
for region in regions:
client = boto3.client('ssm', region)
paginator = client.get_paginator('get_parameters_by_path')
iterator = paginator.paginate(
Path='/aws/service/ami-amazon-linux-latest'
)
matches = []
for batch in iterator:
matches.extend(list(filter(lambda det: image in det['Name'], batch['Parameters'])))
if len(matches) < 1:
print('no matches found in', region)
continue
elif len(matches) > 1:
print('multiple matches found in', region, '- perhaps you should narrow your results')
for match in matches:
print("\t-", match)
# grab the first one if we have more than one
print('found', matches[0]['Name'], matches[0]['Value'], 'in', region)
amis[region] = {'AMI': matches[0]['Value']}
print(dumps(amis, indent=4))
print(dump(amis))