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

  • 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
  • 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}."
  • 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;
    }
    }
  • The most simple method to post a message to slack via an 'incoming webhook' from PHP
    <?php
    function post_slack_message($msg) {
    $opts = [
    'http' => [
    'method' => 'POST',
    'header' => "Content-type: application/json\r\n",
    'content' => json_encode([
    'attachments' => [
    [
    'color' => '#ff0000',
    'blocks' => [
    [
    'type' => 'header',
    'text' => [
    'type' => 'plain_text',
    'text' => $msg,
    ]
    ]
    ]
    ],
    ]
    ]),
    'ignore_errors' => true,
    ]
    ];
    $response = file_get_contents(getenv('SLACK_WEBHOOK_URL'), false, stream_context_create($opts));
    if($response !== 'ok') {
    var_dump($http_response_header); // dump headers
    var_dump($response); // dump body as well (usually where the problem can be found)
    throw new \Exception("Unable to post to slack");
    }
    }
    post_slack_message('hi there');
    <?php
    function post_slack_message_styled($msg) {
    $opts = [
    'http' => [
    'method' => 'POST',
    'header' => "Content-type: application/json\r\n",
    'content' => json_encode([
    'text' => $msg,
    ]),
    'ignore_errors' => true,
    ]
    ];
    $response = file_get_contents(getenv('SLACK_WEBHOOK_URL'), false, stream_context_create($opts));
    if($response !== 'ok') {
    var_dump($http_response_header); // dump headers
    var_dump($response); // dump body as well (usually where the problem can be found)
    throw new \Exception("Unable to post to slack");
    }
    }
    post_slack_message_styled('hi there');
  • How to adjust Softail rear shock preload

    FLSTN Rear Shock Adjustment

    So Softails's come from the factory with the lightest setting on the read shocks which is only good for solo riders weighing less than 180 lbs. If you weigh more yourself or you want to ride with a pack, a passenger or both - you need to adjust the preload on the rear shocks to safe your back, your passengers back and potentially your ride. A properly adjusted suspension makes all the difference!

    Both the owners manual and service manual have a section describing the basics of adjusting the preload, but nothing from HD offers the actual steps to take or any guide to adjust for a specific weight. The procedure is very simple but without a guide you're left to trial and error and a lot of jacking or relying on a shop to make the adjustment for you. This is based on articles posted online by dealers and HD service shops (and doesn't that defeat the purpose of having an HD in the first place?).

    This is not an exact adjustment, and perhaps that's why HD doesn't spell it out. Below there are guidelines to start from, but you adjust for a softer or stiffer ride to match your preference. Just make sure you never adjust one shock without adjusting the other!

    Warnings

    Disclaimer: It should go without saying but just in case it's not obvious:

    Be safe, think it through and don't hurt yourself!

    • Doing this properly requires jacking the bike up. Otherwise you'll be trying to lift the weight of the bike with a little wrench which is less than a foot long. (leverage not in your favor)
    • Anytime you jack up your bike you must make sure you strap it down before you go putting anything fragile like your head underneath!
    • The most critical step to adjusting dual shocks is setting them both to the same position/adjustment. The process below tells you how, but make sure you don't mess this up. There are plenty of videos online capturing the horror awaiting the rider who doesn't heed this warning. Do not add your name to the list!

    Tools required:

    • A bike jack or lift, straps and the knowledge of how to use them to safely and securely lift your bike up enough to take the weight off the rear wheel.
    • Spanner Wrench: HD part 94448-82B
    • 11/16 open ended wrench

    Prep

    1. Jack the bike up so the rear tire is off the ground. The more clearance you have the easier it is. Don't forget to strap the bike down.
    2. Loosen the jam nut all the way out. This is the 11/16" nut the wrench is for. You may need some penetrating lube to break it free.
    3. With the spanner wrench, turn the adjuster plate counter-clockwise (this will cause the shock housing to move towards the front of the bike) until it is all the way up against the jam nut. This is your starting position.
    4. Then take a sharpie, paint pen or a piece of tape to mark the bottom center of the adjuster plate. You can skip this step if there is already a mark from a previous adjustment/owner and the marks on both shocks are in the same position. This is a reference point so you can adjust both of them the same. Don't worry if the two shocks are not in exactly the same position - the physical structure of the shocks themselves. This is normal and does not change anything here. This is actually why we need to mark the adjustment plate with a pen or tape.

    Adjustment

    Now you're ready to adjust the shocks to your weight/load/preference.

    1. With the spanner wrench, turn the adjuster plate clockwise no more than 4 complete turns by counting how many times the mark from step 4 crosses the 6 o'clock position. 0 turns is the stiffest setting, and 4 turns is the softest setting.

      Ballpark weight settings:

      First determine your load weight. This includes your weight as the rider (with whatever gear you wear), the weight of a passenger unless you're riding solo, and anything else you're packin' on the bike.

      • 4 turns for load up to 180 lbs
      • 3 turns for a load from 180 lbs to 235 lbs
      • 2 turns for a load from 235 lbs to 300 lbs
      • 1 turn for a load from 300 lbs to 375 lbs
      • 0 turns for a load over 375 lbs
    2. Once you have your adjustment made, lock it in position by tightening up the jam nut against the adjuster plate by turning the nut clockwise while holding the adjuster plate in place with the spanner wrench.

    3. Now repeat the exact same process on the other shock. The key is to adjust both shocks equally.