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

  • Sendgrid's UI does not give you full control over API keys. This script does. Set two environment variables and you can view your keys, their scopes and add/remove scopes from any key. Simple utility but saves a lot of time and headache.
    #!/usr/bin/env ruby
    require 'base64'
    require 'rest-client'
    require 'json'
    # https://stackoverflow.com/questions/38613941/sendgrid-error-access-forbidden-when-trying-to-get-user-profile-api
    $base_endpiont = "https://api.sendgrid.com/v3/api_keys"
    def usage
    exe = File.basename $0
    puts
    puts "Usage:"
    puts "\t#{exe}"
    puts "\t - list keys"
    puts "\t#{exe} [ KEY ID ]"
    puts "\t - view a key's details"
    puts "\t#{exe} [ KEY ID ] add [ SCOPES ... ]"
    puts "\t - add one or more scopes to a key"
    puts "\t#{exe} [ KEY ID ] del [ SCOPES ... ]"
    puts "\t - remove one or more scopes from a key"
    puts
    puts "A list of available scopes can be found here:"
    puts "https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/api_key_permissions_list.html"
    puts
    end
    def basicAuth
    "Basic #{ Base64.encode64("#{ENV['SENDGRID_USERNAME']}:#{ENV['SENDGRID_PASSWORD']}") }"
    end
    def get_key(key=nil)
    url = $base_endpiont
    if key
    url += "/#{key}"
    end
    begin
    resp = RestClient.get(url, headers={Authorization: basicAuth})
    JSON.parse(resp.body)
    rescue Exception => e
    puts e
    puts e.response
    exit
    end
    end
    def save_key(id, key)
    if not key or key['errors']
    puts "Key not found"
    exit
    end
    key.delete('api_key_id')
    key['scopes'].uniq!
    begin
    resp = RestClient.put("#{$base_endpiont}/#{id}", key.to_json, headers={Authorization: basicAuth, 'Content-Type' => 'application/json'})
    JSON.parse(resp.body)
    rescue Exception => e
    puts e
    puts e.response
    exit
    end
    end
    def add_scope(id, *scopes)
    key = (get_key id)
    key['scopes'].concat scopes
    res = save_key(id, key)
    print_key res
    end
    def del_scope(id, *scopes)
    key = (get_key id)
    key['scopes'] = key['scopes'] - scopes
    res = save_key(id, key)
    print_key res
    end
    def print_key(key)
    if not key or key['errors']
    puts "Key not found"
    exit
    end
    puts "Name: #{key['name']}"
    puts "Key ID: #{key['api_key_id']}"
    puts "Scopes:"
    key['scopes'].each { |scope|
    puts " - #{scope}"
    }
    end
    case ARGV.size
    when 0 then
    res = get_key
    res['result'].each { |key|
    puts "#{key['name']}: #{key['api_key_id']}"
    }
    when 1 then
    print_key(get_key ARGV[0])
    when 3..100
    case ARGV[1].downcase
    when "add" then
    add_scope ARGV[0], *ARGV.drop(2)
    when "del" then
    del_scope ARGV[0], *ARGV.drop(2)
    else
    puts ">> #{ARGV[1].downcase}"
    usage
    end
    else
    usage
    end
  • Run chrome completely separate from the standard as though it has never been run before on that computer, but skip the welcome wizard stuff.
    #!/bin/bash
    app="/Applications/Google Chrome.app"
    dir=$(mktemp -d)
    # echo $dir
    touch "${dir}/First Run"
    mkdir "${dir}/Default"
    echo '{"browser":{"has_seen_welcome_page":true}}' > "${dir}/Default/Preferences"
    exec "${app}/Contents/MacOS/Google Chrome" --bwsi --disable-signin-promo --bypass-app-banner-engagement-checks --no-managed-user-acknowledgment-check -no-first-run --user-data-dir=${dir} --system-developer-mode
  • 45.380328, -63.266579
  • Python function to wait for a recently published version of an npm package to become available. Too many times I've been bitten by npm publish returning but the package not becoming available for several seconds, minutes and sometimes even hours later. If you automate your deployment with python this can help you too.
    import sys
    import json
    import time
    # pip install npm
    from npm.bindings import npm_run
    def wait(package, version):
    while True:
    err, raw = npm_run('view','--json', package)
    sys.stdout.write('.')
    sys.stdout.flush()
    if err:
    raise Exception('npm view returned an error: ' + str(err))
    info = json.loads(raw)
    if version in info['versions']:
    print('')
    break
    time.sleep(1)
    wait(*sys.argv[1:])
    # python wait-until-published.py PACKAGE VERSION
    # python wait-until-published.py npm 6.9.0
    # python wait-until-published.py @org/pkg 1.2.3
  • Does it irritate anyone else as much as me when you hit the back button and the video keep on playing but all you can see is the audio and it's not clear how to make the audio stop? This fixes that on FireTV+Kodi.

    Run Directly

    curl https://gist.githubusercontent.com/xcsrz/780017ebbd64b2435dc58b1053bf4858/raw/f9d36a87daba4e464a1e61d35172c313ee2fea7f/adb-fix-kodi-back-button-amazon-fire-tv.sh | bash
    
    #/bin/sh
    dir=$(mktemp -d)
    echo "working in ${dir}"
    cat << EOF > "${dir}/keymap.xml"
    <?xml version="1.0" encoding="UTF-8"?>
    <keymap>
    <FullscreenVideo>
    <keyboard>
    <backspace>Stop</backspace>
    </keyboard>
    </FullscreenVideo>
    </keymap>
    EOF
    adb push "${dir}/keymap.xml" "storage/sdcard0/Android/data/org.xbmc.kodi/files/.kodi/userdata/keymaps/"