Skip to content

Instantly share code, notes, and snippets.

Avatar

Owen Garland bag-man

View GitHub Profile
@bag-man
bag-man / cpu.js
Last active Jun 15, 2021
How to calculate the current CPU load with Node.js; without using any external modules or OS specific calls.
View cpu.js
var os = require("os");
//Create function to get CPU information
function cpuAverage() {
//Initialise sum of idle and time of cores and fetch CPU info
var totalIdle = 0, totalTick = 0;
var cpus = os.cpus();
//Loop through CPU cores
@bag-man
bag-man / counter.py
Created May 31, 2021
Blackjack card counter
View counter.py
#!/bin/python3
import curses
import sys
import os
from math import floor
decks = 6
bet_unit = 10
def main(win, decks):
@bag-man
bag-man / command.sh
Last active May 23, 2021
mrkoll.se scraper
View command.sh
SEEDPERSON=$1
rm output.csv
urls=$(curl -s "$SEEDPERSON" | grep /person/ | grep -v Sammanf | grep grannHeader | sed -r 's/.*href=(.*)>/\1/g' | sed 's/>.*$//' | sed 's/^\//https:\/\/mrkoll.se\//')
#echo "Url, First Name, Last Name, Address, Appt. No., Postcode, DoB, Gender, Res. Since, Phone" >> output.csv
for url in $urls
do
echo -n "$url," >> output.csv
curl -s "$url" | grep -E '(tel:|block_col1)' -A 47 | grep span | sed '/tel:/q' | sed -E 's/<div class=history_span/\n<div class=history_span/' | grep -vE '(history_span|f_head1|förnamn|f_line1|mellannamn)' | sed 's/<[^>]*>//g' | sed -E 's/ lgh ([0-9]{4})/\nlgh \1\n/' | sed s/-XXXX// | sed -r '/^\s*$/d' | sed 's/^herre/M/'| sed 's/^tjej/F/' | sed 's/^dam/F/' | sed 's/^kvinna/F/' | sed 's/^man/M/' | sed '6,7d;' | dos2unix | paste -s -d ',' >> output.csv
@bag-man
bag-man / post.md
Created Feb 4, 2017
fzf + rgrep + vim mini tutorial
View post.md

I've always had fzf and ripgrep on my radar, and I've finally gotten around to using them together. Good lord it makes a world of difference, especially when added to Vim as well as Bash.

Add the following snippet to your ~/.bashrc, this add's fzf keybindings to bash and gets fzf to use ripgrep by default for faster searching.

[ -f ~/.fzf.bash ] && source ~/.fzf.bash
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'
bind -x '"\C-p": vim $(fzf);'

Okay now what can you do?

@bag-man
bag-man / recursive-factory.ts
Last active Oct 25, 2019
Generate recursive typed functions
View recursive-factory.ts
import { deepEqual } from 'assert'
type RecursiveFn<T> = (xs: T[], out?: T[]) => T[]
type OperatorFn<T> = (T: T, xs: T[], out: T[]) => T | void
type FactoryFn = <T>(fn: OperatorFn<T>) => RecursiveFn<T>
const recursiveFactory: FactoryFn = <T>(fn: OperatorFn<T>) => {
const recursive: RecursiveFn<T> = (xs, out = []) => {
const x = xs.shift()
@bag-man
bag-man / database.js
Last active Jul 22, 2019
Connect and use MongoDB with ES6 in Node 4
View database.js
'use strict'
const MongoClient = require('mongodb')
class Database {
constructor (uri) {
this.uri = uri
this.db = {}
return this
@bag-man
bag-man / XMLReader.ts
Last active Jul 11, 2019
A class wrapper around libxmljs, for reading values with XML namespaces
View XMLReader.ts
import { Document, Element, parseXml } from 'libxmljs';
import { VError } from 'verror';
export class XMLReader {
private document: Document;
private namespaces: Record<string, string>;
constructor(inputXml: string) {
try {
this.document = parseXml(inputXml);
@bag-man
bag-man / test.ts
Last active Apr 16, 2019
1 + 1 = 2 testing antipattern
View test.ts
// Avoid generating expected results as they are not testing your logic independently
describe('1+1=2 testing antipattern', () => {
describe('add', () => {
const add = (a: number, b: number): number => a + b;
const expected = 1 + 1;
it('should add two numbers', () => {
// This is testing two sets of logic, and doesnt guarantee the correctness of the function
// as the output of expected could also be wrong
expect(add(1, 1)).toEqual(expected);
@bag-man
bag-man / log
Last active Apr 10, 2019
nave debug log
View log
+ '[' -z /bin/bash ']'
++ basename /bin/bash
+ shell=bash
+ case "$shell" in
+ shopt -s extglob
+ NODEDIST=https://nodejs.org/dist
+ NAVE_CACHE_DUR=86400
++ curl --version
++ head -n1
+ NAVEUA='nave/curl 7.64.1 (x86_64-pc-linux-gnu) libcurl/7.64.1 OpenSSL/1.1.1b zlib/1.2.11 libidn2/2.1.1 libpsl/0.20.2 (+libidn2/2.1.1) libssh2/1.8.1 nghttp2/1.36.0'
View keybase.md

Keybase proof

I hereby claim:

  • I am bag-man on github.
  • I am bag_man (https://keybase.io/bag_man) on keybase.
  • I have a public key ASDcVWpn_Gzg_zivsjlklILaFDKNDBCaRxiwJdJvvdMIdQo

To claim this, I am signing this object: