Skip to content

Instantly share code, notes, and snippets.

@dvdbng
dvdbng / pg_dump
Created December 6, 2016 21:56
rails rake db:migrate pg_dump docker
#!/bin/bash
# rake db:migrate tries to connect to dump local postgres using pg_dump but
# you are using a docker instance? try saving this script as pg_dump somewhere
# in your PATH with higher priority
orig_params="$@"
volmount=""
while [[ $# -gt 1 ]]
do
@dvdbng
dvdbng / list_bucket.py
Created August 22, 2016 09:48
List bucket skipping over some directories
def fast_list_keys(bucket, prefix="", ignore_dirs=()):
"""
Like boto.s3.bucket.list but skip over directories named in ignore_dirs
"""
marker = ""
more_results = True
ignore_dirs = set(ignore_dirs)
keys = []
while more_results:
rs = bucket.get_all_keys(prefix=prefix, marker=marker)
@dvdbng
dvdbng / github_pr_notifications.user.js
Last active August 18, 2016 14:34
An userscript that shows a desktop notification when the status of a github PR changes
// ==UserScript==
// @name PR Notifications
// @namespace ghnotifications
// @description Show notifications when the status of a PR changes
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
(function(){
from collections import Counter
import heapq
class SummaryDict(Counter):
"""
A counting dict that aggregates uncommon values in a "Others" key.
The full set of keys is kept in a separate counter, in case they need to
be added back to the main counter.
@dvdbng
dvdbng / scrapy_spider.py
Created April 20, 2016 07:53
Use scrapy and splash in the same process
import scrapy
from scrapy.crawler import CrawlerRunner
from scrapy.settings import Settings
from scrapy.http import TextResponse
from twisted.internet import defer
from splash.browser_tab import BrowserTab
from splash.render_options import RenderOptions
from splash import defaults
import random
/**
* Levenshtein distance between two arrays or strings
*/
function arrDistance(a, b) {
if(a.length === 0) { return b.length; }
if(b.length === 0) { return a.length; }
var matrix = [];
// increment along the first column of each row
# Download every episode of How It's Made from Youtube
# Needs: youtube-dl
urlencode() {
python -c "import sys, urllib; print urllib.quote_plus(sys.stdin.read())"
}
dl(){
q=`echo How its made $2 $3 $4 | urlencode`
youtube-dl --max-downloads=1 -o "HowItsMade_${1}_%(title)s_%(format)s.%(ext)s" "https://www.youtube.com/results?search_query=$q"
@dvdbng
dvdbng / xpath.js
Created October 30, 2015 08:05
Query XPath
function queryXPath(expr, ctx, type){
ctx = ctx || document;
type = type || XPathResult.ANY_TYPE;
var doc = ctx.nodeType === Node.DOCUMENT_NODE ? ctx : ctx.ownerDocument;
var nsResolver = doc.createNSResolver(doc.documentElement);
var arr = [], i = null;
try {
expr = ("" + expr).replace(/\bhasClass\(["'](\w+)["']\)/, function(expr, className){
return "contains(concat(' ', normalize-space(@class), ' '), ' " + className + " ')";
});
@dvdbng
dvdbng / headers.lua
Created October 30, 2015 08:04
Splash normalize headers
-- x-FOO-bAr -> X-Foo-Bar
function normalize_header(name)
name = upper(sub(name, 1, 1)) .. lower(sub(name, 2))
while true do
local start,fin = find(name, '-[a-z]')
if start ~= nil then
name = sub(name, 1, start) .. upper(sub(name, fin, fin)) .. sub(name, fin+1)
else
break
end
;(function(){
function makeStorage(){
var keys = [];
var storage = Object.create(Object, {
getItem: {value: function(k) {
if(this.hasOwnProperty(k)) {
return this[k];
}
}},