Skip to content

Instantly share code, notes, and snippets.

@rctay
rctay / gist:d4c1e97aee49c5e2e946
Created April 14, 2015 15:50
www.sgxdata.pebbleslab.com scrape table to csv eg. for Excel
var rows_all = document.getElementsByTagName("table")[4].children[0].children
var rows = Array.prototype.slice.call(rows_all, 2)
var rows_data = rows.map(function(row) { return Array.prototype.map.call(row.children, function(c, i) { var v = c.innerText; return i===2 ? v.replace("\n"," ") : v }) })
// poor man's csv - doesn't handle values with the delimiter itself!
console.log(rows_data.map(function(c) { return c.join(",") }).join("\n"))
@rctay
rctay / gist:6e583e956c37a310c2f7
Created April 11, 2015 16:34
append EXIF date taken to filename
# produce lines like
#
# mv "./IMG_8869.jpg" "2015-03-29_19-08-01_IMG_8869.jpg"
#
find \( -name 'DSC_*' -or -name 'IMG_*' -or -name 'GOPR*' \) -and -iname '*.jpg'\
| while read x; do\
echo mv \"$x\" \"$(exif --tag='Date and Time (Original)' -m "$x" | sed -e 's/:/-/g' -e 's/ /_/')_${x#./}\"; done >foo
# try one line first
awk NR==1 foo | sh -v
@rctay
rctay / gist:e8c7f6e71b5096f257ea
Last active August 29, 2015 14:08
track local variables in a context
class LocalsTracker(object):
def __enter__(self):
self._locals = locals()
def __exit__(self):
locals_now = locals()
# compute diff between locals_now and self.locals...
def foo():
a = 100
#!/usr/bin/env node
// Reads JSON from stdin, and runs a JSONPath expression from the command-line on it.
//
// eg.
// $ npm install # install dependencies
// $ echo '{"store": {"book":[{"category":"fiction"}]}}' | node jsonpath.js '$.store.book[0].category'
// fiction
var stdin = process.stdin,
@rctay
rctay / browserify-deps-ex.js
Last active August 29, 2015 14:03
demo to filter out 'source' field in deps output
var browserify = require('browserify');
var JSONStream = require('JSONStream');
var through = require('through2');
var b = browserify({basedir: __dirname, entries: ['./main.js']})
b.deps({})
.pipe(through({objectMode: true}, function(chunk, enc, cb) {
// poor man's _.omit()
var o = {};
for (var prop in chunk) {
@rctay
rctay / README.md
Last active August 29, 2015 14:00
paging through git log by commits

assumption: your format.pretty begins with %h, eg. format:%h %s (%an, %ad).

@rctay
rctay / gist:9829266
Created March 28, 2014 10:01
shuffle/randomize playlist/set tracks in Soundcloud Widget eg. https://w.soundcloud.com/player/?url=http://api.soundcloud.com/users/1539950/favorites
require(["lib/play-manager"], function(a) {
// via http://stackoverflow.com/a/6274381
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuf(o) { for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); }
shuf(a.source.models);
var c = a.getCurrentSound(), p = c ? c.isPaused() : false;
a.playNext(); a.playPrev(); // poor man's refresh of current playing song
if (p) a.pause(c); // preserve first-run state
@rctay
rctay / playlist_shuffle.py
Last active November 14, 2018 19:52
SoundCloud set/playlist shuffle via Python + Requests + mpg123. Ctrl-C for next (press Ctrl-C in quick succession to quit), Ctrl-Z/fg to pause/resume
import requests
import soundcloud
import random
import socket
import subprocess
SOUNDCLOUD_API_KEY = '00000000000000000000000000000000'
PLAYLIST_URL = 'https://soundcloud.com/nervomusic/sets/nervomusic-com'
@rctay
rctay / gist:9045618
Created February 17, 2014 06:18
ocaml prologue for HackerRank (reading ints, |>, etc)
let (|>) v f = f v;;
let string_of_char_list (ls : char list) : string =
let s = String.make (List.length ls) '0' in
List.fold_left (fun i c -> s.[i] <- c; i+1) 0 ls;
s;;
let append_string (xs : string list) (ls : char list) : string list =
let s = string_of_char_list ls in
if String.length s = 0 then xs else (xs @ [s]);;
@rctay
rctay / gist:9036785
Created February 16, 2014 16:25
Python: linked lists
class Node(object):
prev = None
next = None
value = None
def __init__(self, prev, value):
self.value = value
self.appendTo(prev)
def unlink(self):